diff options
author | adamw <adam@warski.org> | 2017-08-03 11:38:23 +0200 |
---|---|---|
committer | adamw <adam@warski.org> | 2017-08-03 11:38:23 +0200 |
commit | c07f98349ac5dc646855d00425a9dc2c3324465e (patch) | |
tree | 6f07784a68290b406a3b50a59fac36b83c5df355 /async-http-client-handler | |
parent | 90615deb20ce43f09371b10a25628be8d68485d8 (diff) | |
download | sttp-c07f98349ac5dc646855d00425a9dc2c3324465e.tar.gz sttp-c07f98349ac5dc646855d00425a9dc2c3324465e.tar.bz2 sttp-c07f98349ac5dc646855d00425a9dc2c3324465e.zip |
Making the response monad a top-level concept, to make it possible to write SttpHandler wrappers.
Diffstat (limited to 'async-http-client-handler')
4 files changed, 14 insertions, 53 deletions
diff --git a/async-http-client-handler/future/src/main/scala/com/softwaremill/sttp/asynchttpclient/future/FutureAsyncHttpClientHandler.scala b/async-http-client-handler/future/src/main/scala/com/softwaremill/sttp/asynchttpclient/future/FutureAsyncHttpClientHandler.scala index 5d71511..3209e9b 100644 --- a/async-http-client-handler/future/src/main/scala/com/softwaremill/sttp/asynchttpclient/future/FutureAsyncHttpClientHandler.scala +++ b/async-http-client-handler/future/src/main/scala/com/softwaremill/sttp/asynchttpclient/future/FutureAsyncHttpClientHandler.scala @@ -2,11 +2,8 @@ package com.softwaremill.sttp.asynchttpclient.future import java.nio.ByteBuffer -import com.softwaremill.sttp.SttpHandler -import com.softwaremill.sttp.asynchttpclient.{ - AsyncHttpClientHandler, - MonadAsyncError -} +import com.softwaremill.sttp.asynchttpclient.AsyncHttpClientHandler +import com.softwaremill.sttp.{FutureMonad, SttpHandler} import org.asynchttpclient.{ AsyncHttpClient, AsyncHttpClientConfig, @@ -14,13 +11,13 @@ import org.asynchttpclient.{ } import org.reactivestreams.Publisher -import scala.concurrent.{ExecutionContext, Future, Promise} +import scala.concurrent.{ExecutionContext, Future} class FutureAsyncHttpClientHandler private ( asyncHttpClient: AsyncHttpClient, closeClient: Boolean)(implicit ec: ExecutionContext) extends AsyncHttpClientHandler[Future, Nothing](asyncHttpClient, - new FutureMonad(), + new FutureMonad, closeClient) { override protected def streamBodyToPublisher( @@ -64,25 +61,3 @@ object FutureAsyncHttpClientHandler { : SttpHandler[Future, Nothing] = new FutureAsyncHttpClientHandler(client, closeClient = false) } - -private[future] class FutureMonad(implicit ec: ExecutionContext) - extends MonadAsyncError[Future] { - override def unit[T](t: T): Future[T] = Future.successful(t) - - override def map[T, T2](fa: Future[T], f: (T) => T2): Future[T2] = fa.map(f) - - override def flatMap[T, T2](fa: Future[T], f: (T) => Future[T2]): Future[T2] = - fa.flatMap(f) - - override def async[T]( - register: ((Either[Throwable, T]) => Unit) => Unit): Future[T] = { - val p = Promise[T]() - register { - case Left(t) => p.failure(t) - case Right(t) => p.success(t) - } - p.future - } - - override def error[T](t: Throwable): Future[T] = Future.failed(t) -} diff --git a/async-http-client-handler/monix/src/main/scala/com/softwaremill/sttp/asynchttpclient/monix/MonixAsyncHttpClientHandler.scala b/async-http-client-handler/monix/src/main/scala/com/softwaremill/sttp/asynchttpclient/monix/MonixAsyncHttpClientHandler.scala index 2357c06..2407c19 100644 --- a/async-http-client-handler/monix/src/main/scala/com/softwaremill/sttp/asynchttpclient/monix/MonixAsyncHttpClientHandler.scala +++ b/async-http-client-handler/monix/src/main/scala/com/softwaremill/sttp/asynchttpclient/monix/MonixAsyncHttpClientHandler.scala @@ -2,11 +2,8 @@ package com.softwaremill.sttp.asynchttpclient.monix import java.nio.ByteBuffer -import com.softwaremill.sttp.SttpHandler -import com.softwaremill.sttp.asynchttpclient.{ - AsyncHttpClientHandler, - MonadAsyncError -} +import com.softwaremill.sttp.{MonadAsyncError, SttpHandler} +import com.softwaremill.sttp.asynchttpclient.AsyncHttpClientHandler import monix.eval.Task import monix.execution.{Cancelable, Scheduler} import monix.reactive.Observable diff --git a/async-http-client-handler/scalaz/src/main/scala/com/softwaremill/sttp/asynchttpclient/scalaz/ScalazAsyncHttpClientHandler.scala b/async-http-client-handler/scalaz/src/main/scala/com/softwaremill/sttp/asynchttpclient/scalaz/ScalazAsyncHttpClientHandler.scala index ef06a41..01c3cdb 100644 --- a/async-http-client-handler/scalaz/src/main/scala/com/softwaremill/sttp/asynchttpclient/scalaz/ScalazAsyncHttpClientHandler.scala +++ b/async-http-client-handler/scalaz/src/main/scala/com/softwaremill/sttp/asynchttpclient/scalaz/ScalazAsyncHttpClientHandler.scala @@ -2,11 +2,8 @@ package com.softwaremill.sttp.asynchttpclient.scalaz import java.nio.ByteBuffer -import com.softwaremill.sttp.SttpHandler -import com.softwaremill.sttp.asynchttpclient.{ - AsyncHttpClientHandler, - MonadAsyncError -} +import com.softwaremill.sttp.{MonadAsyncError, SttpHandler} +import com.softwaremill.sttp.asynchttpclient.AsyncHttpClientHandler import org.asynchttpclient.{ AsyncHttpClient, AsyncHttpClientConfig, 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 4824b39..f5b4569 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 @@ -5,10 +5,12 @@ import java.nio.charset.Charset import com.softwaremill.sttp.model._ import com.softwaremill.sttp.{ + ContentLengthHeader, + MonadAsyncError, + MonadError, Request, Response, - SttpHandler, - ContentLengthHeader + SttpHandler } import org.asynchttpclient.AsyncHandler.State import org.asynchttpclient.handler.StreamedAsyncHandler @@ -50,11 +52,11 @@ abstract class AsyncHttpClientHandler[R[_], S](asyncHttpClient: AsyncHttpClient, preparedRequest .execute(eagerAsyncHandler(ra, success, error)) } - }) - } + override def responseMonad: MonadError[R] = rm + protected def streamBodyToPublisher(s: S): Publisher[ByteBuffer] protected def publisherToStreamBody(p: Publisher[ByteBuffer]): S @@ -234,16 +236,6 @@ abstract class AsyncHttpClientHandler[R[_], S](asyncHttpClient: AsyncHttpClient, } } -trait MonadAsyncError[R[_]] { - def unit[T](t: T): R[T] - def map[T, T2](fa: R[T], f: T => T2): R[T2] - def flatMap[T, T2](fa: R[T], f: T => R[T2]): R[T2] - def async[T](register: (Either[Throwable, T] => Unit) => Unit): R[T] - def error[T](t: Throwable): R[T] - - def flatten[T](ffa: R[R[T]]): R[T] = flatMap[R[T], T](ffa, identity) -} - object EmptyPublisher extends Publisher[ByteBuffer] { override def subscribe(s: Subscriber[_ >: ByteBuffer]): Unit = { s.onComplete() |