Skip to content

Commit

Permalink
Armeria
Browse files Browse the repository at this point in the history
  • Loading branch information
adamw committed Dec 28, 2024
1 parent aa8823b commit e793de3
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import sttp.capabilities.fs2.Fs2Streams
import sttp.client4.armeria.ArmeriaWebClient.newClient
import sttp.client4.armeria.{AbstractArmeriaBackend, BodyFromStreamMessage}
import sttp.client4.impl.cats.CatsMonadAsyncError
import sttp.client4.wrappers.FollowRedirectsBackend
import sttp.client4.{wrappers, BackendOptions, StreamBackend}
import sttp.monad.MonadAsyncError
import sttp.client4.compression.Compressor
import sttp.client4.impl.fs2.DeflateFs2Compressor
import sttp.client4.impl.fs2.GZipFs2Compressor

private final class ArmeriaFs2Backend[F[_]: Async](client: WebClient, closeFactory: Boolean, dispatcher: Dispatcher[F])
extends AbstractArmeriaBackend[F, Fs2Streams[F]](client, closeFactory, new CatsMonadAsyncError) {
Expand All @@ -41,6 +43,9 @@ private final class ArmeriaFs2Backend[F[_]: Async](client: WebClient, closeFacto
},
dispatcher
)

override protected def compressors: List[Compressor[R]] =
List(new GZipFs2Compressor[F, R](), new DeflateFs2Compressor[F, R]())
}

object ArmeriaFs2Backend {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ import sttp.client4.internal.toByteArray
import sttp.model._
import sttp.monad.syntax._
import sttp.monad.{Canceler, MonadAsyncError}
import sttp.client4.compression.Compressor
import sttp.client4.compression.GZipDefaultCompressor
import sttp.client4.compression.DeflateDefaultCompressor

abstract class AbstractArmeriaBackend[F[_], S <: Streams[S]](
client: WebClient = WebClient.of(),
Expand All @@ -54,6 +57,8 @@ abstract class AbstractArmeriaBackend[F[_], S <: Streams[S]](

protected def streamToPublisher(stream: streams.BinaryStream): Publisher[HttpData]

protected def compressors: List[Compressor[R]] = List(new GZipDefaultCompressor(), new DeflateDefaultCompressor())

override def send[T](request: GenericRequest[T, R]): F[Response[T]] =
monad.suspend(adjustExceptions(request)(execute(request)))

Expand Down Expand Up @@ -87,7 +92,7 @@ abstract class AbstractArmeriaBackend[F[_], S <: Streams[S]](
} finally captor.close()
}

private def requestToArmeria(request: GenericRequest[_, Nothing]): WebClientRequestPreparation = {
private def requestToArmeria(request: GenericRequest[_, R]): WebClientRequestPreparation = {
val requestPreparation = client
.prepare()
.disablePathParams()
Expand All @@ -102,19 +107,24 @@ abstract class AbstractArmeriaBackend[F[_], S <: Streams[S]](
requestPreparation.responseTimeoutMillis(Long.MaxValue)
}

val (body, contentLength) = Compressor.compressIfNeeded(request, compressors)

var customContentType: Option[ArmeriaMediaType] = None
request.headers.foreach { header =>
if (header.name.equalsIgnoreCase(HeaderNames.ContentType)) {
if (header.is(HeaderNames.ContentType)) {
// A Content-Type will be set with the body content
customContentType = Some(ArmeriaMediaType.parse(header.value))
} else {
requestPreparation.header(header.name, header.value)
} else if (!header.is(HeaderNames.ContentLength)) {
val _ = requestPreparation.header(header.name, header.value)
}
}
contentLength.foreach { cl =>
requestPreparation.header(HeaderNames.ContentLength, cl.toString)
}

val contentType = customContentType.getOrElse(ArmeriaMediaType.parse(request.body.defaultContentType.toString()))

request.body match {
body match {
case NoBody => requestPreparation
case StringBody(s, encoding, _) =>
val charset =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ import sttp.capabilities.zio.ZioStreams
import sttp.client4.armeria.ArmeriaWebClient.newClient
import sttp.client4.armeria.{AbstractArmeriaBackend, BodyFromStreamMessage}
import sttp.client4.impl.zio.RIOMonadAsyncError
import sttp.client4.wrappers.FollowRedirectsBackend
import sttp.client4.{wrappers, BackendOptions, StreamBackend}
import sttp.monad.MonadAsyncError
import zio.stream.Stream
import sttp.client4.compression.Compressor
import sttp.client4.impl.zio.GZipZioCompressor
import sttp.client4.impl.zio.DeflateZioCompressor

private final class ArmeriaZioBackend(runtime: Runtime[Any], client: WebClient, closeFactory: Boolean)
extends AbstractArmeriaBackend[Task, ZioStreams](client, closeFactory, new RIOMonadAsyncError[Any]) {
Expand All @@ -40,6 +42,9 @@ private final class ArmeriaZioBackend(runtime: Runtime[Any], client: WebClient,
.run(stream.mapChunks(c => Chunk.single(HttpData.wrap(c.toArray))).toPublisher)
.getOrThrowFiberFailure()
}

override protected def compressors: List[Compressor[R]] =
List(new GZipZioCompressor[R](), new DeflateZioCompressor[R]())
}

object ArmeriaZioBackend {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import java.util
import java.util.concurrent.Flow.Publisher
import java.{util => ju}
import sttp.client4.compression.Compressor
import sttp.client4.impl.zio.{DeflateZioCompressor, GZipZioCompressor}

class HttpClientZioBackend private (
client: HttpClient,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sttp.client4.httpclient.zio
package sttp.client4.impl.zio

import sttp.client4._
import sttp.client4.compression.Compressor
Expand Down

0 comments on commit e793de3

Please sign in to comment.