diff options
author | adamw <adam@warski.org> | 2017-07-06 15:45:50 +0200 |
---|---|---|
committer | adamw <adam@warski.org> | 2017-07-06 15:45:50 +0200 |
commit | d1c5efa7a05875ef6a24946bc86e395cbc6cac30 (patch) | |
tree | 1d1d28b114861868448ec0f5bb6e7685dc099301 /core/src/main | |
parent | e4507b7f26c3091c4bd00c99f5320ff816f9bc6a (diff) | |
download | sttp-d1c5efa7a05875ef6a24946bc86e395cbc6cac30.tar.gz sttp-d1c5efa7a05875ef6a24946bc86e395cbc6cac30.tar.bz2 sttp-d1c5efa7a05875ef6a24946bc86e395cbc6cac30.zip |
Further simplifying requesting streaming responses
Diffstat (limited to 'core/src/main')
4 files changed, 18 insertions, 16 deletions
diff --git a/core/src/main/scala/com/softwaremill/sttp/HttpConnectionSttpHandler.scala b/core/src/main/scala/com/softwaremill/sttp/HttpConnectionSttpHandler.scala index 1ca2f77..8d14907 100644 --- a/core/src/main/scala/com/softwaremill/sttp/HttpConnectionSttpHandler.scala +++ b/core/src/main/scala/com/softwaremill/sttp/HttpConnectionSttpHandler.scala @@ -10,8 +10,8 @@ import com.softwaremill.sttp.model._ import scala.annotation.tailrec import scala.io.Source -object HttpConnectionSttpHandler extends SttpHandler[Id, Nothing, ResponseAsBasic] { - override def send[T](r: Request, responseAs: ResponseAsBasic[T, Nothing]): Response[T] = { +object HttpConnectionSttpHandler extends SttpHandler[Id, Nothing] { + override def send[T](r: Request, responseAs: ResponseAs[T, Nothing]): Response[T] = { val c = r.uri.toURL.openConnection().asInstanceOf[HttpURLConnection] c.setRequestMethod(r.method.m) r.headers.foreach { case (k, v) => c.setRequestProperty(k, v) } @@ -68,7 +68,7 @@ object HttpConnectionSttpHandler extends SttpHandler[Id, Nothing, ResponseAsBasi } } - private def readResponse[T](is: InputStream, responseAs: ResponseAsBasic[T, Nothing]): T = responseAs match { + private def readResponse[T](is: InputStream, responseAs: ResponseAs[T, Nothing]): T = responseAs match { case IgnoreResponse => @tailrec def consume(): Unit = if (is.read() != -1) consume() consume() @@ -93,5 +93,9 @@ object HttpConnectionSttpHandler extends SttpHandler[Id, Nothing, ResponseAsBasi transfer() os.toByteArray + + case ResponseAsStream() => + // only possible when the user requests the response as a stream of Nothing. Oh well ... + throw new IllegalStateException() } } diff --git a/core/src/main/scala/com/softwaremill/sttp/SttpHandler.scala b/core/src/main/scala/com/softwaremill/sttp/SttpHandler.scala index 500e889..a0a039e 100644 --- a/core/src/main/scala/com/softwaremill/sttp/SttpHandler.scala +++ b/core/src/main/scala/com/softwaremill/sttp/SttpHandler.scala @@ -4,6 +4,6 @@ import com.softwaremill.sttp.model.ResponseAs import scala.language.higherKinds -trait SttpHandler[R[_], -S, -AcceptsResponseAs[x, +s] <: ResponseAs[x, s]] { - def send[T](request: Request, responseAs: AcceptsResponseAs[T, S]): R[Response[T]] +trait SttpHandler[R[_], -S] { + def send[T](request: Request, responseAs: ResponseAs[T, S]): R[Response[T]] } diff --git a/core/src/main/scala/com/softwaremill/sttp/model/package.scala b/core/src/main/scala/com/softwaremill/sttp/model/package.scala index 784b72c..2b01e83 100644 --- a/core/src/main/scala/com/softwaremill/sttp/model/package.scala +++ b/core/src/main/scala/com/softwaremill/sttp/model/package.scala @@ -44,11 +44,9 @@ package object model { */ sealed trait ResponseAs[T, +S] - sealed trait ResponseAsBasic[T, +S] extends ResponseAs[T, S] - object IgnoreResponse extends ResponseAsBasic[Unit, Nothing] - case class ResponseAsString(encoding: String) extends ResponseAsBasic[String, Nothing] - object ResponseAsByteArray extends ResponseAsBasic[Array[Byte], Nothing] + object IgnoreResponse extends ResponseAs[Unit, Nothing] + case class ResponseAsString(encoding: String) extends ResponseAs[String, Nothing] + object ResponseAsByteArray extends ResponseAs[Array[Byte], Nothing] // response as params - case class ResponseAsStream[T, S]()(implicit val responseIsStream: S =:= T) extends ResponseAs[T, S] } diff --git a/core/src/main/scala/com/softwaremill/sttp/package.scala b/core/src/main/scala/com/softwaremill/sttp/package.scala index 8410685..1122f2e 100644 --- a/core/src/main/scala/com/softwaremill/sttp/package.scala +++ b/core/src/main/scala/com/softwaremill/sttp/package.scala @@ -15,13 +15,13 @@ package object sttp { type Id[X] = X type Empty[X] = None.type - def ignoreResponse: ResponseAsBasic[Unit, Nothing] = IgnoreResponse + def ignoreResponse: ResponseAs[Unit, Nothing] = IgnoreResponse /** * Uses `utf-8` encoding. */ - def responseAsString: ResponseAsBasic[String, Nothing] = responseAsString(Utf8) - def responseAsString(encoding: String): ResponseAsBasic[String, Nothing] = ResponseAsString(encoding) - def responseAsByteArray: ResponseAsBasic[Array[Byte], Nothing] = ResponseAsByteArray + def responseAsString: ResponseAs[String, Nothing] = responseAsString(Utf8) + def responseAsString(encoding: String): ResponseAs[String, Nothing] = ResponseAsString(encoding) + def responseAsByteArray: ResponseAs[Array[Byte], Nothing] = ResponseAsByteArray def responseAsStream[S]: ResponseAs[S, S] = ResponseAsStream[S, S]() /** @@ -150,8 +150,8 @@ package object sttp { * to consume it. An exception to this are streaming responses, which need to fully consumed * by the client if such a response type is requested. */ - def send[R[_], S, T, TypeOfResponseAs[x, +s] <: ResponseAs[x, s]](responseAs: TypeOfResponseAs[T, S])( - implicit handler: SttpHandler[R, S, TypeOfResponseAs], isRequest: IsRequest[U]): R[Response[T]] = { + def send[R[_], S, T](responseAs: ResponseAs[T, S])( + implicit handler: SttpHandler[R, S], isRequest: IsRequest[U]): R[Response[T]] = { handler.send(this, responseAs) } |