diff options
Diffstat (limited to 'async-http-client-backend/monix')
-rw-r--r-- | async-http-client-backend/monix/src/main/scala/com/softwaremill/sttp/asynchttpclient/monix/AsyncHttpClientMonixBackend.scala | 65 |
1 files changed, 19 insertions, 46 deletions
diff --git a/async-http-client-backend/monix/src/main/scala/com/softwaremill/sttp/asynchttpclient/monix/AsyncHttpClientMonixBackend.scala b/async-http-client-backend/monix/src/main/scala/com/softwaremill/sttp/asynchttpclient/monix/AsyncHttpClientMonixBackend.scala index 04df4ed..03d4d09 100644 --- a/async-http-client-backend/monix/src/main/scala/com/softwaremill/sttp/asynchttpclient/monix/AsyncHttpClientMonixBackend.scala +++ b/async-http-client-backend/monix/src/main/scala/com/softwaremill/sttp/asynchttpclient/monix/AsyncHttpClientMonixBackend.scala @@ -2,45 +2,27 @@ package com.softwaremill.sttp.asynchttpclient.monix import java.nio.ByteBuffer -import com.softwaremill.sttp.{ - FollowRedirectsBackend, - MonadAsyncError, - SttpBackend, - SttpBackendOptions, - Utf8, - concatByteBuffers -} import com.softwaremill.sttp.asynchttpclient.AsyncHttpClientBackend +import com.softwaremill.sttp.{FollowRedirectsBackend, MonadAsyncError, SttpBackend, SttpBackendOptions, Utf8, concatByteBuffers} import monix.eval.Task import monix.execution.{Cancelable, Scheduler} import monix.reactive.Observable -import org.asynchttpclient.{ - AsyncHttpClient, - AsyncHttpClientConfig, - DefaultAsyncHttpClient -} +import org.asynchttpclient.{AsyncHttpClient, AsyncHttpClientConfig, DefaultAsyncHttpClient} import org.reactivestreams.Publisher import scala.util.{Failure, Success} -class AsyncHttpClientMonixBackend private ( - asyncHttpClient: AsyncHttpClient, - closeClient: Boolean)(implicit scheduler: Scheduler) - extends AsyncHttpClientBackend[Task, Observable[ByteBuffer]]( - asyncHttpClient, - TaskMonad, - closeClient) { +class AsyncHttpClientMonixBackend private (asyncHttpClient: AsyncHttpClient, closeClient: Boolean)( + implicit scheduler: Scheduler) + extends AsyncHttpClientBackend[Task, Observable[ByteBuffer]](asyncHttpClient, TaskMonad, closeClient) { - override protected def streamBodyToPublisher( - s: Observable[ByteBuffer]): Publisher[ByteBuffer] = + override protected def streamBodyToPublisher(s: Observable[ByteBuffer]): Publisher[ByteBuffer] = s.toReactivePublisher - override protected def publisherToStreamBody( - p: Publisher[ByteBuffer]): Observable[ByteBuffer] = + override protected def publisherToStreamBody(p: Publisher[ByteBuffer]): Observable[ByteBuffer] = Observable.fromReactivePublisher(p) - override protected def publisherToString( - p: Publisher[ByteBuffer]): Task[String] = { + override protected def publisherToString(p: Publisher[ByteBuffer]): Task[String] = { val bytes = Observable .fromReactivePublisher(p) @@ -53,38 +35,31 @@ class AsyncHttpClientMonixBackend private ( object AsyncHttpClientMonixBackend { private def apply(asyncHttpClient: AsyncHttpClient, closeClient: Boolean)( - implicit scheduler: Scheduler) - : SttpBackend[Task, Observable[ByteBuffer]] = - new FollowRedirectsBackend( - new AsyncHttpClientMonixBackend(asyncHttpClient, closeClient)) + implicit scheduler: Scheduler): SttpBackend[Task, Observable[ByteBuffer]] = + new FollowRedirectsBackend(new AsyncHttpClientMonixBackend(asyncHttpClient, closeClient)) /** * @param s The scheduler used for streaming request bodies. Defaults to the * global scheduler. */ def apply(options: SttpBackendOptions = SttpBackendOptions.Default)( - implicit s: Scheduler = Scheduler.Implicits.global) - : SttpBackend[Task, Observable[ByteBuffer]] = - AsyncHttpClientMonixBackend(AsyncHttpClientBackend.defaultClient(options), - closeClient = true) + implicit s: Scheduler = Scheduler.Implicits.global): SttpBackend[Task, Observable[ByteBuffer]] = + AsyncHttpClientMonixBackend(AsyncHttpClientBackend.defaultClient(options), closeClient = true) /** * @param s The scheduler used for streaming request bodies. Defaults to the * global scheduler. */ - def usingConfig(cfg: AsyncHttpClientConfig)(implicit s: Scheduler = - Scheduler.Implicits.global) - : SttpBackend[Task, Observable[ByteBuffer]] = - AsyncHttpClientMonixBackend(new DefaultAsyncHttpClient(cfg), - closeClient = true) + def usingConfig(cfg: AsyncHttpClientConfig)( + implicit s: Scheduler = Scheduler.Implicits.global): SttpBackend[Task, Observable[ByteBuffer]] = + AsyncHttpClientMonixBackend(new DefaultAsyncHttpClient(cfg), closeClient = true) /** * @param s The scheduler used for streaming request bodies. Defaults to the * global scheduler. */ - def usingClient(client: AsyncHttpClient)(implicit s: Scheduler = - Scheduler.Implicits.global) - : SttpBackend[Task, Observable[ByteBuffer]] = + def usingClient(client: AsyncHttpClient)( + implicit s: Scheduler = Scheduler.Implicits.global): SttpBackend[Task, Observable[ByteBuffer]] = AsyncHttpClientMonixBackend(client, closeClient = false) } @@ -96,8 +71,7 @@ private[monix] object TaskMonad extends MonadAsyncError[Task] { override def flatMap[T, T2](fa: Task[T])(f: (T) => Task[T2]): Task[T2] = fa.flatMap(f) - override def async[T]( - register: ((Either[Throwable, T]) => Unit) => Unit): Task[T] = + override def async[T](register: ((Either[Throwable, T]) => Unit) => Unit): Task[T] = Task.async { (_, cb) => register { case Left(t) => cb(Failure(t)) @@ -109,7 +83,6 @@ private[monix] object TaskMonad extends MonadAsyncError[Task] { override def error[T](t: Throwable): Task[T] = Task.raiseError(t) - override protected def handleWrappedError[T](rt: Task[T])( - h: PartialFunction[Throwable, Task[T]]): Task[T] = + override protected def handleWrappedError[T](rt: Task[T])(h: PartialFunction[Throwable, Task[T]]): Task[T] = rt.onErrorRecoverWith(h) } |