diff options
Diffstat (limited to 'async-http-client-handler/src')
-rw-r--r-- | async-http-client-handler/src/main/scala/com/softwaremill/sttp/asynchttpclient/internal/AsyncHttpClientHandler.scala (renamed from async-http-client-handler/src/main/scala/com/softwaremill/sttp/asynchttpclient/AsyncHttpClientHandler.scala) | 47 |
1 files changed, 24 insertions, 23 deletions
diff --git a/async-http-client-handler/src/main/scala/com/softwaremill/sttp/asynchttpclient/AsyncHttpClientHandler.scala b/async-http-client-handler/src/main/scala/com/softwaremill/sttp/asynchttpclient/internal/AsyncHttpClientHandler.scala index ecd49cd..924e0bc 100644 --- a/async-http-client-handler/src/main/scala/com/softwaremill/sttp/asynchttpclient/AsyncHttpClientHandler.scala +++ b/async-http-client-handler/src/main/scala/com/softwaremill/sttp/asynchttpclient/internal/AsyncHttpClientHandler.scala @@ -1,4 +1,4 @@ -package com.softwaremill.sttp.asynchttpclient +package com.softwaremill.sttp.asynchttpclient.internal import java.nio.charset.Charset @@ -7,34 +7,31 @@ import com.softwaremill.sttp.{Request, Response, SttpHandler} import org.asynchttpclient.{ AsyncCompletionHandler, AsyncHttpClient, - AsyncHttpClientConfig, - DefaultAsyncHttpClient, RequestBuilder, Request => AsyncRequest, Response => AsyncResponse } -import scala.concurrent.{Future, Promise} import scala.collection.JavaConverters._ - -class AsyncHttpClientHandler(asyncHttpClient: AsyncHttpClient) - extends SttpHandler[Future, Nothing] { - def this() = this(new DefaultAsyncHttpClient()) - def this(cfg: AsyncHttpClientConfig) = this(new DefaultAsyncHttpClient(cfg)) - - override def send[T](r: Request[T, Nothing]): Future[Response[T]] = { - val p = Promise[Response[T]]() - asyncHttpClient - .prepareRequest(requestToAsync(r)) - .execute(new AsyncCompletionHandler[AsyncResponse] { - override def onCompleted(response: AsyncResponse): AsyncResponse = { - p.success(readResponse(response, r.responseAs)) - response - } - override def onThrowable(t: Throwable): Unit = p.failure(t) - }) - - p.future +import scala.language.higherKinds + +private[asynchttpclient] class AsyncHttpClientHandler[R[_]]( + asyncHttpClient: AsyncHttpClient, + wrapper: WrapperFromAsync[R]) + extends SttpHandler[R, Nothing] { + + override def send[T](r: Request[T, Nothing]): R[Response[T]] = { + wrapper { cb => + asyncHttpClient + .prepareRequest(requestToAsync(r)) + .execute(new AsyncCompletionHandler[AsyncResponse] { + override def onCompleted(response: AsyncResponse): AsyncResponse = { + cb(Right(readResponse(response, r.responseAs))) + response + } + override def onThrowable(t: Throwable): Unit = cb(Left(t)) + }) + } } private def requestToAsync(r: Request[_, Nothing]): AsyncRequest = { @@ -111,3 +108,7 @@ class AsyncHttpClientHandler(asyncHttpClient: AsyncHttpClient) } } } + +private[asynchttpclient] trait WrapperFromAsync[R[_]] { + def apply[T](register: (Either[Throwable, T] => Unit) => Unit): R[T] +} |