aboutsummaryrefslogtreecommitdiff
path: root/async-http-client-handler/future/src/main/scala/com/softwaremill/sttp/asynchttpclient/future/FutureAsyncHttpClientHandler.scala
blob: 624c51b174ab6fc45ed2104a588f170f3c492af5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.softwaremill.sttp.asynchttpclient.future

import com.softwaremill.sttp.asynchttpclient.internal.{
  AsyncHttpClientHandler,
  WrapperFromAsync
}
import org.asynchttpclient.{
  AsyncHttpClient,
  AsyncHttpClientConfig,
  DefaultAsyncHttpClient
}

import scala.concurrent.{Future, Promise}

class FutureAsyncHttpClientHandler(asyncHttpClient: AsyncHttpClient)
    extends AsyncHttpClientHandler[Future](asyncHttpClient, FutureFromAsync) {

  def this() = this(new DefaultAsyncHttpClient())
  def this(cfg: AsyncHttpClientConfig) = this(new DefaultAsyncHttpClient(cfg))
}

private[asynchttpclient] object FutureFromAsync
    extends WrapperFromAsync[Future] {
  override def apply[T](
      register: ((Either[Throwable, T]) => Unit) => Unit): Future[T] = {
    val p = Promise[T]()
    register {
      case Left(t)  => p.failure(t)
      case Right(t) => p.success(t)
    }
    p.future
  }
}