jove.notebook.services.Sessions.scala Maven / Gradle / Ivy
The newest version!
package jove.notebook.services
import jove.helpers.Kernels
import jove.notebook._
import jove.notebook.components.Session
import jove.protocol.NbUUID
import org.http4s.dsl._
import org.http4s.argonaut._
import argonaut._, Argonaut._, Shapeless._
import scalaz._, Scalaz._
object Sessions {
def apply(kernel: Kernels, session: Session): Plan = {
case GET -> Root / "api" / "sessions" =>
Ok(session.dummySessions.toList.map{
case (id, session) =>
Protocol.Session(
id,
Protocol.Session.Notebook(session.notebook.path),
Protocol.Session.Kernel(session.kernel.id, session.kernel.name)
)
}.asJson)
case req @ (POST -> Root / "api" / "sessions") =>
req.decode[Json] { reqJson =>
val c = reqJson.hcursor
for {
notebookPath <- c.--\("notebook").--\("path").as[String].result.leftMap(_ => "No notebook path specified")
kernelId <- {
(c.--\("kernel").--\("id").as[String].result orElse c.--\("kernel").--\("name").as[String].result).toOption.filter(kernel.kernel(_).nonEmpty)
.orElse(kernel.kernelFor(notebookPath))
.orElse(kernel.defaultKernel)
} toRightDisjunction "No kernel or kernel not found"
} yield {
val id = NbUUID.randomUUID().toString
val _session = Protocol.Session(id, Protocol.Session.Notebook(notebookPath), Protocol.Session.Kernel(kernelId, kernelId))
session.addDummySession(id, _session)
_session
}
}
case GET -> Root / "api" / "sessions" / id =>
for {
_session <- session.dummySessions.get(id) toRightDisjunction s"Session $id not found"
} yield
Protocol.Session(
id,
Protocol.Session.Notebook(_session.notebook.path),
Protocol.Session.Kernel(_session.kernel.id, _session.kernel.name)
)
case PATCH -> Root / "api" / "sessions" / id =>
// means notebook renamed -> we don't care
NoContent()
case DELETE -> Root / "api" / "sessions" / id =>
// FIXME Really stop the session somehow here
session.deleteDummySession(id)
NoContent()
}
}