aboutsummaryrefslogtreecommitdiff
path: root/async-http-client-backend/fs2/src/main/scala/com/softwaremill/sttp/asynchttpclient/fs2/AsyncHttpClientFs2Backend.scala
blob: 4c6dc71e9db5e1ff95711acb53b10d86452106df (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package com.softwaremill.sttp.asynchttpclient.fs2

import java.nio.ByteBuffer

import cats.effect._
import com.softwaremill.sttp.asynchttpclient.AsyncHttpClientBackend
import com.softwaremill.sttp.{FollowRedirectsBackend, MonadAsyncError, SttpBackend, SttpBackendOptions, Utf8, concatByteBuffers}
import fs2._
import fs2.interop.reactivestreams._
import org.asynchttpclient.{AsyncHttpClient, AsyncHttpClientConfig, DefaultAsyncHttpClient}
import org.reactivestreams.Publisher

import scala.concurrent.ExecutionContext
import scala.language.higherKinds

class AsyncHttpClientFs2Backend[F[_]: Effect] private (
    asyncHttpClient: AsyncHttpClient,
    closeClient: Boolean)(implicit ec: ExecutionContext)
    extends AsyncHttpClientBackend[F, Stream[F, ByteBuffer]](
      asyncHttpClient,
      new EffectMonad,
      closeClient
    ) {

  override protected def streamBodyToPublisher(
      s: Stream[F, ByteBuffer]): Publisher[ByteBuffer] =
    s.toUnicastPublisher

  override protected def publisherToStreamBody(
      p: Publisher[ByteBuffer]): Stream[F, ByteBuffer] =
    p.toStream[F]

  override protected def publisherToString(
      p: Publisher[ByteBuffer]): F[String] = {
    val bytes = p
      .toStream[F]
      .compile
      .fold(ByteBuffer.allocate(0))(concatByteBuffers)

    implicitly[Effect[F]].map(bytes)(bb => new String(bb.array(), Utf8))
  }
}

object AsyncHttpClientFs2Backend {

  private def apply[F[_]: Effect](asyncHttpClient: AsyncHttpClient,
                                  closeClient: Boolean)(
      implicit ec: ExecutionContext): SttpBackend[F, Stream[F, ByteBuffer]] =
    new FollowRedirectsBackend(
      new AsyncHttpClientFs2Backend(asyncHttpClient, closeClient))

  /**
    * @param ec The execution context for running non-network related operations,
    *           e.g. mapping responses. Defaults to the global execution
    *           context.
    */
  def apply[F[_]: Effect](options: SttpBackendOptions =
                            SttpBackendOptions.Default)(
      implicit ec: ExecutionContext = ExecutionContext.Implicits.global)
    : SttpBackend[F, Stream[F, ByteBuffer]] =
    AsyncHttpClientFs2Backend[F](AsyncHttpClientBackend.defaultClient(options),
                                 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[F[_]: Effect](cfg: AsyncHttpClientConfig)(
      implicit ec: ExecutionContext = ExecutionContext.Implicits.global)
    : SttpBackend[F, Stream[F, ByteBuffer]] =
    AsyncHttpClientFs2Backend[F](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[F[_]: Effect](client: AsyncHttpClient)(
      implicit ec: ExecutionContext = ExecutionContext.Implicits.global)
    : SttpBackend[F, Stream[F, ByteBuffer]] =
    AsyncHttpClientFs2Backend[F](client, closeClient = false)
}

private[fs2] class EffectMonad[F[_]](implicit F: Effect[F])
    extends MonadAsyncError[F] {

  override def async[T](
      register: ((Either[Throwable, T]) => Unit) => Unit): F[T] =
    F.async(register)

  override def unit[T](t: T): F[T] = F.pure(t)

  override def map[T, T2](fa: F[T])(f: (T) => T2): F[T2] = F.map(fa)(f)

  override def flatMap[T, T2](fa: F[T])(f: (T) => F[T2]): F[T2] =
    F.flatMap(fa)(f)

  override def error[T](t: Throwable): F[T] = F.raiseError(t)

  override protected def handleWrappedError[T](rt: F[T])(
      h: PartialFunction[Throwable, F[T]]): F[T] =
    F.recoverWith(rt)(h)
}