diff --git a/build.sbt b/build.sbt index ce7f971..80c22b3 100644 --- a/build.sbt +++ b/build.sbt @@ -6,7 +6,7 @@ ThisBuild / sonatypeRepository := "https://s01.oss.sonatype.org/service/local" inThisBuild( List( organization := "dev.playmonad", - homepage := Some(url("https://github.com/tlazaro/playmonad")), + homepage := Some(url("https://github.com/tlazaro/play-monad")), licenses := List("Apache-2.0" -> url("http://www.apache.org/licenses/LICENSE-2.0")), developers := List( Developer( diff --git a/play-akka-streams/src/main/scala/dev/playmonad/MonadicAction.scala b/play-akka-streams/src/main/scala/dev/playmonad/MonadicAction.scala index faebd0a..d1da750 100644 --- a/play-akka-streams/src/main/scala/dev/playmonad/MonadicAction.scala +++ b/play-akka-streams/src/main/scala/dev/playmonad/MonadicAction.scala @@ -58,7 +58,7 @@ object HeaderReader extends MonadicActionImplicits { object BodyReader extends MonadicActionImplicits { type Aux[Body, A] = IndexedStateT[EitherT[Future, Result, *], HeaderReader, BodyReader[Body], Future[A]] - def apply[A](bodyParser: BodyParser[A]): BodyReader.Aux[A, A] = + def withBody[A](bodyParser: BodyParser[A]): BodyReader.Aux[A, A] = IndexedStateT[EitherT[Future, Result, *], HeaderReader, BodyReader[A], Future[A]] { state => val promise = Promise[A]() diff --git a/play-akka-streams/src/main/scala/dev/playmonad/package.scala b/play-akka-streams/src/main/scala/dev/playmonad/package.scala new file mode 100644 index 0000000..23842dd --- /dev/null +++ b/play-akka-streams/src/main/scala/dev/playmonad/package.scala @@ -0,0 +1,17 @@ +package dev + +import play.api.mvc.BodyParser +import play.api.mvc.Results.BadRequest + +import scala.concurrent.Future + +package object playmonad { + def header(name: String): HeaderReader.Aux[String] = HeaderReader.withHeadersM { request => + request.headers.get(name) match { + case Some(value) => Future.successful(Right(value)) + case None => Future.successful(Left(BadRequest(s"Header $name must be provided"))) + } + } + + def body[A](bodyParser: BodyParser[A]): BodyReader.Aux[A, A] = BodyReader.withBody(bodyParser) +} diff --git a/play-iteratees/src/main/scala/dev/playmonad/MonadicAction.scala b/play-iteratees/src/main/scala/dev/playmonad/MonadicAction.scala index fcf29c4..ab082c7 100644 --- a/play-iteratees/src/main/scala/dev/playmonad/MonadicAction.scala +++ b/play-iteratees/src/main/scala/dev/playmonad/MonadicAction.scala @@ -9,7 +9,6 @@ import scala.concurrent.{ExecutionContext, Future, Promise} sealed trait RequestReader case class HeaderReader(requestHeader: RequestHeader) extends RequestReader -case class HeaderReaderHeaderReader(requestHeader: RequestHeader) extends RequestReader case class BodyReader[A](accumulator: Iteratee[Array[Byte], Either[Result, A]]) extends RequestReader trait MonadicActionImplicits { diff --git a/play-iteratees/src/main/scala/dev/playmonad/package.scala b/play-iteratees/src/main/scala/dev/playmonad/package.scala new file mode 100644 index 0000000..23842dd --- /dev/null +++ b/play-iteratees/src/main/scala/dev/playmonad/package.scala @@ -0,0 +1,17 @@ +package dev + +import play.api.mvc.BodyParser +import play.api.mvc.Results.BadRequest + +import scala.concurrent.Future + +package object playmonad { + def header(name: String): HeaderReader.Aux[String] = HeaderReader.withHeadersM { request => + request.headers.get(name) match { + case Some(value) => Future.successful(Right(value)) + case None => Future.successful(Left(BadRequest(s"Header $name must be provided"))) + } + } + + def body[A](bodyParser: BodyParser[A]): BodyReader.Aux[A, A] = BodyReader.withBody(bodyParser) +} diff --git a/sample-play25/app/controllers/HelloController.scala b/sample-play25/app/controllers/HelloController.scala index 1dee9da..8dba78c 100644 --- a/sample-play25/app/controllers/HelloController.scala +++ b/sample-play25/app/controllers/HelloController.scala @@ -6,7 +6,6 @@ import dev.playmonad._ import play.api.Play._ import play.api.libs.concurrent.Execution.Implicits.defaultContext import play.api.mvc.BodyParsers -import play.api.mvc.Action import scala.concurrent.Future @@ -36,9 +35,7 @@ class HelloController { _ <- auth(name) jsonBody <- body(BodyParsers.parse.tolerantJson) } yield { - jsonBody.map { json => - Ok(s"Hello $name, from $agent and ${json.toString()}") - } + jsonBody.map(json => Ok(s"Hello $name, from $agent and $json")) } } }