diff options
author | Sam Guymer <sam@guymer.me> | 2018-05-17 20:07:04 +1000 |
---|---|---|
committer | Sam Guymer <sam@guymer.me> | 2018-05-18 22:28:28 +1000 |
commit | 40288a1aaddfc27e141771371d69122ce222a8d0 (patch) | |
tree | 14177a51769ce42e88ec3d20435abe505b44aac5 /async-http-client-backend | |
parent | 96ff655f906f2e3f4e9ba906c42e96506f4668b9 (diff) | |
download | sttp-40288a1aaddfc27e141771371d69122ce222a8d0.tar.gz sttp-40288a1aaddfc27e141771371d69122ce222a8d0.tar.bz2 sttp-40288a1aaddfc27e141771371d69122ce222a8d0.zip |
Extract MonadAsyncError implementations
Extract MonadAsyncError implementations into their own projects to allow
reuse by multiple backends.
Diffstat (limited to 'async-http-client-backend')
4 files changed, 12 insertions, 82 deletions
diff --git a/async-http-client-backend/cats/src/main/scala/com/softwaremill/sttp/asynchttpclient/cats/AsyncHttpClientCatsBackend.scala b/async-http-client-backend/cats/src/main/scala/com/softwaremill/sttp/asynchttpclient/cats/AsyncHttpClientCatsBackend.scala index 506ad42..0e5955a 100644 --- a/async-http-client-backend/cats/src/main/scala/com/softwaremill/sttp/asynchttpclient/cats/AsyncHttpClientCatsBackend.scala +++ b/async-http-client-backend/cats/src/main/scala/com/softwaremill/sttp/asynchttpclient/cats/AsyncHttpClientCatsBackend.scala @@ -2,9 +2,10 @@ package com.softwaremill.sttp.asynchttpclient.cats import java.nio.ByteBuffer -import cats.effect._ +import cats.effect.Async import com.softwaremill.sttp.asynchttpclient.AsyncHttpClientBackend -import com.softwaremill.sttp.{FollowRedirectsBackend, MonadAsyncError, SttpBackend, SttpBackendOptions} +import com.softwaremill.sttp.impl.cats.AsyncMonadAsyncError +import com.softwaremill.sttp.{FollowRedirectsBackend, SttpBackend, SttpBackendOptions} import io.netty.buffer.ByteBuf import org.asynchttpclient.{AsyncHttpClient, AsyncHttpClientConfig, DefaultAsyncHttpClient} import org.reactivestreams.Publisher @@ -16,7 +17,7 @@ class AsyncHttpClientCatsBackend[F[_]: Async] private ( closeClient: Boolean ) extends AsyncHttpClientBackend[F, Nothing]( asyncHttpClient, - new AsyncMonad, + new AsyncMonadAsyncError, closeClient ) { override protected def streamBodyToPublisher(s: Nothing): Publisher[ByteBuf] = @@ -43,21 +44,3 @@ object AsyncHttpClientCatsBackend { def usingClient[F[_]: Async](client: AsyncHttpClient): SttpBackend[F, Nothing] = AsyncHttpClientCatsBackend(client, closeClient = false) } - -private[cats] class AsyncMonad[F[_]](implicit F: Async[F]) extends MonadAsyncError[F] { - - override def async[T](register: ((Either[Throwable, T]) => Unit) => Unit): F[T] = - F.async(register) - - override def unit[T](t: T): F[T] = F.pure(t) - - override def map[T, T2](fa: F[T])(f: (T) => T2): F[T2] = F.map(fa)(f) - - override def flatMap[T, T2](fa: F[T])(f: (T) => F[T2]): F[T2] = - F.flatMap(fa)(f) - - override def error[T](t: Throwable): F[T] = F.raiseError(t) - - override protected def handleWrappedError[T](rt: F[T])(h: PartialFunction[Throwable, F[T]]): F[T] = - F.recoverWith(rt)(h) -} diff --git a/async-http-client-backend/fs2/src/main/scala/com/softwaremill/sttp/asynchttpclient/fs2/AsyncHttpClientFs2Backend.scala b/async-http-client-backend/fs2/src/main/scala/com/softwaremill/sttp/asynchttpclient/fs2/AsyncHttpClientFs2Backend.scala index 35366c1..50d9057 100644 --- a/async-http-client-backend/fs2/src/main/scala/com/softwaremill/sttp/asynchttpclient/fs2/AsyncHttpClientFs2Backend.scala +++ b/async-http-client-backend/fs2/src/main/scala/com/softwaremill/sttp/asynchttpclient/fs2/AsyncHttpClientFs2Backend.scala @@ -79,6 +79,7 @@ object AsyncHttpClientFs2Backend { AsyncHttpClientFs2Backend[F](client, closeClient = false) } +// TODO replace with EffectMonadAsyncError when cats-effect versions are bin compat private[fs2] class EffectMonad[F[_]](implicit F: Effect[F]) extends MonadAsyncError[F] { override def async[T](register: ((Either[Throwable, T]) => Unit) => Unit): F[T] = 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 915b325..a181517 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 @@ -3,26 +3,18 @@ package com.softwaremill.sttp.asynchttpclient.monix import java.nio.ByteBuffer import com.softwaremill.sttp.asynchttpclient.AsyncHttpClientBackend -import com.softwaremill.sttp.{ - FollowRedirectsBackend, - MonadAsyncError, - SttpBackend, - SttpBackendOptions, - Utf8, - concatByteBuffers -} +import com.softwaremill.sttp.impl.monix.TaskMonadAsyncError +import com.softwaremill.sttp.{FollowRedirectsBackend, SttpBackend, SttpBackendOptions, Utf8, concatByteBuffers} import io.netty.buffer.{ByteBuf, Unpooled} import monix.eval.Task -import monix.execution.{Cancelable, Scheduler} +import monix.execution.Scheduler import monix.reactive.Observable 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) { + extends AsyncHttpClientBackend[Task, Observable[ByteBuffer]](asyncHttpClient, TaskMonadAsyncError, closeClient) { override protected def streamBodyToPublisher(s: Observable[ByteBuffer]): Publisher[ByteBuf] = s.map(Unpooled.wrappedBuffer).toReactivePublisher @@ -70,27 +62,3 @@ object AsyncHttpClientMonixBackend { implicit s: Scheduler = Scheduler.Implicits.global): SttpBackend[Task, Observable[ByteBuffer]] = AsyncHttpClientMonixBackend(client, closeClient = false) } - -private[monix] object TaskMonad extends MonadAsyncError[Task] { - override def unit[T](t: T): Task[T] = Task.now(t) - - override def map[T, T2](fa: Task[T])(f: (T) => T2): Task[T2] = fa.map(f) - - 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] = - Task.async { (_, cb) => - register { - case Left(t) => cb(Failure(t)) - case Right(t) => cb(Success(t)) - } - - Cancelable.empty - } - - 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] = - rt.onErrorRecoverWith(h) -} diff --git a/async-http-client-backend/scalaz/src/main/scala/com/softwaremill/sttp/asynchttpclient/scalaz/AsyncHttpClientScalazBackend.scala b/async-http-client-backend/scalaz/src/main/scala/com/softwaremill/sttp/asynchttpclient/scalaz/AsyncHttpClientScalazBackend.scala index 7c2343d..db8731e 100644 --- a/async-http-client-backend/scalaz/src/main/scala/com/softwaremill/sttp/asynchttpclient/scalaz/AsyncHttpClientScalazBackend.scala +++ b/async-http-client-backend/scalaz/src/main/scala/com/softwaremill/sttp/asynchttpclient/scalaz/AsyncHttpClientScalazBackend.scala @@ -3,16 +3,16 @@ package com.softwaremill.sttp.asynchttpclient.scalaz import java.nio.ByteBuffer import com.softwaremill.sttp.asynchttpclient.AsyncHttpClientBackend -import com.softwaremill.sttp.{FollowRedirectsBackend, MonadAsyncError, SttpBackend, SttpBackendOptions} +import com.softwaremill.sttp.impl.scalaz.TaskMonadAsyncError +import com.softwaremill.sttp.{FollowRedirectsBackend, SttpBackend, SttpBackendOptions} import io.netty.buffer.ByteBuf import org.asynchttpclient.{AsyncHttpClient, AsyncHttpClientConfig, DefaultAsyncHttpClient} import org.reactivestreams.Publisher import scalaz.concurrent.Task -import scalaz.{-\/, \/-} class AsyncHttpClientScalazBackend private (asyncHttpClient: AsyncHttpClient, closeClient: Boolean) - extends AsyncHttpClientBackend[Task, Nothing](asyncHttpClient, TaskMonad, closeClient) { + extends AsyncHttpClientBackend[Task, Nothing](asyncHttpClient, TaskMonadAsyncError, closeClient) { override protected def streamBodyToPublisher(s: Nothing): Publisher[ByteBuf] = s // nothing is everything @@ -37,25 +37,3 @@ object AsyncHttpClientScalazBackend { def usingClient(client: AsyncHttpClient): SttpBackend[Task, Nothing] = AsyncHttpClientScalazBackend(client, closeClient = false) } - -private[scalaz] object TaskMonad extends MonadAsyncError[Task] { - override def unit[T](t: T): Task[T] = Task.point(t) - - override def map[T, T2](fa: Task[T])(f: (T) => T2): Task[T2] = fa.map(f) - - 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] = - Task.async { cb => - register { - case Left(t) => cb(-\/(t)) - case Right(t) => cb(\/-(t)) - } - } - - override def error[T](t: Throwable): Task[T] = Task.fail(t) - - override protected def handleWrappedError[T](rt: Task[T])(h: PartialFunction[Throwable, Task[T]]): Task[T] = - rt.handleWith(h) -} |