All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jove.notebook.services.package.scala Maven / Gradle / Ivy

The newest version!
package jove.notebook

import java.net.URLDecoder

import org.http4s.{MediaRange, EntityDecoder, Response}

import language.implicitConversions

import argonaut._, Argonaut._
import org.http4s.argonaut._

import org.http4s.dsl._
import scalaz.\/
import scalaz.concurrent.Task

trait ServicesLowPriorityImplicits {
  implicit def disjunctionToResult[T: EncodeJson](d: String \/ T): Task[Response] =
    d.bimap(BadRequest(_), t => Ok(t.asJson)).merge
}

package object services extends ServicesLowPriorityImplicits {
  implicit def disjunctionToNoContentResult(d: String \/ Unit): Task[Response] =
    d.bimap(BadRequest(_), t => NoContent()).merge


  class PrefixedDecodedPath(prefix: Path) {
    val base = prefix.toList
    val baseLength = base.toList.length

    def unapply(path: Path): Option[List[String]] = {
      val l = path.toList
      if (l startsWith base)
        l.drop(baseLength) match {
          case _l if _l.nonEmpty =>
            Some(_l.map(URLDecoder.decode(_, "UTF-8")))
          case _ =>
            None
        }
      else
        None
    }
  }

  implicit val noneEntityDecoder = EntityDecoder.decodeBy(MediaRange.`*/*`)(_ => org.http4s.DecodeResult.success(None))

  implicit def optionEntityDecoder[T: EntityDecoder]: EntityDecoder[Option[T]] =
    EntityDecoder[T].map(Some(_): Option[T]) orElse noneEntityDecoder.map(x => x: Option[T])
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy