Skip to content

Commit 9d4207d

Browse files
committed
Migrate some async-http-client backends
1 parent 4041706 commit 9d4207d

File tree

21 files changed

+243
-946
lines changed

21 files changed

+243
-946
lines changed

async-http-client-backend/cats-ce2/src/main/scala/sttp/client4/asynchttpclient/cats/AsyncHttpClientCatsBackend.scala

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,28 @@
11
package sttp.client4.asynchttpclient.cats
22

3-
import java.io.{ByteArrayInputStream, File}
4-
import java.nio.ByteBuffer
53
import cats.effect.implicits._
64
import cats.effect.{Concurrent, ContextShift, Resource, Sync}
7-
import io.netty.buffer.ByteBuf
8-
import org.asynchttpclient.{
9-
AsyncHttpClient,
10-
AsyncHttpClientConfig,
11-
BoundRequestBuilder,
12-
DefaultAsyncHttpClient,
13-
DefaultAsyncHttpClientConfig
14-
}
15-
import org.reactivestreams.Publisher
16-
import sttp.client4.asynchttpclient.{AsyncHttpClientBackend, BodyFromAHC, BodyToAHC}
5+
import org.asynchttpclient._
6+
import sttp.client4.asynchttpclient.AsyncHttpClientBackend
177
import sttp.client4.impl.cats.CatsMonadAsyncError
18-
import sttp.client4.internal.{FileHelpers, NoStreams}
19-
import sttp.client4.{wrappers, Backend, BackendOptions, GenericRequest, Response}
20-
import cats.implicits._
218
import sttp.client4.internal.ws.SimpleQueue
229
import sttp.client4.testing.BackendStub
23-
import sttp.client4.wrappers.FollowRedirectsBackend
24-
import sttp.monad.MonadAsyncError
25-
import sttp.ws.WebSocket
10+
import sttp.client4.{wrappers, Backend, BackendOptions, GenericRequest, Response}
2611

2712
class AsyncHttpClientCatsBackend[F[_]: Concurrent: ContextShift] private (
2813
asyncHttpClient: AsyncHttpClient,
2914
closeClient: Boolean,
3015
customizeRequest: BoundRequestBuilder => BoundRequestBuilder
31-
) extends AsyncHttpClientBackend[F, Nothing, Any](
16+
) extends AsyncHttpClientBackend[F, Any](
3217
asyncHttpClient,
3318
new CatsMonadAsyncError,
3419
closeClient,
3520
customizeRequest
3621
) {
3722

38-
override val streams: NoStreams = NoStreams
39-
4023
override def send[T](r: GenericRequest[T, R]): F[Response[T]] =
4124
super.send(r).guarantee(implicitly[ContextShift[F]].shift)
4225

43-
override protected val bodyFromAHC: BodyFromAHC[F, Nothing] = new BodyFromAHC[F, Nothing] {
44-
override val streams: NoStreams = NoStreams
45-
override implicit val monad: MonadAsyncError[F] = new CatsMonadAsyncError
46-
override def publisherToStream(p: Publisher[ByteBuffer]): Nothing =
47-
throw new IllegalStateException("This backend does not support streaming")
48-
override def compileWebSocketPipe(ws: WebSocket[F], pipe: Nothing): F[Unit] = pipe // nothing is everything
49-
50-
override def publisherToFile(p: Publisher[ByteBuffer], f: File): F[Unit] =
51-
publisherToBytes(p)
52-
.guarantee(implicitly[ContextShift[F]].shift)
53-
.map(bytes => FileHelpers.saveFile(f, new ByteArrayInputStream(bytes)))
54-
}
55-
56-
override protected def bodyToAHC: BodyToAHC[F, Nothing] =
57-
new BodyToAHC[F, Nothing] {
58-
override val streams: NoStreams = NoStreams
59-
override protected def streamToPublisher(s: Nothing): Publisher[ByteBuf] = s // nothing is everything
60-
}
61-
6226
override protected def createSimpleQueue[T]: F[SimpleQueue[F, T]] =
6327
throw new IllegalStateException("Web sockets are not supported!")
6428
}

async-http-client-backend/cats/src/main/scala/sttp/client4/asynchttpclient/cats/AsyncHttpClientCatsBackend.scala

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,23 @@
11
package sttp.client4.asynchttpclient.cats
22

3-
import java.io.{ByteArrayInputStream, File}
4-
import java.nio.ByteBuffer
53
import cats.effect.kernel.{Async, Resource, Sync}
6-
import io.netty.buffer.ByteBuf
7-
import org.asynchttpclient.{
8-
AsyncHttpClient,
9-
AsyncHttpClientConfig,
10-
BoundRequestBuilder,
11-
DefaultAsyncHttpClient,
12-
DefaultAsyncHttpClientConfig
13-
}
14-
import org.reactivestreams.Publisher
15-
import sttp.client4.asynchttpclient.{AsyncHttpClientBackend, BodyFromAHC, BodyToAHC}
4+
import org.asynchttpclient._
5+
import sttp.client4.asynchttpclient.AsyncHttpClientBackend
166
import sttp.client4.impl.cats.CatsMonadAsyncError
17-
import sttp.client4.internal.{FileHelpers, NoStreams}
18-
import sttp.client4.{wrappers, Backend, BackendOptions}
19-
import cats.implicits._
207
import sttp.client4.internal.ws.SimpleQueue
218
import sttp.client4.testing.BackendStub
22-
import sttp.client4.wrappers.FollowRedirectsBackend
23-
import sttp.monad.MonadAsyncError
24-
import sttp.ws.WebSocket
9+
import sttp.client4.{wrappers, Backend, BackendOptions}
2510

