A suite of scala libraries for building and consuming RESTful web services on top of Akka: lightweight, asynchronous, non-blocking, actor-based, testable
responseHeader(`Date`, new DateTime()) {
complete()
}
This my error:
could not find implicit value for parameter m: akka.http.scaladsl.marshalling.Marshaller[v2d2.actions.generic.protocol.Foo,akka.http.scaladsl.model.RequestEntity]
This is my protocol:
case class Foo(thing1:String, thing2:Seq[String], thing3:String)
trait FooJsonProtocol extends DefaultJsonProtocol {
implicit val sendFooFormat = jsonFormat3(Foo.apply)
}
object FooJsonProtocol extends FooJsonProtocol
This my http client side request
class Blah(muc: MultiUserChat) extends Actor with ActorLogging with FooJsonProtocol {
import system.dispatcher
implicit val system = ActorSystem()
implicit val materializer = ActorMaterializer()
implicit val timeout = Timeout(25.seconds)
def receive: Receive = {
case bar:Bar =>
val foo = Foo(bar.a, bar.b, bar.c)
val content = for {
request <- Marshal(foo).to[RequestEntity]
response <- Http().singleRequest(
HttpRequest(
method = HttpMethods.POST,
"https://blah.com"),
entity = request))
entity <- Unmarshal(response.entity).to[String]
} yield entity
content onComplete {
case Success(s) => self ! s
case Failure(t) =>
context.parent ! "An error has occured: " + t.getMessage
}
}
sealed trait PetKind
sealed trait CatKind extends PetKind
sealed trait DogKind extends PetKind
sealed trait Pet[Kind <: PetKind]
final case class Cat(favoriteFish: String) extends Pet[CatKind]
final case class Dog(favoriteBone: String) extends Pet[DogKind]
final case class Pets(animals: Seq[Pet[_])
sealed trait PetKind
sealed trait CatKind
sealed trait DogKind
sealed trait Pet {
type Kind <: PetKind
}
final case class Cat(favoriteFish: String) extends Pet {
type Kind = CatKind
}
final case class Dog(favoriteBone: String) extends Pet {
type Kind = DogKind
}
final case class Pets(animals: Seq[Pet])
code
def rejection(implicit logging: LoggingContext) = case MalformedQueryParamRejection(param, message, error) :: _ => ctx =>
respondToBadRequest(ctx, s"Malformed params: $param")
case MissingQueryParamRejection(param) :: _ => ctx =>
respondToBadRequest(ctx, s"Missing params: $param")
case MissingFormFieldRejection(field) :: _ => ctx =>
respondToBadRequest(ctx, s"Missing field $field")
case MalformedFormFieldRejection(field, error, cause) :: _ => ctx =>
respondToBadRequest(ctx, s"Malformed field $field. Error: $error")
case MalformedRequestContentRejection(message, cause) :: _ => ctx =>
respondToBadRequest(ctx, message)
case RequestEntityExpectedRejection :: _ => ctx => {
val message =
s"""Request entity expected but not supplied.
| Target: ${ctx.request.unmatchedPath}
| Entity: ${ctx.request.entity.asString}
"""
respondToBadRequest(ctx, message)
}
case SchemeRejection(message) :: _ => ctx =>
respondToBadRequest(ctx, s"Schema rejection. Error: $message")
case MethodRejection(message) :: _ => ctx =>
respondToRejection(ctx, s"Method not allowed on this resource. Error $message", MethodNotAllowed)
case UnsupportedRequestContentTypeRejection(error) :: _ => ctx =>
respondToBadRequest(ctx, error)
case ValidationRejection(message, cause) :: _ => ctx =>
respondToBadRequest(ctx, s"Validation rejection: $message")
}
import com.konga.order.models.OrderJsonSupport._
import DefaultJsonProtocol._
RouteTest
in the testkit appears to eagerly create and start the actor system. While this normally wouldn't be an issue, we use ScalaTest's tagging to include/exclude tests. The eager loading slows things down considerably as the actor system will be initialized even though no tests are actually run.