Created
March 11, 2015 12:57
-
-
Save hamnis/f322895c6f5379759e42 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package unfilteredx | |
import unfiltered.response.{ContentLength, InternalServerError, ResponseFunction} | |
import scala.language.implicitConversions | |
import scala.language.higherKinds | |
import scala.concurrent.{ExecutionContext, Future} | |
import d2.{Result, Directive, Directives} | |
import unfiltered.request._ | |
import unfiltered.directives.{Result => Res} | |
import scalaz._ | |
import Id._ | |
import std.scalaFuture._ | |
package object directives2 { | |
def futuredirectives(implicit ec: ExecutionContext) = EnhancedDirectives[Future] | |
val identitydirectives = EnhancedDirectives[Id] | |
def AsyncPath(handler: PartialFunction[Throwable, ResponseFunction[Any]] = kit.ResponseException.defaultAsync) = d2.Async(handler).Mapping[Any, String] { | |
case Path(p) => p | |
} | |
trait EnhancedDirectives[F[+_]] extends Directives[F] { | |
val enhancedOps: DirectiveOps[F] | |
} | |
object EnhancedDirectives { | |
def apply[F[+_]](implicit M:Monad[F]):EnhancedDirectives[F] = new EnhancedDirectives[F] { | |
implicit val F: Monad[F] = M | |
val enhancedOps: DirectiveOps[F] = new DirectiveOps(this)(M) | |
} | |
} | |
class DirectiveOps[F[+_]](dir: Directives[F])(implicit m: Monad[F]) { | |
implicit def queryParamsDirective[A, L](t: QueryParams.type): Directive[A, F, L, Map[String, Seq[String]]] = { | |
dir.request[A].map{case QueryParams(qp) => qp} | |
} | |
implicit def stringHeaderDirective[A, L](Header: StringHeader): Directive[A, F, L, String] = { | |
dir.request[A].map{case Header(qp) => qp} | |
} | |
implicit def fromUnfilteredDirective[T, L, R](d1: unfiltered.directives.Directive[T, L, R]): Directive[T, F, L, R] = { | |
dir.Directive{ r => | |
val res = d1(r) | |
res match { | |
case Res.Success(s) => m.point(Result.Success(s)) | |
case Res.Failure(e) => m.point(Result.Failure(e)) | |
case Res.Error(e) => m.point(Result.Error(e)) | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment