Skip to content

Commit

Permalink
Akka & Pekko backends
Browse files Browse the repository at this point in the history
  • Loading branch information
adamw committed Jan 21, 2025
1 parent 2dda137 commit 78fb6c3
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,19 @@ class AkkaHttpBackend private (
val body = bodyFromAkka(
r.response,
responseMetadata,
wsFlow.map(Right(_)).getOrElse(Left(decodeAkkaResponse(hr, r.autoDecompressionEnabled)))
wsFlow
.map(Right(_))
.getOrElse(
Left(decodeAkkaResponse(limitPekkoResponseIfNeeded(hr, r.maxResponseBodyLength), r.autoDecompressionEnabled))
)
)

body.map(sttp.client4.Response(_, code, statusText, headers, Nil, r.onlyMetadata))
}

private def limitPekkoResponseIfNeeded(response: HttpResponse, limit: Option[Long]): HttpResponse =
limit.fold(response)(l => response.withEntity(response.entity.withSizeLimit(l)))

// http://doc.akka.io/docs/akka-http/10.0.7/scala/http/common/de-coding.html
private def decodeAkkaResponse(response: HttpResponse, autoDecompressionEnabled: Boolean): HttpResponse =
if (!response.status.allowsEntity() || !autoDecompressionEnabled) response
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sttp.client4.akkahttp

import akka.http.scaladsl.model.HttpResponse
import akka.http.scaladsl.model.EntityStreamSizeException
import sttp.client4.{GenericRequest, SttpClientException}
import sttp.model.{Header, HeaderNames}

Expand All @@ -25,6 +26,7 @@ private[akkahttp] object FromAkka {
case _ => Some(new SttpClientException.ReadException(request, e))
}
case e: akka.stream.scaladsl.TcpIdleTimeoutException => Some(new SttpClientException.TimeoutException(request, e))
case e: EntityStreamSizeException => Some(new SttpClientException.ReadException(request, e))
case e: Exception => SttpClientException.defaultExceptionToSttpClientException(request, e)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sttp.client4.pekkohttp

import org.apache.pekko
import pekko.http.scaladsl.model.HttpResponse
import pekko.http.scaladsl.model.EntityStreamSizeException
import sttp.client4.{GenericRequest, SttpClientException}
import sttp.model.{Header, HeaderNames}

Expand All @@ -27,6 +28,7 @@ private[pekkohttp] object FromPekko {
}
case e: pekko.stream.scaladsl.TcpIdleTimeoutException =>
Some(new SttpClientException.TimeoutException(request, e))
case e: Exception => SttpClientException.defaultExceptionToSttpClientException(request, e)
case e: EntityStreamSizeException => Some(new SttpClientException.ReadException(request, e))
case e: Exception => SttpClientException.defaultExceptionToSttpClientException(request, e)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,19 @@ class PekkoHttpBackend private (
val body = bodyFromPekko(
r.response,
responseMetadata,
wsFlow.map(Right(_)).getOrElse(Left(decodePekkoResponse(hr, r.autoDecompressionEnabled)))
wsFlow
.map(Right(_))
.getOrElse(
Left(decodePekkoResponse(limitPekkoResponseIfNeeded(hr, r.maxResponseBodyLength), r.autoDecompressionEnabled))
)
)

body.map(sttp.client4.Response(_, code, statusText, headers, Nil, r.onlyMetadata))
}

private def limitPekkoResponseIfNeeded(response: HttpResponse, limit: Option[Long]): HttpResponse =
limit.fold(response)(l => response.withEntity(response.entity.withSizeLimit(l)))

// http://doc.akka.io/docs/akka-http/10.0.7/scala/http/common/de-coding.html
private def decodePekkoResponse(response: HttpResponse, enableAutoDecompression: Boolean): HttpResponse =
if (!response.status.allowsEntity() || !enableAutoDecompression) response
Expand Down

0 comments on commit 78fb6c3

Please sign in to comment.