Skip to content

Commit

Permalink
Adds missing package object and minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
tlazaro committed Mar 19, 2022
1 parent 8464261 commit c64ee07
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 7 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]()

Expand Down
17 changes: 17 additions & 0 deletions play-akka-streams/src/main/scala/dev/playmonad/package.scala
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
17 changes: 17 additions & 0 deletions play-iteratees/src/main/scala/dev/playmonad/package.scala
Original file line number Diff line number Diff line change
@@ -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)
}
5 changes: 1 addition & 4 deletions sample-play25/app/controllers/HelloController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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"))
}
}
}

0 comments on commit c64ee07

Please sign in to comment.