diff options
author | adamw <adam@warski.org> | 2018-02-20 12:24:10 +0100 |
---|---|---|
committer | adamw <adam@warski.org> | 2018-02-20 12:24:10 +0100 |
commit | 52017e118df6cf22b95f4c10b6abe4162801a1ef (patch) | |
tree | 4acefa1d7de60c212c39b98bd93242e3b3db2791 /async-http-client-backend | |
parent | e8b583709c9654c0ba16944a6242814d2355d4db (diff) | |
download | sttp-52017e118df6cf22b95f4c10b6abe4162801a1ef.tar.gz sttp-52017e118df6cf22b95f4c10b6abe4162801a1ef.tar.bz2 sttp-52017e118df6cf22b95f4c10b6abe4162801a1ef.zip |
New scalafmt settings
Diffstat (limited to 'async-http-client-backend')
6 files changed, 105 insertions, 250 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 471d26c..9d7ed88 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 @@ -4,17 +4,8 @@ import java.nio.ByteBuffer import cats.effect._ import com.softwaremill.sttp.asynchttpclient.AsyncHttpClientBackend -import com.softwaremill.sttp.{ - FollowRedirectsBackend, - MonadAsyncError, - SttpBackend, - SttpBackendOptions -} -import org.asynchttpclient.{ - AsyncHttpClient, - AsyncHttpClientConfig, - DefaultAsyncHttpClient -} +import com.softwaremill.sttp.{FollowRedirectsBackend, MonadAsyncError, SttpBackend, SttpBackendOptions} +import org.asynchttpclient.{AsyncHttpClient, AsyncHttpClientConfig, DefaultAsyncHttpClient} import org.reactivestreams.Publisher import scala.language.higherKinds @@ -27,47 +18,33 @@ class AsyncHttpClientCatsBackend[F[_]: Async] private ( new AsyncMonad, closeClient ) { - override protected def streamBodyToPublisher( - s: Nothing): Publisher[ByteBuffer] = s // nothing is everything + override protected def streamBodyToPublisher(s: Nothing): Publisher[ByteBuffer] = s // nothing is everything - override protected def publisherToStreamBody( - p: Publisher[ByteBuffer]): Nothing = + 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] = + 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]( - options: SttpBackendOptions = SttpBackendOptions.Default) - : SttpBackend[F, Nothing] = - AsyncHttpClientCatsBackend(AsyncHttpClientBackend.defaultClient(options), - 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] = + private def apply[F[_]: Async](asyncHttpClient: AsyncHttpClient, closeClient: Boolean): SttpBackend[F, Nothing] = + new FollowRedirectsBackend[F, Nothing](new AsyncHttpClientCatsBackend(asyncHttpClient, closeClient)) + + def apply[F[_]: Async](options: SttpBackendOptions = SttpBackendOptions.Default): SttpBackend[F, Nothing] = + AsyncHttpClientCatsBackend(AsyncHttpClientBackend.defaultClient(options), 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] { +private[cats] class AsyncMonad[F[_]](implicit F: Async[F]) extends MonadAsyncError[F] { - override def async[T]( - register: ((Either[Throwable, T]) => Unit) => Unit): F[T] = + 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) @@ -79,7 +56,6 @@ private[cats] class AsyncMonad[F[_]](implicit F: Async[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] = + 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 4c6dc71..e608499 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 @@ -13,25 +13,21 @@ import org.reactivestreams.Publisher import scala.concurrent.ExecutionContext import scala.language.higherKinds -class AsyncHttpClientFs2Backend[F[_]: Effect] private ( - asyncHttpClient: AsyncHttpClient, - closeClient: Boolean)(implicit ec: ExecutionContext) +class AsyncHttpClientFs2Backend[F[_]: Effect] private (asyncHttpClient: AsyncHttpClient, closeClient: Boolean)( + implicit ec: ExecutionContext) extends AsyncHttpClientBackend[F, Stream[F, ByteBuffer]]( asyncHttpClient, new EffectMonad, closeClient ) { - override protected def streamBodyToPublisher( - s: Stream[F, ByteBuffer]): Publisher[ByteBuffer] = + override protected def streamBodyToPublisher(s: Stream[F, ByteBuffer]): Publisher[ByteBuffer] = s.toUnicastPublisher - override protected def publisherToStreamBody( - p: Publisher[ByteBuffer]): Stream[F, ByteBuffer] = + override protected def publisherToStreamBody(p: Publisher[ByteBuffer]): Stream[F, ByteBuffer] = p.toStream[F] - override protected def publisherToString( - p: Publisher[ByteBuffer]): F[String] = { + override protected def publisherToString(p: Publisher[ByteBuffer]): F[String] = { val bytes = p .toStream[F] .compile @@ -43,23 +39,18 @@ class AsyncHttpClientFs2Backend[F[_]: Effect] private ( object AsyncHttpClientFs2Backend { - private def apply[F[_]: Effect](asyncHttpClient: AsyncHttpClient, - closeClient: Boolean)( + private def apply[F[_]: Effect](asyncHttpClient: AsyncHttpClient, closeClient: Boolean)( implicit ec: ExecutionContext): SttpBackend[F, Stream[F, ByteBuffer]] = - new FollowRedirectsBackend( - new AsyncHttpClientFs2Backend(asyncHttpClient, closeClient)) + new FollowRedirectsBackend(new AsyncHttpClientFs2Backend(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[F[_]: Effect](options: SttpBackendOptions = - SttpBackendOptions.Default)( - implicit ec: ExecutionContext = ExecutionContext.Implicits.global) - : SttpBackend[F, Stream[F, ByteBuffer]] = - AsyncHttpClientFs2Backend[F](AsyncHttpClientBackend.defaultClient(options), - closeClient = true) + def apply[F[_]: Effect](options: SttpBackendOptions = SttpBackendOptions.Default)( + implicit ec: ExecutionContext = ExecutionContext.Implicits.global): SttpBackend[F, Stream[F, ByteBuffer]] = + AsyncHttpClientFs2Backend[F](AsyncHttpClientBackend.defaultClient(options), closeClient = true) /** * @param ec The execution context for running non-network related operations, @@ -67,10 +58,8 @@ object AsyncHttpClientFs2Backend { * context. */ def usingConfig[F[_]: Effect](cfg: AsyncHttpClientConfig)( - implicit ec: ExecutionContext = ExecutionContext.Implicits.global) - : SttpBackend[F, Stream[F, ByteBuffer]] = - AsyncHttpClientFs2Backend[F](new DefaultAsyncHttpClient(cfg), - closeClient = true) + implicit ec: ExecutionContext = ExecutionContext.Implicits.global): SttpBackend[F, Stream[F, ByteBuffer]] = + AsyncHttpClientFs2Backend[F](new DefaultAsyncHttpClient(cfg), closeClient = true) /** * @param ec The execution context for running non-network related operations, @@ -78,16 +67,13 @@ object AsyncHttpClientFs2Backend { * context. */ def usingClient[F[_]: Effect](client: AsyncHttpClient)( - implicit ec: ExecutionContext = ExecutionContext.Implicits.global) - : SttpBackend[F, Stream[F, ByteBuffer]] = + implicit ec: ExecutionContext = ExecutionContext.Implicits.global): SttpBackend[F, Stream[F, ByteBuffer]] = AsyncHttpClientFs2Backend[F](client, closeClient = false) } -private[fs2] class EffectMonad[F[_]](implicit F: Effect[F]) - extends MonadAsyncError[F] { +private[fs2] class EffectMonad[F[_]](implicit F: Effect[F]) extends MonadAsyncError[F] { - override def async[T]( - register: ((Either[Throwable, T]) => Unit) => Unit): F[T] = + 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) @@ -99,7 +85,6 @@ private[fs2] class EffectMonad[F[_]](implicit F: Effect[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] = + 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/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 index 6778fa0..d91f404 100644 --- 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 @@ -3,37 +3,22 @@ package com.softwaremill.sttp.asynchttpclient.future import java.nio.ByteBuffer import com.softwaremill.sttp.asynchttpclient.AsyncHttpClientBackend -import com.softwaremill.sttp.{ - FollowRedirectsBackend, - FutureMonad, - SttpBackend, - SttpBackendOptions -} -import org.asynchttpclient.{ - AsyncHttpClient, - AsyncHttpClientConfig, - DefaultAsyncHttpClient -} +import com.softwaremill.sttp.{FollowRedirectsBackend, FutureMonad, SttpBackend, SttpBackendOptions} +import org.asynchttpclient.{AsyncHttpClient, AsyncHttpClientConfig, DefaultAsyncHttpClient} import org.reactivestreams.Publisher import scala.concurrent.{ExecutionContext, Future} -class AsyncHttpClientFutureBackend private ( - asyncHttpClient: AsyncHttpClient, - closeClient: Boolean)(implicit ec: ExecutionContext) - extends AsyncHttpClientBackend[Future, Nothing](asyncHttpClient, - new FutureMonad, - closeClient) { +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 streamBodyToPublisher(s: Nothing): Publisher[ByteBuffer] = s // nothing is everything - override protected def publisherToStreamBody( - p: Publisher[ByteBuffer]): Nothing = + 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] = + override protected def publisherToString(p: Publisher[ByteBuffer]): Future[String] = throw new IllegalStateException("This backend does not support streaming") } @@ -41,8 +26,7 @@ object AsyncHttpClientFutureBackend { private def apply(asyncHttpClient: AsyncHttpClient, closeClient: Boolean)( implicit ec: ExecutionContext): SttpBackend[Future, Nothing] = - new FollowRedirectsBackend[Future, Nothing]( - new AsyncHttpClientFutureBackend(asyncHttpClient, closeClient)) + new FollowRedirectsBackend[Future, Nothing](new AsyncHttpClientFutureBackend(asyncHttpClient, closeClient)) /** * @param ec The execution context for running non-network related operations, @@ -50,10 +34,8 @@ object AsyncHttpClientFutureBackend { * context. */ def apply(options: SttpBackendOptions = SttpBackendOptions.Default)( - implicit ec: ExecutionContext = ExecutionContext.Implicits.global) - : SttpBackend[Future, Nothing] = - AsyncHttpClientFutureBackend(AsyncHttpClientBackend.defaultClient(options), - closeClient = true) + implicit ec: ExecutionContext = ExecutionContext.Implicits.global): SttpBackend[Future, Nothing] = + AsyncHttpClientFutureBackend(AsyncHttpClientBackend.defaultClient(options), closeClient = true) /** * @param ec The execution context for running non-network related operations, @@ -61,18 +43,15 @@ object AsyncHttpClientFutureBackend { * context. */ def usingConfig(cfg: AsyncHttpClientConfig)( - implicit ec: ExecutionContext = ExecutionContext.Implicits.global) - : SttpBackend[Future, Nothing] = - AsyncHttpClientFutureBackend(new DefaultAsyncHttpClient(cfg), - closeClient = true) + 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] = + def usingClient(client: AsyncHttpClient)( + implicit ec: ExecutionContext = ExecutionContext.Implicits.global): SttpBackend[Future, Nothing] = AsyncHttpClientFutureBackend(client, closeClient = false) } 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 04df4ed..03d4d09 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 @@ -2,45 +2,27 @@ package com.softwaremill.sttp.asynchttpclient.monix import java.nio.ByteBuffer -import com.softwaremill.sttp.{ - FollowRedirectsBackend, - MonadAsyncError, - SttpBackend, - SttpBackendOptions, - Utf8, - concatByteBuffers -} import com.softwaremill.sttp.asynchttpclient.AsyncHttpClientBackend +import com.softwaremill.sttp.{FollowRedirectsBackend, MonadAsyncError, SttpBackend, SttpBackendOptions, Utf8, concatByteBuffers} import monix.eval.Task import monix.execution.{Cancelable, Scheduler} import monix.reactive.Observable -import org.asynchttpclient.{ - AsyncHttpClient, - AsyncHttpClientConfig, - DefaultAsyncHttpClient -} +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) { +class AsyncHttpClientMonixBackend private (asyncHttpClient: AsyncHttpClient, closeClient: Boolean)( + implicit scheduler: Scheduler) + extends AsyncHttpClientBackend[Task, Observable[ByteBuffer]](asyncHttpClient, TaskMonad, closeClient) { - override protected def streamBodyToPublisher( - s: Observable[ByteBuffer]): Publisher[ByteBuffer] = + override protected def streamBodyToPublisher(s: Observable[ByteBuffer]): Publisher[ByteBuffer] = s.toReactivePublisher - override protected def publisherToStreamBody( - p: Publisher[ByteBuffer]): Observable[ByteBuffer] = + override protected def publisherToStreamBody(p: Publisher[ByteBuffer]): Observable[ByteBuffer] = Observable.fromReactivePublisher(p) - override protected def publisherToString( - p: Publisher[ByteBuffer]): Task[String] = { + override protected def publisherToString(p: Publisher[ByteBuffer]): Task[String] = { val bytes = Observable .fromReactivePublisher(p) @@ -53,38 +35,31 @@ class AsyncHttpClientMonixBackend private ( object AsyncHttpClientMonixBackend { private def apply(asyncHttpClient: AsyncHttpClient, closeClient: Boolean)( - implicit scheduler: Scheduler) - : SttpBackend[Task, Observable[ByteBuffer]] = - new FollowRedirectsBackend( - new AsyncHttpClientMonixBackend(asyncHttpClient, closeClient)) + implicit scheduler: Scheduler): SttpBackend[Task, Observable[ByteBuffer]] = + new FollowRedirectsBackend(new AsyncHttpClientMonixBackend(asyncHttpClient, closeClient)) /** * @param s The scheduler used for streaming request bodies. Defaults to the * global scheduler. */ def apply(options: SttpBackendOptions = SttpBackendOptions.Default)( - implicit s: Scheduler = Scheduler.Implicits.global) - : SttpBackend[Task, Observable[ByteBuffer]] = - AsyncHttpClientMonixBackend(AsyncHttpClientBackend.defaultClient(options), - closeClient = true) + implicit s: Scheduler = Scheduler.Implicits.global): SttpBackend[Task, Observable[ByteBuffer]] = + AsyncHttpClientMonixBackend(AsyncHttpClientBackend.defaultClient(options), closeClient = true) /** * @param s The scheduler used for streaming request bodies. Defaults to the * global scheduler. */ - def usingConfig(cfg: AsyncHttpClientConfig)(implicit s: Scheduler = - Scheduler.Implicits.global) - : SttpBackend[Task, Observable[ByteBuffer]] = - AsyncHttpClientMonixBackend(new DefaultAsyncHttpClient(cfg), - closeClient = true) + def usingConfig(cfg: AsyncHttpClientConfig)( + implicit s: Scheduler = Scheduler.Implicits.global): SttpBackend[Task, Observable[ByteBuffer]] = + AsyncHttpClientMonixBackend(new DefaultAsyncHttpClient(cfg), closeClient = true) /** * @param s The scheduler used for streaming request bodies. Defaults to the * global scheduler. */ - def usingClient(client: AsyncHttpClient)(implicit s: Scheduler = - Scheduler.Implicits.global) - : SttpBackend[Task, Observable[ByteBuffer]] = + def usingClient(client: AsyncHttpClient)( + implicit s: Scheduler = Scheduler.Implicits.global): SttpBackend[Task, Observable[ByteBuffer]] = AsyncHttpClientMonixBackend(client, closeClient = false) } @@ -96,8 +71,7 @@ private[monix] object TaskMonad extends MonadAsyncError[Task] { 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] = + override def async[T](register: ((Either[Throwable, T]) => Unit) => Unit): Task[T] = Task.async { (_, cb) => register { case Left(t) => cb(Failure(t)) @@ -109,7 +83,6 @@ private[monix] object TaskMonad extends MonadAsyncError[Task] { 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] = + 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 3828233..cdb45c3 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 @@ -2,55 +2,35 @@ package com.softwaremill.sttp.asynchttpclient.scalaz import java.nio.ByteBuffer -import com.softwaremill.sttp.{ - FollowRedirectsBackend, - MonadAsyncError, - SttpBackend, - SttpBackendOptions -} import com.softwaremill.sttp.asynchttpclient.AsyncHttpClientBackend -import org.asynchttpclient.{ - AsyncHttpClient, - AsyncHttpClientConfig, - DefaultAsyncHttpClient -} +import com.softwaremill.sttp.{FollowRedirectsBackend, MonadAsyncError, SttpBackend, SttpBackendOptions} +import org.asynchttpclient.{AsyncHttpClient, AsyncHttpClientConfig, DefaultAsyncHttpClient} import org.reactivestreams.Publisher -import scalaz.{-\/, \/-} import scalaz.concurrent.Task +import scalaz.{-\/, \/-} -class AsyncHttpClientScalazBackend private (asyncHttpClient: AsyncHttpClient, - closeClient: Boolean) - extends AsyncHttpClientBackend[Task, Nothing](asyncHttpClient, - TaskMonad, - closeClient) { +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 streamBodyToPublisher(s: Nothing): Publisher[ByteBuffer] = s // nothing is everything - override protected def publisherToStreamBody( - p: Publisher[ByteBuffer]): Nothing = + 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] = + 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)) + private def apply(asyncHttpClient: AsyncHttpClient, closeClient: Boolean): SttpBackend[Task, Nothing] = + new FollowRedirectsBackend[Task, Nothing](new AsyncHttpClientScalazBackend(asyncHttpClient, closeClient)) - def apply(options: SttpBackendOptions = SttpBackendOptions.Default) - : SttpBackend[Task, Nothing] = - AsyncHttpClientScalazBackend(AsyncHttpClientBackend.defaultClient(options), - closeClient = true) + def apply(options: SttpBackendOptions = SttpBackendOptions.Default): SttpBackend[Task, Nothing] = + AsyncHttpClientScalazBackend(AsyncHttpClientBackend.defaultClient(options), closeClient = true) def usingConfig(cfg: AsyncHttpClientConfig): SttpBackend[Task, Nothing] = - AsyncHttpClientScalazBackend(new DefaultAsyncHttpClient(cfg), - closeClient = true) + AsyncHttpClientScalazBackend(new DefaultAsyncHttpClient(cfg), closeClient = true) def usingClient(client: AsyncHttpClient): SttpBackend[Task, Nothing] = AsyncHttpClientScalazBackend(client, closeClient = false) @@ -64,8 +44,7 @@ private[scalaz] object TaskMonad extends MonadAsyncError[Task] { 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] = + override def async[T](register: ((Either[Throwable, T]) => Unit) => Unit): Task[T] = Task.async { cb => register { case Left(t) => cb(-\/(t)) @@ -75,6 +54,6 @@ private[scalaz] object TaskMonad extends MonadAsyncError[Task] { 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) + override protected def handleWrappedError[T](rt: Task[T])(h: PartialFunction[Throwable, Task[T]]): Task[T] = + rt.handleWith(h) } diff --git a/async-http-client-backend/src/main/scala/com/softwaremill/sttp/asynchttpclient/AsyncHttpClientBackend.scala b/async-http-client-backend/src/main/scala/com/softwaremill/sttp/asynchttpclient/AsyncHttpClientBackend.scala index fb0f780..d804f08 100644 --- a/async-http-client-backend/src/main/scala/com/softwaremill/sttp/asynchttpclient/AsyncHttpClientBackend.scala +++ b/async-http-client-backend/src/main/scala/com/softwaremill/sttp/asynchttpclient/AsyncHttpClientBackend.scala @@ -8,25 +8,8 @@ import com.softwaremill.sttp._ import org.asynchttpclient.AsyncHandler.State import org.asynchttpclient.handler.StreamedAsyncHandler import org.asynchttpclient.proxy.ProxyServer -import org.asynchttpclient.request.body.multipart.{ - ByteArrayPart, - FilePart, - StringPart -} -import org.asynchttpclient.{ - AsyncCompletionHandler, - AsyncHandler, - AsyncHttpClient, - DefaultAsyncHttpClient, - DefaultAsyncHttpClientConfig, - HttpResponseBodyPart, - HttpResponseHeaders, - HttpResponseStatus, - Param, - RequestBuilder, - Request => AsyncRequest, - Response => AsyncResponse -} +import org.asynchttpclient.request.body.multipart.{ByteArrayPart, FilePart, StringPart} +import org.asynchttpclient.{AsyncCompletionHandler, AsyncHandler, AsyncHttpClient, DefaultAsyncHttpClient, DefaultAsyncHttpClientConfig, HttpResponseBodyPart, HttpResponseHeaders, HttpResponseStatus, Param, RequestBuilder, Request => AsyncRequest, Response => AsyncResponse} import org.reactivestreams.{Publisher, Subscriber, Subscription} import scala.collection.JavaConverters._ @@ -66,10 +49,9 @@ abstract class AsyncHttpClientBackend[R[_], S](asyncHttpClient: AsyncHttpClient, protected def publisherToString(p: Publisher[ByteBuffer]): R[String] - private def eagerAsyncHandler[T]( - responseAs: ResponseAs[T, S], - success: R[Response[T]] => Unit, - error: Throwable => Unit): AsyncHandler[Unit] = { + private def eagerAsyncHandler[T](responseAs: ResponseAs[T, S], + success: R[Response[T]] => Unit, + error: Throwable => Unit): AsyncHandler[Unit] = { new AsyncCompletionHandler[Unit] { override def onCompleted(response: AsyncResponse): Unit = @@ -79,17 +61,15 @@ abstract class AsyncHttpClientBackend[R[_], S](asyncHttpClient: AsyncHttpClient, } } - private def streamingAsyncHandler[T]( - responseAs: ResponseAsStream[T, S], - success: R[Response[T]] => Unit, - error: Throwable => Unit): AsyncHandler[Unit] = { + private def streamingAsyncHandler[T](responseAs: ResponseAsStream[T, S], + success: R[Response[T]] => Unit, + error: Throwable => Unit): AsyncHandler[Unit] = { new StreamedAsyncHandler[Unit] { private val builder = new AsyncResponse.ResponseBuilder() private var publisher: Option[Publisher[ByteBuffer]] = None private var completed = false - override def onStream( - p: Publisher[HttpResponseBodyPart]): AsyncHandler.State = { + override def onStream(p: Publisher[HttpResponseBodyPart]): AsyncHandler.State = { // Sadly we don't have .map on Publisher publisher = Some(new Publisher[ByteBuffer] { override def subscribe(s: Subscriber[_ >: ByteBuffer]): Unit = @@ -109,19 +89,15 @@ abstract class AsyncHttpClientBackend[R[_], S](asyncHttpClient: AsyncHttpClient, State.CONTINUE } - override def onBodyPartReceived( - bodyPart: HttpResponseBodyPart): AsyncHandler.State = - throw new IllegalStateException( - "Requested a streaming backend, unexpected eager body parts.") + override def onBodyPartReceived(bodyPart: HttpResponseBodyPart): AsyncHandler.State = + throw new IllegalStateException("Requested a streaming backend, unexpected eager body parts.") - override def onHeadersReceived( - headers: HttpResponseHeaders): AsyncHandler.State = { + override def onHeadersReceived(headers: HttpResponseHeaders): AsyncHandler.State = { builder.accumulate(headers) State.CONTINUE } - override def onStatusReceived( - responseStatus: HttpResponseStatus): AsyncHandler.State = { + override def onStatusReceived(responseStatus: HttpResponseStatus): AsyncHandler.State = { builder.accumulate(responseStatus) State.CONTINUE } @@ -160,16 +136,13 @@ abstract class AsyncHttpClientBackend[R[_], S](asyncHttpClient: AsyncHttpClient, val readTimeout = r.options.readTimeout val rb = new RequestBuilder(r.method.m) .setUrl(r.uri.toString) - .setRequestTimeout( - if (readTimeout.isFinite()) readTimeout.toMillis.toInt else -1) + .setRequestTimeout(if (readTimeout.isFinite()) readTimeout.toMillis.toInt else -1) r.headers.foreach { case (k, v) => rb.setHeader(k, v) } setBody(r, r.body, rb) rb.build() } - private def setBody(r: Request[_, S], - body: RequestBody[S], - rb: RequestBuilder): Unit = { + private def setBody(r: Request[_, S], body: RequestBody[S], rb: RequestBuilder): Unit = { body match { case NoBody => // skip @@ -211,10 +184,7 @@ abstract class AsyncHttpClientBackend[R[_], S](asyncHttpClient: AsyncHttpClient, val bodyPart = mp.body match { case StringBody(b, encoding, _) => - new StringPart(nameWithFilename, - b, - mp.contentType.getOrElse(TextPlainContentType), - Charset.forName(encoding)) + new StringPart(nameWithFilename, b, mp.contentType.getOrElse(TextPlainContentType), Charset.forName(encoding)) case ByteArrayBody(b, _) => new ByteArrayPart(nameWithFilename, b) case ByteBufferBody(b, _) => @@ -227,15 +197,12 @@ abstract class AsyncHttpClientBackend[R[_], S](asyncHttpClient: AsyncHttpClient, new FilePart(mp.name, b.toFile, null, null, mp.fileName.orNull) } - bodyPart.setCustomHeaders( - mp.additionalHeaders.map(h => new Param(h._1, h._2)).toList.asJava) + bodyPart.setCustomHeaders(mp.additionalHeaders.map(h => new Param(h._1, h._2)).toList.asJava) rb.addBodyPart(bodyPart) } - private def readEagerResponse[T]( - response: AsyncResponse, - responseAs: ResponseAs[T, S]): R[Response[T]] = { + private def readEagerResponse[T](response: AsyncResponse, responseAs: ResponseAs[T, S]): R[Response[T]] = { val base = readResponseNoBody(response) val body = if (codeIsSuccess(base.code)) { @@ -280,9 +247,7 @@ abstract class AsyncHttpClientBackend[R[_], S](asyncHttpClient: AsyncHttpClient, Try(response.getResponseBodyAsBytes) case ResponseAsStream() => - Failure( - new IllegalStateException( - "Requested a streaming response, trying to read eagerly.")) + Failure(new IllegalStateException("Requested a streaming response, trying to read eagerly.")) case ResponseAsFile(file, overwrite) => Try( @@ -299,8 +264,7 @@ abstract class AsyncHttpClientBackend[R[_], S](asyncHttpClient: AsyncHttpClient, object AsyncHttpClientBackend { - private[asynchttpclient] def defaultClient( - options: SttpBackendOptions): AsyncHttpClient = { + private[asynchttpclient] def defaultClient(options: SttpBackendOptions): AsyncHttpClient = { var configBuilder = new DefaultAsyncHttpClientConfig.Builder() .setConnectTimeout(options.connectionTimeout.toMillis.toInt) @@ -308,8 +272,7 @@ object AsyncHttpClientBackend { configBuilder = options.proxy match { case None => configBuilder case Some(p) => - configBuilder.setProxyServer( - new ProxyServer.Builder(p.host, p.port).build()) + configBuilder.setProxyServer(new ProxyServer.Builder(p.host, p.port).build()) } new DefaultAsyncHttpClient(configBuilder.build()) |