From bc685df2cd50814b45e669f4f602732887c2879c Mon Sep 17 00:00:00 2001 From: adamw Date: Sat, 15 Jul 2017 10:58:24 +0200 Subject: Headers & errors support --- .../com/softwaremill/sttp/akkahttp/AkkaHttpSttpHandler.scala | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'akka-http-handler') 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 fc2d632..9125ca3 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,6 +14,7 @@ import com.softwaremill.sttp.model._ import scala.concurrent.Future import scala.util.{Failure, Success, Try} +import scala.collection.immutable.Seq class AkkaHttpSttpHandler(actorSystem: ActorSystem) extends SttpHandler[Future, Source[ByteString, Any]] { @@ -32,7 +33,8 @@ class AkkaHttpSttpHandler(actorSystem: ActorSystem) .flatMap(Http().singleRequest(_)) .flatMap { hr => val code = hr.status.intValue() - bodyFromAkka(responseAs, hr).map(Response(code, _)) + bodyFromAkka(responseAs, hr).map( + Response(_, code, headersFromAkka(hr))) } } @@ -72,6 +74,14 @@ class AkkaHttpSttpHandler(actorSystem: ActorSystem) } } + private def headersFromAkka(hr: HttpResponse): Seq[(String, String)] = { + val ch = ContentTypeHeader -> hr.entity.contentType.toString() + val cl = + hr.entity.contentLengthOption.map(ContentLengthHeader -> _.toString) + val other = hr.headers.map(h => (h.name, h.value)) + ch :: (cl.toList ++ other) + } + private def requestToAkka(r: Request): Future[HttpRequest] = { val ar = HttpRequest(uri = r.uri.toString, method = methodToAkka(r.method)) val parsed = -- cgit v1.2.3