diff options
author | adamw <adam@warski.org> | 2017-07-19 14:45:11 +0200 |
---|---|---|
committer | adamw <adam@warski.org> | 2017-07-19 14:45:11 +0200 |
commit | 5aaac06c2d5ea122470ee7b27277ac0747e767d1 (patch) | |
tree | b63519364f12c952cf1b9100ae8706e2877a0324 /akka-http-handler/src/main | |
parent | 4cde9e296663eecd866e0e4e38a955e0c6c05f62 (diff) | |
download | sttp-5aaac06c2d5ea122470ee7b27277ac0747e767d1.tar.gz sttp-5aaac06c2d5ea122470ee7b27277ac0747e767d1.tar.bz2 sttp-5aaac06c2d5ea122470ee7b27277ac0747e767d1.zip |
How the response should be handled is now part of the request definition
Diffstat (limited to 'akka-http-handler/src/main')
-rw-r--r-- | akka-http-handler/src/main/scala/com/softwaremill/sttp/akkahttp/AkkaHttpSttpHandler.scala | 33 | ||||
-rw-r--r-- | akka-http-handler/src/main/scala/com/softwaremill/sttp/akkahttp/package.scala | 16 |
2 files changed, 14 insertions, 35 deletions
diff --git a/akka-http-handler/src/main/scala/com/softwaremill/sttp/akkahttp/AkkaHttpSttpHandler.scala b/akka-http-handler/src/main/scala/com/softwaremill/sttp/akkahttp/AkkaHttpSttpHandler.scala index 866aaad..62e066b 100644 --- a/akka-http-handler/src/main/scala/com/softwaremill/sttp/akkahttp/AkkaHttpSttpHandler.scala +++ b/akka-http-handler/src/main/scala/com/softwaremill/sttp/akkahttp/AkkaHttpSttpHandler.scala @@ -19,22 +19,23 @@ import scala.collection.immutable.Seq class AkkaHttpSttpHandler(actorSystem: ActorSystem) extends SttpHandler[Future, Source[ByteString, Any]] { + // the supported stream type + private type S = Source[ByteString, Any] + def this() = this(ActorSystem("sttp")) private implicit val as = actorSystem private implicit val materializer = ActorMaterializer() import as.dispatcher - override def send[T](r: Request, - responseAs: ResponseAs[T, Source[ByteString, Any]]) - : Future[Response[T]] = { + override def send[T](r: Request[T, S]): Future[Response[T]] = { requestToAkka(r) .map(setBodyOnAkka(r, r.body, _).get) .flatMap(Http().singleRequest(_)) .flatMap { hr => val code = hr.status.intValue() - bodyFromAkka(responseAs, hr).map( - Response(_, code, headersFromAkka(hr))) + bodyFromAkka(r.responseAs, hr) + .map(Response(_, code, headersFromAkka(hr))) } } @@ -51,7 +52,7 @@ class AkkaHttpSttpHandler(actorSystem: ActorSystem) case _ => HttpMethod.custom(m.m) } - private def bodyFromAkka[T](rr: ResponseAs[T, Source[ByteString, Any]], + private def bodyFromAkka[T](rr: ResponseAs[T, S], hr: HttpResponse): Future[T] = { def asByteArray = hr.entity.dataBytes @@ -88,7 +89,7 @@ class AkkaHttpSttpHandler(actorSystem: ActorSystem) ch :: (cl.toList ++ other) } - private def requestToAkka(r: Request): Future[HttpRequest] = { + private def requestToAkka(r: Request[_, S]): Future[HttpRequest] = { val ar = HttpRequest(uri = r.uri.toString, method = methodToAkka(r.method)) val parsed = r.headers.filterNot(isContentType).map(h => HttpHeader.parse(h._1, h._2)) @@ -106,11 +107,11 @@ class AkkaHttpSttpHandler(actorSystem: ActorSystem) } } - private def setBodyOnAkka(r: Request, - body: RequestBody, + private def setBodyOnAkka(r: Request[_, S], + body: RequestBody[S], ar: HttpRequest): Try[HttpRequest] = { getContentTypeOrOctetStream(r).map { ct => - def doSet(body: RequestBody): HttpRequest = body match { + def doSet(body: RequestBody[S]): HttpRequest = body match { case NoBody => ar case StringBody(b, encoding) => val ctWithEncoding = HttpCharsets @@ -124,21 +125,15 @@ class AkkaHttpSttpHandler(actorSystem: ActorSystem) ar.withEntity( HttpEntity(ct, StreamConverters.fromInputStream(() => b))) case PathBody(b) => ar.withEntity(ct, b) - case s @ SerializableBody(_, _) => doSetSerializable(s) + case StreamBody(s) => ar.withEntity(HttpEntity(ct, s)) + case SerializableBody(f, t) => doSet(f(t)) } - def doSetSerializable[T](body: SerializableBody[T]): HttpRequest = - body match { - case SerializableBody(SourceBodySerializer, t) => - ar.withEntity(HttpEntity(ct, t)) - case SerializableBody(f, t) => doSet(f(t)) - } - doSet(body) } } - private def getContentTypeOrOctetStream(r: Request): Try[ContentType] = { + private def getContentTypeOrOctetStream(r: Request[_, S]): Try[ContentType] = { r.headers .find(isContentType) .map(_._2) diff --git a/akka-http-handler/src/main/scala/com/softwaremill/sttp/akkahttp/package.scala b/akka-http-handler/src/main/scala/com/softwaremill/sttp/akkahttp/package.scala deleted file mode 100644 index 0357dd8..0000000 --- a/akka-http-handler/src/main/scala/com/softwaremill/sttp/akkahttp/package.scala +++ /dev/null @@ -1,16 +0,0 @@ -package com.softwaremill.sttp - -import akka.stream.scaladsl.Source -import akka.util.ByteString -import com.softwaremill.sttp.model.{BasicRequestBody, BodySerializer} - -package object akkahttp { - private[akkahttp] case object SourceBodySerializer - extends BodySerializer[Source[ByteString, Any]] { - def apply(t: Source[ByteString, Any]): BasicRequestBody = - throw new RuntimeException("Can only be used with akka-http handler") - } - - implicit val sourceBodySerializer: BodySerializer[Source[ByteString, Any]] = - SourceBodySerializer -} |