2611
class AsyncHttpClientCatsBackend[F[_]: Async] private (
2712
asyncHttpClient: AsyncHttpClient,
2813
closeClient: Boolean,
2914
customizeRequest: BoundRequestBuilder => BoundRequestBuilder
30-
) extends AsyncHttpClientBackend[F, Nothing, Any](
15+
) extends AsyncHttpClientBackend[F, Any](
3116
asyncHttpClient,
3217
new CatsMonadAsyncError,
3318
closeClient,
3419
customizeRequest
3520
) {
36-
37-
override val streams: NoStreams = NoStreams
38-
39-
override protected val bodyFromAHC: BodyFromAHC[F, Nothing] = new BodyFromAHC[F, Nothing] {
40-
override val streams: NoStreams = NoStreams
41-
override implicit val monad: MonadAsyncError[F] = new CatsMonadAsyncError
42-
override def publisherToStream(p: Publisher[ByteBuffer]): Nothing =
43-
throw new IllegalStateException("This backend does not support streaming")
44-
override def compileWebSocketPipe(ws: WebSocket[F], pipe: Nothing): F[Unit] = pipe // nothing is everything
45-
46-
override def publisherToFile(p: Publisher[ByteBuffer], f: File): F[Unit] =
47-
publisherToBytes(p)
48-
.map(bytes => FileHelpers.saveFile(f, new ByteArrayInputStream(bytes)))
49-
}
50-
51-
override protected def bodyToAHC: BodyToAHC[F, Nothing] =
52-
new BodyToAHC[F, Nothing] {
53-
override val streams: NoStreams = NoStreams
54-
override protected def streamToPublisher(s: Nothing): Publisher[ByteBuf] = s // nothing is everything
55-
}
56-
5721
override protected def createSimpleQueue[T]: F[SimpleQueue[F, T]] =
5822
throw new IllegalStateException("Web sockets are not supported!")
5923
}

async-http-client-backend/future/src/main/scala/sttp/client4/asynchttpclient/future/AsyncHttpClientFutureBackend.scala

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,11 @@
11
package sttp.client4.asynchttpclient.future
22

3-
import java.nio.ByteBuffer
4-
import io.netty.buffer.ByteBuf
5-
import org.asynchttpclient.{
6-
AsyncHttpClient,
7-
AsyncHttpClientConfig,
8-
BoundRequestBuilder,
9-
DefaultAsyncHttpClient,
10-
DefaultAsyncHttpClientConfig
11-
}
12-
import org.reactivestreams.Publisher
13-
import sttp.client4.asynchttpclient.{AsyncHttpClientBackend, BodyFromAHC, BodyToAHC}
14-
import sttp.client4.internal.NoStreams
3+
import org.asynchttpclient._
4+
import sttp.client4.asynchttpclient.AsyncHttpClientBackend
155
import sttp.client4.internal.ws.SimpleQueue
166
import sttp.client4.testing.BackendStub
17-
import sttp.client4.wrappers.FollowRedirectsBackend
187
import sttp.client4.{wrappers, Backend, BackendOptions}
19-
import sttp.monad.{FutureMonad, MonadAsyncError}
20-
import sttp.ws.WebSocket
8+
import sttp.monad.FutureMonad
219

2210
import scala.concurrent.{ExecutionContext, Future}
2311

@@ -27,30 +15,13 @@ class AsyncHttpClientFutureBackend private (
2715
customizeRequest: BoundRequestBuilder => BoundRequestBuilder
2816
)(implicit
2917
ec: ExecutionContext
30-
) extends AsyncHttpClientBackend[Future, Nothing, Any](
18+
) extends AsyncHttpClientBackend[Future, Any](
3119
asyncHttpClient,
3220
new FutureMonad,
3321
closeClient,
3422
customizeRequest
3523
) {
3624

37-
override val streams: NoStreams = NoStreams
38-
39-
override protected val bodyFromAHC: BodyFromAHC[Future, Nothing] = new BodyFromAHC[Future, Nothing] {
40-
override val streams: NoStreams = NoStreams
41-
override implicit val monad: MonadAsyncError[Future] = new FutureMonad
42-
override def publisherToStream(p: Publisher[ByteBuffer]): Nothing =
43-
throw new IllegalStateException("This backend does not support streaming")
44-
override def compileWebSocketPipe(ws: WebSocket[Future], pipe: Nothing): Future[Unit] =
45-
pipe // nothing is everything
46-
}
47-
48-
override protected def bodyToAHC: BodyToAHC[Future, Nothing] =
49-
new BodyToAHC[Future, Nothing] {
50-
override val streams: NoStreams = NoStreams
51-
override protected def streamToPublisher(s: Nothing): Publisher[ByteBuf] = s // nothing is everything
52-
}
53-
5425
override protected def createSimpleQueue[T]: Future[SimpleQueue[Future, T]] =
5526
throw new IllegalStateException("Web sockets are not supported!")
5627
}

0 commit comments

Comments
 (0)