aboutsummaryrefslogtreecommitdiff
path: root/async-http-client-backend/cats
diff options
context:
space:
mode:
authoradamw <adam@warski.org>2017-09-14 11:03:21 +0100
committeradamw <adam@warski.org>2017-09-14 11:03:21 +0100
commitfbc71ee712635ed64c50ca694735a84ec794eb11 (patch)
treebf1dd7335306b7f320262d45d0d5b6d02f5a0b27 /async-http-client-backend/cats
parenta971d409cb1063a2089d936abf3d3ab70bbbabb6 (diff)
downloadsttp-fbc71ee712635ed64c50ca694735a84ec794eb11.tar.gz
sttp-fbc71ee712635ed64c50ca694735a84ec794eb11.tar.bz2
sttp-fbc71ee712635ed64c50ca694735a84ec794eb11.zip
Renaming "handler" to "backend"
Diffstat (limited to 'async-http-client-backend/cats')
-rw-r--r--async-http-client-backend/cats/src/main/scala/com/softwaremill/sttp/asynchttpclient/cats/AsyncHttpClientCatsBackend.scala82
1 files changed, 82 insertions, 0 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
new file mode 100644
index 0000000..b5beb75
--- /dev/null
+++ b/async-http-client-backend/cats/src/main/scala/com/softwaremill/sttp/asynchttpclient/cats/AsyncHttpClientCatsBackend.scala
@@ -0,0 +1,82 @@
+package com.softwaremill.sttp.asynchttpclient.cats
+
+import java.nio.ByteBuffer
+
+import cats.effect._
+import com.softwaremill.sttp.asynchttpclient.AsyncHttpClientBackend
+import com.softwaremill.sttp.{
+ FollowRedirectsBackend,
+ MonadAsyncError,
+ SttpBackend
+}
+import org.asynchttpclient.{
+ AsyncHttpClient,
+ AsyncHttpClientConfig,
+ DefaultAsyncHttpClient
+}
+import org.reactivestreams.Publisher
+
+import scala.concurrent.duration.FiniteDuration
+import scala.language.higherKinds
+
+class AsyncHttpClientCatsBackend[F[_]: Async] private (
+ asyncHttpClient: AsyncHttpClient,
+ closeClient: Boolean
+) extends AsyncHttpClientBackend[F, Nothing](
+ asyncHttpClient,
+ new AsyncMonad,
+ closeClient
+ ) {
+ override protected def streamBodyToPublisher(
+ s: Nothing): Publisher[ByteBuffer] = s // nothing is everything
+
+ override protected def publisherToStreamBody(
+ p: Publisher[ByteBuffer]): Nothing =
+ throw new IllegalStateException("This backend does not support streaming")
+
+ override protected def publisherToString(
+ p: Publisher[ByteBuffer]): F[String] =
+ throw new IllegalStateException("This backend does not support streaming")
+}
+
+object AsyncHttpClientCatsBackend {
+
+ private def apply[F[_]: Async](
+ asyncHttpClient: AsyncHttpClient,
+ closeClient: Boolean): SttpBackend[F, Nothing] =
+ new FollowRedirectsBackend[F, Nothing](
+ new AsyncHttpClientCatsBackend(asyncHttpClient, closeClient))
+
+ def apply[F[_]: Async](
+ connectionTimeout: FiniteDuration = SttpBackend.DefaultConnectionTimeout)
+ : SttpBackend[F, Nothing] =
+ AsyncHttpClientCatsBackend(
+ AsyncHttpClientBackend.defaultClient(connectionTimeout.toMillis.toInt),
+ closeClient = true)
+
+ def usingConfig[F[_]: Async](
+ cfg: AsyncHttpClientConfig): SttpBackend[F, Nothing] =
+ AsyncHttpClientCatsBackend(new DefaultAsyncHttpClient(cfg),
+ closeClient = true)
+
+ 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)
+}