-
Notifications
You must be signed in to change notification settings - Fork 311
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add request body compression support (#2381)
- Loading branch information
Showing
71 changed files
with
1,431 additions
and
585 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
akka-http-backend/src/main/scala/sttp/client4/akkahttp/AkkaCompressor.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package sttp.client4.akkahttp | ||
|
||
import akka.util.ByteString | ||
import akka.stream.scaladsl.Compression | ||
import sttp.capabilities.akka.AkkaStreams | ||
import akka.stream.scaladsl.Source | ||
import sttp.client4._ | ||
import sttp.client4.compression.DeflateDefaultCompressor | ||
import sttp.client4.compression.GZipDefaultCompressor | ||
import sttp.client4.compression.Compressor | ||
import akka.stream.scaladsl.StreamConverters | ||
import akka.stream.scaladsl.FileIO | ||
|
||
trait AkkaCompressor extends Compressor[AkkaStreams] { | ||
override abstract def apply[R2 <: AkkaStreams](body: GenericRequestBody[R2]): GenericRequestBody[AkkaStreams] = | ||
body match { | ||
case InputStreamBody(b, _) => StreamBody(AkkaStreams)(compressStream(StreamConverters.fromInputStream(() => b))) | ||
case StreamBody(b) => StreamBody(AkkaStreams)(compressStream(b.asInstanceOf[Source[ByteString, Any]])) | ||
case FileBody(f, _) => StreamBody(AkkaStreams)(compressStream(FileIO.fromPath(f.toPath))) | ||
case _ => super.apply(body) | ||
} | ||
|
||
def compressStream(stream: Source[ByteString, Any]): Source[ByteString, Any] | ||
} | ||
|
||
object GZipAkkaCompressor extends GZipDefaultCompressor[AkkaStreams] with AkkaCompressor { | ||
def compressStream(stream: Source[ByteString, Any]): Source[ByteString, Any] = stream.via(Compression.gzip) | ||
} | ||
|
||
object DeflateAkkaCompressor extends DeflateDefaultCompressor[AkkaStreams] with AkkaCompressor { | ||
def compressStream(stream: Source[ByteString, Any]): Source[ByteString, Any] = stream.via(Compression.deflate) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
akka-http-backend/src/main/scala/sttp/client4/akkahttp/akkaDecompressors.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package sttp.client4.akkahttp | ||
|
||
import sttp.client4.compression.Decompressor | ||
import sttp.model.Encodings | ||
import akka.http.scaladsl.coding.Coders | ||
import akka.http.scaladsl.model.HttpResponse | ||
|
||
object GZipAkkaDecompressor extends Decompressor[HttpResponse] { | ||
override val encoding: String = Encodings.Gzip | ||
override def apply(body: HttpResponse): HttpResponse = Coders.Gzip.decodeMessage(body) | ||
} | ||
|
||
object DeflateAkkaDecompressor extends Decompressor[HttpResponse] { | ||
override val encoding: String = Encodings.Deflate | ||
override def apply(body: HttpResponse): HttpResponse = Coders.Deflate.decodeMessage(body) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.