blob: e6dcf1b46565680cfb63c368ff56708501daf41e (
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
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
async-http-client backend
=========================
To use, add the following dependency to your project::
"com.softwaremill.sttp" %% "async-http-client-backend-future" % "1.0.0"
// or
"com.softwaremill.sttp" %% "async-http-client-backend-scalaz" % "1.0.0"
// or
"com.softwaremill.sttp" %% "async-http-client-backend-monix" % "1.0.0"
// or
"com.softwaremill.sttp" %% "async-http-client-backend-cats" % "1.0.0"
This backend depends on `async-http-client <https://github.com/AsyncHttpClient/async-http-client>`_.
A fully **asynchronous** backend, which uses `Netty <http://netty.io>`_ behind the
scenes.
The responses are wrapped depending on the dependency chosen in either a:
* standard Scala ``Future``
* `Scalaz <https://github.com/scalaz/scalaz>`_ ``Task``. There's a transitive dependency on ``scalaz-concurrent``.
* `Monix <https://monix.io>`_ ``Task``. There's a transitive dependency on ``monix-eval``.
* Any type implementing the `Cats Effect <https://github.com/typelevel/cats-effect>`_ ``Async`` typeclass, such as ``cats.effect.IO``. There's a transitive dependency on ``cats-effect``.
Next you'll need to add an implicit value::
implicit val sttpBackend = AsyncHttpClientFutureBackend()
// or, if you're using the scalaz version:
implicit val sttpBackend = AsyncHttpClientScalazBackend()
// or, if you're using the monix version:
implicit val sttpBackend = AsyncHttpClientMonixBackend()
// or, if you're using the cats effect version:
implicit val sttpBackend = AsyncHttpClientCatsBackend[cats.effect.IO]()
// or, if you'd like to use custom configuration:
implicit val sttpBackend = AsyncHttpClientFutureBackend.usingConfig(asyncHttpClientConfig)
// or, if you'd like to instantiate the AsyncHttpClient yourself:
implicit val sttpBackend = AsyncHttpClientFutureBackend.usingClient(asyncHttpClient)
Streaming using Monix
---------------------
The Monix backend supports streaming (as both Monix and Async Http Client support reactive streams ``Publisher`` s out of the box). The type of supported streams in this case is ``Observable[ByteBuffer]``. That is, you can set such an observable as a request body::
import com.softwaremill.sttp._
import java.nio.ByteBuffer
import monix.reactive.Observable
val obs: Observable[ByteBuffer] = ...
sttp
.streamBody(obs)
.post(uri"...")
And receive responses as an observable stream::
import com.softwaremill.sttp._
import com.softwaremill.sttp.asynchttpclient.monix._
import java.nio.ByteBuffer
import monix.eval.Task
import monix.reactive.Observable
implicit val sttpBackend = AsyncHttpClientMonixBackend()
val response: Task[Response[Observable[ByteBuffer]]] =
sttp
.post(uri"...")
.response(asStream[Observable[ByteBuffer]])
.send()
It's also possible to use `fs2 <https://github.com/functional-streams-for-scala/fs2>`_ streams for sending request & receiving responses.
|