Skip to content

Commit

Permalink
OkHttp
Browse files Browse the repository at this point in the history
  • Loading branch information
adamw committed Dec 28, 2024
1 parent e793de3 commit 4cc8427
Showing 1 changed file with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ import sttp.client4._
import sttp.model._

import scala.collection.JavaConverters._
import sttp.client4.compression.Compressor
import sttp.client4.compression.DeflateDefaultCompressor
import sttp.client4.compression.GZipDefaultCompressor

abstract class OkHttpBackend[F[_], S <: Streams[S], P](
client: OkHttpClient,
Expand All @@ -33,6 +36,8 @@ abstract class OkHttpBackend[F[_], S <: Streams[S], P](
val streams: Streams[S]
type R = P with Effect[F]

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

override def send[T](request: GenericRequest[T, R]): F[Response[T]] =
adjustExceptions(request.isWebSocket, request) {
if (request.isWebSocket) {
Expand All @@ -54,7 +59,8 @@ abstract class OkHttpBackend[F[_], S <: Streams[S], P](
val builder = new OkHttpRequest.Builder()
.url(request.uri.toString)

val body = bodyToOkHttp(request.body, request.contentType, request.contentLength)
val (maybeCompressedBody, contentLength) = Compressor.compressIfNeeded(request, compressors)
val body = bodyToOkHttp(maybeCompressedBody, request.contentType, contentLength)
builder.method(
request.method.method,
body.getOrElse {
Expand All @@ -64,7 +70,13 @@ abstract class OkHttpBackend[F[_], S <: Streams[S], P](
}
)

request.headers.foreach(header => builder.addHeader(header.name, header.value))
// the content-length header's value might have changed due to compression
request.headers.foreach(header =>
if (!header.is(HeaderNames.ContentLength)) {
val _ = builder.addHeader(header.name, header.value)
}
)
contentLength.foreach(cl => builder.addHeader(HeaderNames.ContentLength, cl.toString))

builder.build()
}
Expand Down

0 comments on commit 4cc8427

Please sign in to comment.