diff options
author | adamw <adam@warski.org> | 2017-07-24 12:18:27 +0200 |
---|---|---|
committer | adamw <adam@warski.org> | 2017-07-24 12:18:27 +0200 |
commit | ccd2c4b1d53bf68e04ff1f8bca032d870494d9a8 (patch) | |
tree | e298b14664b07dc9aab54f74abe956fb797fe1bb /async-http-client-handler/future/src | |
parent | fef16dd2dbd0f53ee7432ab2ff39255279932ac4 (diff) | |
download | sttp-ccd2c4b1d53bf68e04ff1f8bca032d870494d9a8.tar.gz sttp-ccd2c4b1d53bf68e04ff1f8bca032d870494d9a8.tar.bz2 sttp-ccd2c4b1d53bf68e04ff1f8bca032d870494d9a8.zip |
Better responseAs mapping, done on the client thread pool
Diffstat (limited to 'async-http-client-handler/future/src')
-rw-r--r-- | async-http-client-handler/future/src/main/scala/com/softwaremill/sttp/asynchttpclient/future/FutureAsyncHttpClientHandler.scala | 25 |
1 files changed, 18 insertions, 7 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 59119d7..41fcf68 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,7 +2,7 @@ package com.softwaremill.sttp.asynchttpclient.future import com.softwaremill.sttp.asynchttpclient.{ AsyncHttpClientHandler, - WrapperFromAsync + MonadAsyncError } import org.asynchttpclient.{ AsyncHttpClient, @@ -10,18 +10,27 @@ import org.asynchttpclient.{ DefaultAsyncHttpClient } -import scala.concurrent.{Future, Promise} +import scala.concurrent.{ExecutionContext, Future, Promise} -class FutureAsyncHttpClientHandler(asyncHttpClient: AsyncHttpClient) - extends AsyncHttpClientHandler[Future](asyncHttpClient, FutureFromAsync) { +class FutureAsyncHttpClientHandler(asyncHttpClient: AsyncHttpClient)( + implicit ec: ExecutionContext = ExecutionContext.Implicits.global) + extends AsyncHttpClientHandler[Future](asyncHttpClient, new FutureMonad()) { def this() = this(new DefaultAsyncHttpClient()) def this(cfg: AsyncHttpClientConfig) = this(new DefaultAsyncHttpClient(cfg)) } -private[asynchttpclient] object FutureFromAsync - extends WrapperFromAsync[Future] { - override def apply[T]( +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 { @@ -30,4 +39,6 @@ private[asynchttpclient] object FutureFromAsync } p.future } + + override def error[T](t: Throwable): Future[T] = Future.failed(t) } |