aboutsummaryrefslogtreecommitdiff
path: root/async-http-client-handler/src
diff options
context:
space:
mode:
authoradamw <adam@warski.org>2017-07-21 16:40:07 +0200
committeradamw <adam@warski.org>2017-07-21 16:40:07 +0200
commite5ebd242a4cb982af6b01ec1976ecfc91398189f (patch)
tree3ca28ad0c4409d05bf50c3059e783a08d0947153 /async-http-client-handler/src
parent1e8b6064c3855a85c340165fad7feefaf656e074 (diff)
downloadsttp-e5ebd242a4cb982af6b01ec1976ecfc91398189f.tar.gz
sttp-e5ebd242a4cb982af6b01ec1976ecfc91398189f.tar.bz2
sttp-e5ebd242a4cb982af6b01ec1976ecfc91398189f.zip
Scalaz version of the async http client handler, wrapping responses in a Task
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]
+}