From 666755ede0d221c11c1eac6aac57ce3d18c42c8b Mon Sep 17 00:00:00 2001 From: adamw Date: Thu, 31 Aug 2017 15:11:01 +0200 Subject: Keep a history of redirect responses --- .../scala/com/softwaremill/sttp/HttpURLConnectionHandler.scala | 2 +- core/src/main/scala/com/softwaremill/sttp/Response.scala | 6 +++++- core/src/main/scala/com/softwaremill/sttp/SttpHandler.scala | 8 +++++++- 3 files changed, 13 insertions(+), 3 deletions(-) (limited to 'core/src') diff --git a/core/src/main/scala/com/softwaremill/sttp/HttpURLConnectionHandler.scala b/core/src/main/scala/com/softwaremill/sttp/HttpURLConnectionHandler.scala index ddc1293..24c93f4 100644 --- a/core/src/main/scala/com/softwaremill/sttp/HttpURLConnectionHandler.scala +++ b/core/src/main/scala/com/softwaremill/sttp/HttpURLConnectionHandler.scala @@ -199,7 +199,7 @@ object HttpURLConnectionHandler extends SttpHandler[Id, Nothing] { Left(readResponseBody(wrappedIs, asString)) } - Response(body, code, headers) + Response(body, code, headers, Nil) } private def readResponseBody[T](is: InputStream, diff --git a/core/src/main/scala/com/softwaremill/sttp/Response.scala b/core/src/main/scala/com/softwaremill/sttp/Response.scala index 5a3cfe3..7551ee0 100644 --- a/core/src/main/scala/com/softwaremill/sttp/Response.scala +++ b/core/src/main/scala/com/softwaremill/sttp/Response.scala @@ -21,10 +21,14 @@ import scala.util.Try * `Left(String)`, if the request wasn't successful (status code * 3xx, 4xx or 5xx). In this case, the response body is read into * a `String`. + * @param history If redirects are followed, and there were redirects, + * contains responses for the intermediate requests. + * The first response (oldest) comes first. */ case class Response[T](body: Either[String, T], code: Int, - headers: Seq[(String, String)]) { + headers: Seq[(String, String)], + history: List[Response[Unit]]) { def is200: Boolean = code == 200 def isSuccess: Boolean = codeIsSuccess(code) def isRedirect: Boolean = code >= 300 && code < 400 diff --git a/core/src/main/scala/com/softwaremill/sttp/SttpHandler.scala b/core/src/main/scala/com/softwaremill/sttp/SttpHandler.scala index 26464ba..2f44840 100644 --- a/core/src/main/scala/com/softwaremill/sttp/SttpHandler.scala +++ b/core/src/main/scala/com/softwaremill/sttp/SttpHandler.scala @@ -38,7 +38,13 @@ trait SttpHandler[R[_], -S] { uri"$loc" } - send(request.copy[Id, T, S](uri = uri)) + val redirectResponse = send(request.copy[Id, T, S](uri = uri)) + + responseMonad.map(redirectResponse) { rr => + val responseNoBody = + response.copy(body = response.body.right.map(_ => ())) + rr.copy(history = responseNoBody :: rr.history) + } } } -- cgit v1.2.3