diff options
author | adamw <adam@warski.org> | 2017-07-04 13:46:27 +0200 |
---|---|---|
committer | adamw <adam@warski.org> | 2017-07-04 13:46:27 +0200 |
commit | 09da476faedc382d7f2604be142a353e5851a1e3 (patch) | |
tree | bb70efb1e7bcb100c6fb2234aa277daaa84a393b /akka-http-handler/src/main/scala/com | |
parent | 446dfa70716928c9a92c30ced94a7be6e62d73f0 (diff) | |
download | sttp-09da476faedc382d7f2604be142a353e5851a1e3.tar.gz sttp-09da476faedc382d7f2604be142a353e5851a1e3.tar.bz2 sttp-09da476faedc382d7f2604be142a353e5851a1e3.zip |
First version of unified send
Diffstat (limited to 'akka-http-handler/src/main/scala/com')
-rw-r--r-- | akka-http-handler/src/main/scala/com/softwaremill/sttp/akkahttp/AkkaHttpSttpHandler.scala | 18 |
1 files changed, 8 insertions, 10 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 7c36da6..668da90 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 @@ -14,27 +14,22 @@ import com.softwaremill.sttp.model._ import scala.concurrent.Future -class AkkaHttpSttpHandler(actorSystem: ActorSystem) extends SttpStreamHandler[Future, Source[ByteString, Any]] { +class AkkaHttpSttpHandler(actorSystem: ActorSystem) + extends SttpHandler[Future, Source[ByteString, Any], ResponseAs] { + 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]): Future[Response[T]] = { + override def send[T](r: Request, responseAs: ResponseAs[T, Source[ByteString, Any]]): Future[Response[T]] = { requestToAkka(r).flatMap(setBodyOnAkka(r, r.body, _)).flatMap(Http().singleRequest(_)).flatMap { hr => val code = hr.status.intValue() bodyFromAkka(responseAs, hr).map(Response(code, _)) } } - override def send(r: Request, responseAsStream: ResponseAsStream[Source[ByteString, Any]]): Future[Response[Source[ByteString, Any]]] = { - requestToAkka(r).flatMap(setBodyOnAkka(r, r.body, _)).flatMap(Http().singleRequest(_)).map { hr => - val code = hr.status.intValue() - Response(code, hr.entity.dataBytes) - } - } - private def methodToAkka(m: Method): HttpMethod = m match { case Method.GET => HttpMethods.GET case Method.HEAD => HttpMethods.HEAD @@ -48,7 +43,7 @@ class AkkaHttpSttpHandler(actorSystem: ActorSystem) extends SttpStreamHandler[Fu case _ => HttpMethod.custom(m.m) } - private def bodyFromAkka[T](rr: ResponseAs[T], hr: HttpResponse): Future[T] = { + private def bodyFromAkka[T](rr: ResponseAs[T, Source[ByteString, Any]], hr: HttpResponse): Future[T] = { def asByteArray = hr.entity.dataBytes .runFold(ByteString(""))(_ ++ _) .map(_.toArray[Byte]) @@ -63,6 +58,9 @@ class AkkaHttpSttpHandler(actorSystem: ActorSystem) extends SttpStreamHandler[Fu case ResponseAsByteArray => asByteArray + + case r @ ResponseAsStream() => + Future.successful(r.x(hr.entity.dataBytes)) } } |