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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
package com.softwaremill.sttp.asynchttpclient.future
import java.nio.ByteBuffer
import com.softwaremill.sttp.asynchttpclient.AsyncHttpClientHandler
import com.softwaremill.sttp.{FutureMonad, SttpHandler}
import org.asynchttpclient.{
AsyncHttpClient,
AsyncHttpClientConfig,
DefaultAsyncHttpClient
}
import org.reactivestreams.Publisher
import scala.concurrent.{ExecutionContext, Future}
class AsyncHttpClientFutureHandler private (
asyncHttpClient: AsyncHttpClient,
closeClient: Boolean)(implicit ec: ExecutionContext)
extends AsyncHttpClientHandler[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 handler does not support streaming")
}
object AsyncHttpClientFutureHandler {
/**
* @param ec The execution context for running non-network related operations,
* e.g. mapping responses. Defaults to the global execution
* context.
*/
def apply()(implicit ec: ExecutionContext = ExecutionContext.Implicits.global)
: SttpHandler[Future, Nothing] =
new AsyncHttpClientFutureHandler(new DefaultAsyncHttpClient(),
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)
: SttpHandler[Future, Nothing] =
new AsyncHttpClientFutureHandler(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)
: SttpHandler[Future, Nothing] =
new AsyncHttpClientFutureHandler(client, closeClient = false)
}
|