jove.notebook.services.KernelSpecs.scala Maven / Gradle / Ivy
The newest version!
package jove.notebook.services
import com.google.common.io.Resources
import jove.helpers.Kernels
import jove.notebook._
import org.http4s.headers.`Content-Type`
import org.http4s.MediaType
import org.http4s.dsl._
import org.http4s.argonaut._
import argonaut._, Argonaut._, Shapeless._
import scala.util.{Try, Failure, Success}
object KernelSpecs {
def apply(kernel: Kernels): Plan = {
object KernelSpecPath extends PrefixedDecodedPath(Root / "kernelspecs")
object KernelSpec {
def unapply(path: Path): Option[(String, Path)] =
KernelSpecPath.unapply(path).collect {
case name :: t if t.nonEmpty => (name, Path(t))
}
}
{
case GET -> KernelSpec(name, path) =>
val _path = List("kernel", name, "resources") ++ path.toList
val extension = _path.last.split('.').last
Try(Resources.toByteArray(getClass.getClassLoader getResource _path.mkString("/"))) match {
case Success(res) =>
Ok(res) withContentType MediaType.forExtension(extension).map(`Content-Type`(_))
case Failure(err) =>
NotFound("")
}
case GET -> Root / "api" / "kernelspecs" =>
Ok(Protocol.KernelSpecs(
kernel.defaultKernel getOrElse "",
kernel.kernelsWithInfo.map{case (id, (info, _)) => id -> Protocol.KernelSpec(id, info.name) }
).asJson)
case GET -> Root / "api" / "kernelspecs" / id =>
kernel.kernelName(id) match {
case Some(name) =>
Ok(Protocol.KernelSpec(id, name).asJson)
case None =>
BadRequest("")
}
}
}
}