diff options
Diffstat (limited to 'async-http-client-handler/monix/src/main')
-rw-r--r-- | async-http-client-handler/monix/src/main/scala/com/softwaremill/sttp/asynchttpclient/monix/MonixAsyncHttpClientHandler.scala | 35 |
1 files changed, 35 insertions, 0 deletions
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 new file mode 100644 index 0000000..ddfacb5 --- /dev/null +++ b/async-http-client-handler/monix/src/main/scala/com/softwaremill/sttp/asynchttpclient/monix/MonixAsyncHttpClientHandler.scala @@ -0,0 +1,35 @@ +package com.softwaremill.sttp.asynchttpclient.monix + +import com.softwaremill.sttp.asynchttpclient.internal.{ + AsyncHttpClientHandler, + WrapperFromAsync +} +import monix.eval.Task +import monix.execution.Cancelable +import org.asynchttpclient.{ + AsyncHttpClient, + AsyncHttpClientConfig, + DefaultAsyncHttpClient +} + +import scala.util.{Failure, Success} + +class MonixAsyncHttpClientHandler(asyncHttpClient: AsyncHttpClient) + extends AsyncHttpClientHandler[Task](asyncHttpClient, TaskFromAsync) { + + def this() = this(new DefaultAsyncHttpClient()) + def this(cfg: AsyncHttpClientConfig) = this(new DefaultAsyncHttpClient(cfg)) +} + +private[asynchttpclient] object TaskFromAsync extends WrapperFromAsync[Task] { + override def apply[T]( + register: ((Either[Throwable, T]) => Unit) => Unit): Task[T] = + Task.async { (_, cb) => + register { + case Left(t) => cb(Failure(t)) + case Right(t) => cb(Success(t)) + } + + Cancelable.empty + } +} |