aboutsummaryrefslogtreecommitdiff
path: root/async-http-client-handler/src
diff options
context:
space:
mode:
authoradamw <adam@warski.org>2017-08-31 14:32:01 +0200
committeradamw <adam@warski.org>2017-08-31 14:32:01 +0200
commit71f6a1eeee412045cc08ce8894194573362cb8f0 (patch)
treec7618bb53f6e08c30df4e943f56d16fc1bfb80c1 /async-http-client-handler/src
parent5bc89ddefab16dd814d0b716a72490451b697b32 (diff)
downloadsttp-71f6a1eeee412045cc08ce8894194573362cb8f0.tar.gz
sttp-71f6a1eeee412045cc08ce8894194573362cb8f0.tar.bz2
sttp-71f6a1eeee412045cc08ce8894194573362cb8f0.zip
Response.body is now an Either[String, T], to handle cases when the status code isn't 2xx
Diffstat (limited to 'async-http-client-handler/src')
-rw-r--r--async-http-client-handler/src/main/scala/com/softwaremill/sttp/asynchttpclient/AsyncHttpClientHandler.scala27
1 files changed, 22 insertions, 5 deletions
diff --git a/async-http-client-handler/src/main/scala/com/softwaremill/sttp/asynchttpclient/AsyncHttpClientHandler.scala b/async-http-client-handler/src/main/scala/com/softwaremill/sttp/asynchttpclient/AsyncHttpClientHandler.scala
index 2e5d16b..f8122cc 100644
--- a/async-http-client-handler/src/main/scala/com/softwaremill/sttp/asynchttpclient/AsyncHttpClientHandler.scala
+++ b/async-http-client-handler/src/main/scala/com/softwaremill/sttp/asynchttpclient/AsyncHttpClientHandler.scala
@@ -62,6 +62,8 @@ abstract class AsyncHttpClientHandler[R[_], S](asyncHttpClient: AsyncHttpClient,
protected def publisherToStreamBody(p: Publisher[ByteBuffer]): S
+ protected def publisherToString(p: Publisher[ByteBuffer]): R[String]
+
private def eagerAsyncHandler[T](
responseAs: ResponseAs[T, S],
success: R[Response[T]] => Unit,
@@ -134,8 +136,15 @@ abstract class AsyncHttpClientHandler[R[_], S](asyncHttpClient: AsyncHttpClient,
val baseResponse = readResponseNoBody(builder.build())
val p = publisher.getOrElse(EmptyPublisher)
val s = publisherToStreamBody(p)
- val t = responseAs.responseIsStream(s)
- success(rm.unit(baseResponse.copy(body = t)))
+ val b = if (codeIsSuccess(baseResponse.code)) {
+ rm.unit(Right(responseAs.responseIsStream(s)))
+ } else {
+ rm.map(publisherToString(p), Left(_: String))
+ }
+
+ success(rm.map(b, { bb: Either[String, T] =>
+ baseResponse.copy(body = bb)
+ }))
}
}
@@ -223,12 +232,20 @@ abstract class AsyncHttpClientHandler[R[_], S](asyncHttpClient: AsyncHttpClient,
private def readEagerResponse[T](
response: AsyncResponse,
responseAs: ResponseAs[T, S]): R[Response[T]] = {
- val body = eagerResponseHandler(response).handle(responseAs, rm)
- rm.map(body, (b: T) => readResponseNoBody(response).copy(body = b))
+ val base = readResponseNoBody(response)
+
+ val body = if (codeIsSuccess(base.code)) {
+ rm.map(eagerResponseHandler(response).handle(responseAs, rm), Right(_: T))
+ } else {
+ rm.map(eagerResponseHandler(response).handle(asString, rm),
+ Left(_: String))
+ }
+
+ rm.map(body, (b: Either[String, T]) => base.copy(body = b))
}
private def readResponseNoBody(response: AsyncResponse): Response[Unit] = {
- Response((),
+ Response(Right(()),
response.getStatusCode,
response.getHeaders
.iterator()