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/future | |
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/future')
-rw-r--r-- | async-http-client-backend/future/src/main/scala/com/softwaremill/sttp/asynchttpclient/future/AsyncHttpClientFutureBackend.scala | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/async-http-client-backend/future/src/main/scala/com/softwaremill/sttp/asynchttpclient/future/AsyncHttpClientFutureBackend.scala b/async-http-client-backend/future/src/main/scala/com/softwaremill/sttp/asynchttpclient/future/AsyncHttpClientFutureBackend.scala new file mode 100644 index 0000000..a46ed0d --- /dev/null +++ b/async-http-client-backend/future/src/main/scala/com/softwaremill/sttp/asynchttpclient/future/AsyncHttpClientFutureBackend.scala @@ -0,0 +1,76 @@ +package com.softwaremill.sttp.asynchttpclient.future + +import java.nio.ByteBuffer + +import com.softwaremill.sttp.asynchttpclient.AsyncHttpClientBackend +import com.softwaremill.sttp.{FollowRedirectsBackend, FutureMonad, SttpBackend} +import org.asynchttpclient.{ + AsyncHttpClient, + AsyncHttpClientConfig, + DefaultAsyncHttpClient +} +import org.reactivestreams.Publisher + +import scala.concurrent.duration.FiniteDuration +import scala.concurrent.{ExecutionContext, Future} + +class AsyncHttpClientFutureBackend private ( + asyncHttpClient: AsyncHttpClient, + closeClient: Boolean)(implicit ec: ExecutionContext) + extends AsyncHttpClientBackend[Future, Nothing](asyncHttpClient, + new FutureMonad, + 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]): Future[String] = + throw new IllegalStateException("This backend does not support streaming") +} + +object AsyncHttpClientFutureBackend { + + private def apply(asyncHttpClient: AsyncHttpClient, closeClient: Boolean)( + implicit ec: ExecutionContext): SttpBackend[Future, Nothing] = + new FollowRedirectsBackend[Future, Nothing]( + new AsyncHttpClientFutureBackend(asyncHttpClient, closeClient)) + + /** + * @param ec The execution context for running non-network related operations, + * e.g. mapping responses. Defaults to the global execution + * context. + */ + def apply(connectionTimeout: FiniteDuration = + SttpBackend.DefaultConnectionTimeout)( + implicit ec: ExecutionContext = ExecutionContext.Implicits.global) + : SttpBackend[Future, Nothing] = + AsyncHttpClientFutureBackend( + AsyncHttpClientBackend.defaultClient(connectionTimeout.toMillis.toInt), + closeClient = true) + + /** + * @param ec The execution context for running non-network related operations, + * e.g. mapping responses. Defaults to the global execution + * context. + */ + def usingConfig(cfg: AsyncHttpClientConfig)( + implicit ec: ExecutionContext = ExecutionContext.Implicits.global) + : SttpBackend[Future, Nothing] = + AsyncHttpClientFutureBackend(new DefaultAsyncHttpClient(cfg), + closeClient = true) + + /** + * @param ec The execution context for running non-network related operations, + * e.g. mapping responses. Defaults to the global execution + * context. + */ + def usingClient(client: AsyncHttpClient)(implicit ec: ExecutionContext = + ExecutionContext.Implicits.global) + : SttpBackend[Future, Nothing] = + AsyncHttpClientFutureBackend(client, closeClient = false) +} |