diff options
author | adamw <adam@warski.org> | 2017-09-14 11:03:21 +0100 |
---|---|---|
committer | adamw <adam@warski.org> | 2017-09-14 11:03:21 +0100 |
commit | fbc71ee712635ed64c50ca694735a84ec794eb11 (patch) | |
tree | bf1dd7335306b7f320262d45d0d5b6d02f5a0b27 /async-http-client-backend/scalaz/src | |
parent | a971d409cb1063a2089d936abf3d3ab70bbbabb6 (diff) | |
download | sttp-fbc71ee712635ed64c50ca694735a84ec794eb11.tar.gz sttp-fbc71ee712635ed64c50ca694735a84ec794eb11.tar.bz2 sttp-fbc71ee712635ed64c50ca694735a84ec794eb11.zip |
Renaming "handler" to "backend"
Diffstat (limited to 'async-http-client-backend/scalaz/src')
-rw-r--r-- | async-http-client-backend/scalaz/src/main/scala/com/softwaremill/sttp/asynchttpclient/scalaz/AsyncHttpClientScalazBackend.scala | 79 |
1 files changed, 79 insertions, 0 deletions
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 new file mode 100644 index 0000000..12e217b --- /dev/null +++ b/async-http-client-backend/scalaz/src/main/scala/com/softwaremill/sttp/asynchttpclient/scalaz/AsyncHttpClientScalazBackend.scala @@ -0,0 +1,79 @@ +package com.softwaremill.sttp.asynchttpclient.scalaz + +import java.nio.ByteBuffer + +import com.softwaremill.sttp.{ + FollowRedirectsBackend, + MonadAsyncError, + SttpBackend +} +import com.softwaremill.sttp.asynchttpclient.AsyncHttpClientBackend +import org.asynchttpclient.{ + AsyncHttpClient, + AsyncHttpClientConfig, + DefaultAsyncHttpClient +} +import org.reactivestreams.Publisher + +import scala.concurrent.duration.FiniteDuration +import scalaz.{-\/, \/-} +import scalaz.concurrent.Task + +class AsyncHttpClientScalazBackend private (asyncHttpClient: AsyncHttpClient, + closeClient: Boolean) + extends AsyncHttpClientBackend[Task, Nothing](asyncHttpClient, + TaskMonad, + 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]): Task[String] = + throw new IllegalStateException("This backend does not support streaming") +} + +object AsyncHttpClientScalazBackend { + private def apply(asyncHttpClient: AsyncHttpClient, + closeClient: Boolean): SttpBackend[Task, Nothing] = + new FollowRedirectsBackend[Task, Nothing]( + new AsyncHttpClientScalazBackend(asyncHttpClient, closeClient)) + + def apply( + connectionTimeout: FiniteDuration = SttpBackend.DefaultConnectionTimeout) + : SttpBackend[Task, Nothing] = + AsyncHttpClientScalazBackend( + AsyncHttpClientBackend.defaultClient(connectionTimeout.toMillis.toInt), + closeClient = true) + + def usingConfig(cfg: AsyncHttpClientConfig): SttpBackend[Task, Nothing] = + AsyncHttpClientScalazBackend(new DefaultAsyncHttpClient(cfg), + closeClient = true) + + 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) +} |