-
Notifications
You must be signed in to change notification settings - Fork 331
Add Scala Native support to http4s module #2874
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
majk-p
wants to merge
7
commits into
softwaremill:master
Choose a base branch
from
majk-p:http4s-scala-native
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a203ca6
Add Scala Native support to http4s module
majk-p 9752f4c
extract compression, resue it acrtoss platforms
majk-p 9425cee
fix build for js by extracting platform specific logic
majk-p 56f1a37
fix mdoc
majk-p 5adca75
drop shared parts of http4s backend to fix mdoc compilation bug
majk-p 8c5fdce
restore Netowrk constraint
majk-p 4da6190
tweak buffer handling, refactor shared code
majk-p File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
69 changes: 69 additions & 0 deletions
69
effects/fs2/src/main/scala/sttp/client4/impl/fs2/fs2Compressor.scala
This file contains hidden or 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,69 @@ | ||
| package sttp.client4.impl.fs2 | ||
|
|
||
| import sttp.client4._ | ||
| import sttp.client4.GenericRequestBody | ||
| import fs2._ | ||
| import fs2.compression.Compression | ||
| import cats.syntax.all._ | ||
| import cats.effect.Sync | ||
| import sttp.capabilities.fs2.Fs2Streams | ||
| import fs2.compression.DeflateParams | ||
| import sttp.client4.compression.Compressor | ||
| import sttp.model.Encodings | ||
|
|
||
| trait Fs2Compressor[F[_], R <: Fs2Streams[F]] extends Compressor[R] { | ||
| protected val fSync: Sync[F] | ||
|
|
||
| override def apply[R2 <: R](body: GenericRequestBody[R2]): GenericRequestBody[R] = | ||
| body match { | ||
| case NoBody => NoBody | ||
| case StringBody(s, encoding, _) => | ||
| StreamBody(Fs2Streams[F])(compressStream(Stream.chunk(Chunk.array(s.getBytes(encoding))))) | ||
| case ByteArrayBody(b, _) => | ||
| StreamBody(Fs2Streams[F])(compressStream(Stream.chunk(Chunk.array(b)))) | ||
| case ByteBufferBody(b, _) => | ||
| val buffer = b.duplicate() | ||
| val bytes = | ||
| if (buffer.hasArray()) buffer.array() | ||
| else { | ||
| val arr = new Array[Byte](buffer.remaining()) | ||
| buffer.get(arr) | ||
| arr | ||
| } | ||
| StreamBody(Fs2Streams[F])(compressStream(Stream.chunk(Chunk.array(bytes)))) | ||
| case InputStreamBody(b, _) => | ||
| compressInputStreamBody(b) | ||
| case StreamBody(b) => | ||
| StreamBody(Fs2Streams[F])(compressStream(b.asInstanceOf[Stream[F, Byte]])) | ||
| case fb @ FileBody(_, _) => | ||
| compressFileBody(fb) | ||
| case MultipartStreamBody(_) | BasicMultipartBody(_) => | ||
| throw new IllegalArgumentException("Multipart bodies cannot be compressed") | ||
| } | ||
|
|
||
| protected def compressInputStreamBody(b: java.io.InputStream): GenericRequestBody[R] = | ||
| throw new UnsupportedOperationException("InputStream compression is not supported on this platform") | ||
|
|
||
| protected def compressFileBody(f: FileBody): GenericRequestBody[R] = | ||
| throw new UnsupportedOperationException("File compression is not supported on this platform") | ||
|
|
||
| def compressStream(stream: Stream[F, Byte]): Stream[F, Byte] | ||
| } | ||
|
|
||
| class GZipFs2Compressor[F[_]: Compression: Sync, R <: Fs2Streams[F]] extends Fs2Compressor[F, R] { | ||
|
|
||
| override protected val fSync: Sync[F] = implicitly | ||
| override val encoding: String = Encodings.Gzip | ||
|
|
||
| def compressStream(stream: Stream[F, Byte]): Stream[F, Byte] = | ||
| stream.through(fs2.compression.Compression[F].gzip()) | ||
| } | ||
|
|
||
| class DeflateFs2Compressor[F[_]: Compression: Sync, R <: Fs2Streams[F]] extends Fs2Compressor[F, R] { | ||
|
|
||
| override protected val fSync: Sync[F] = implicitly | ||
| override val encoding: String = Encodings.Deflate | ||
|
|
||
| def compressStream(stream: Stream[F, Byte]): Stream[F, Byte] = | ||
| stream.through(fs2.compression.Compression[F].deflate(DeflateParams())) | ||
| } |
File renamed without changes.
This file contains hidden or 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
48 changes: 0 additions & 48 deletions
48
effects/fs2/src/main/scalajvm/sttp/client4/impl/fs2/fs2Compressor.scala
This file was deleted.
Oops, something went wrong.
27 changes: 27 additions & 0 deletions
27
effects/fs2/src/main/scalajvmnative/sttp/client4/impl/fs2/fs2CompressorPlatform.scala
This file contains hidden or 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,27 @@ | ||
| package sttp.client4.impl.fs2 | ||
|
|
||
| import fs2.Stream | ||
| import fs2.io.file.Files | ||
| import cats.effect.Sync | ||
| import sttp.capabilities.fs2.Fs2Streams | ||
| import sttp.client4._ | ||
|
|
||
| abstract class PlatformGZipFs2Compressor[F[_]: Sync: Files, R <: Fs2Streams[F]] | ||
| extends GZipFs2Compressor[F, R] { | ||
|
|
||
| override protected def compressInputStreamBody(b: java.io.InputStream): GenericRequestBody[R] = | ||
| StreamBody(Fs2Streams[F])(compressStream(fs2.io.readInputStream(fSync.delay(b), 1024)(fSync))) | ||
|
|
||
| override protected def compressFileBody(fb: FileBody): GenericRequestBody[R] = | ||
| StreamBody(Fs2Streams[F])(compressStream(Files[F].readAll(fb.f.toPath, 1024))) | ||
| } | ||
|
|
||
| abstract class PlatformDeflateFs2Compressor[F[_]: Sync: Files, R <: Fs2Streams[F]] | ||
| extends DeflateFs2Compressor[F, R] { | ||
|
|
||
| override protected def compressInputStreamBody(b: java.io.InputStream): GenericRequestBody[R] = | ||
| StreamBody(Fs2Streams[F])(compressStream(fs2.io.readInputStream(fSync.delay(b), 1024)(fSync))) | ||
|
|
||
| override protected def compressFileBody(fb: FileBody): GenericRequestBody[R] = | ||
| StreamBody(Fs2Streams[F])(compressStream(Files[F].readAll(fb.f.toPath, 1024))) | ||
| } |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -23,9 +23,9 @@ import sttp.client4.ws.{GotAWebSocketException, NotAWebSocketException} | |
| import sttp.client4._ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. doesn't this move remove the backend for JS? |
||
| import sttp.client4.wrappers.FollowRedirectsBackend | ||
| import sttp.client4.compression.Compressor | ||
| import sttp.client4.impl.fs2.GZipFs2Compressor | ||
| import sttp.client4.impl.fs2.DeflateFs2Compressor | ||
| import sttp.client4.compression.CompressionHandlers | ||
| import sttp.client4.impl.fs2.PlatformGZipFs2Compressor | ||
| import sttp.client4.impl.fs2.PlatformDeflateFs2Compressor | ||
| import sttp.client4.impl.fs2.GZipFs2Decompressor | ||
| import sttp.client4.impl.fs2.DeflateFs2Decompressor | ||
| import sttp.client4.compression.Decompressor | ||
|
|
@@ -285,25 +285,36 @@ class Http4sBackend[F[_]: Async]( | |
| } | ||
|
|
||
| object Http4sBackend { | ||
|
|
||
| def defaultCompressionHandlers[F[_]: Async]: CompressionHandlers[Fs2Streams[F], Stream[F, Byte]] = | ||
| CompressionHandlers( | ||
| List(new GZipFs2Compressor[F, Fs2Streams[F]](), new DeflateFs2Compressor[F, Fs2Streams[F]]()), | ||
| List(new PlatformGZipFs2Compressor[F, Fs2Streams[F]] {}, new PlatformDeflateFs2Compressor[F, Fs2Streams[F]] {}), | ||
| List(new GZipFs2Decompressor, new DeflateFs2Decompressor) | ||
| ) | ||
|
|
||
| def usingClient[F[_]: Async]( | ||
| client: Client[F], | ||
| customizeRequest: Http4sRequest[F] => Http4sRequest[F] = identity[Http4sRequest[F]] _, | ||
| compressionHandlers: Async[F] => CompressionHandlers[Fs2Streams[F], EntityBody[F]] = | ||
| defaultCompressionHandlers[F](_: Async[F]) | ||
| client: org.http4s.client.Client[F], | ||
| customizeRequest: Http4sRequest[F] => Http4sRequest[F] = identity[Http4sRequest[F]] _ | ||
| ): StreamBackend[F, Fs2Streams[F]] = | ||
| usingClient(client, customizeRequest, defaultCompressionHandlers[F](_: Async[F])) | ||
|
|
||
| def usingClient[F[_]: Async]( | ||
| client: org.http4s.client.Client[F], | ||
| customizeRequest: Http4sRequest[F] => Http4sRequest[F], | ||
| compressionHandlers: Async[F] => CompressionHandlers[Fs2Streams[F], EntityBody[F]] | ||
| ): StreamBackend[F, Fs2Streams[F]] = | ||
| FollowRedirectsBackend(new Http4sBackend[F](client, customizeRequest, compressionHandlers(implicitly))) | ||
|
|
||
| def usingBlazeClientBuilder[F[_]: Async]( | ||
| blazeClientBuilder: BlazeClientBuilder[F], | ||
| customizeRequest: Http4sRequest[F] => Http4sRequest[F] = identity[Http4sRequest[F]] _, | ||
| compressionHandlers: Async[F] => CompressionHandlers[Fs2Streams[F], EntityBody[F]] = | ||
| defaultCompressionHandlers[F](_: Async[F]) | ||
| customizeRequest: Http4sRequest[F] => Http4sRequest[F] = identity[Http4sRequest[F]] _ | ||
| ): Resource[F, StreamBackend[F, Fs2Streams[F]]] = | ||
| usingBlazeClientBuilder(blazeClientBuilder, customizeRequest, defaultCompressionHandlers[F](_: Async[F])) | ||
|
|
||
| def usingBlazeClientBuilder[F[_]: Async]( | ||
| blazeClientBuilder: BlazeClientBuilder[F], | ||
| customizeRequest: Http4sRequest[F] => Http4sRequest[F], | ||
| compressionHandlers: Async[F] => CompressionHandlers[Fs2Streams[F], EntityBody[F]] | ||
| ): Resource[F, StreamBackend[F, Fs2Streams[F]]] = | ||
| blazeClientBuilder.resource.map(c => usingClient(c, customizeRequest, compressionHandlers)) | ||
|
|
||
|
|
@@ -312,17 +323,18 @@ object Http4sBackend { | |
| compressionHandlers: Async[F] => CompressionHandlers[Fs2Streams[F], EntityBody[F]] = | ||
| defaultCompressionHandlers[F](_: Async[F]) | ||
| ): Resource[F, StreamBackend[F, Fs2Streams[F]]] = | ||
| usingBlazeClientBuilder( | ||
| BlazeClientBuilder[F], | ||
| customizeRequest, | ||
| compressionHandlers | ||
| ) | ||
| usingBlazeClientBuilder(BlazeClientBuilder[F], customizeRequest, compressionHandlers) | ||
|
|
||
| def usingEmberClientBuilder[F[_]: Async]( | ||
| emberClientBuilder: EmberClientBuilder[F], | ||
| customizeRequest: Http4sRequest[F] => Http4sRequest[F] = identity[Http4sRequest[F]] _, | ||
| compressionHandlers: Async[F] => CompressionHandlers[Fs2Streams[F], EntityBody[F]] = | ||
| defaultCompressionHandlers[F](_: Async[F]) | ||
| customizeRequest: Http4sRequest[F] => Http4sRequest[F] = identity[Http4sRequest[F]] _ | ||
| ): Resource[F, StreamBackend[F, Fs2Streams[F]]] = | ||
| usingEmberClientBuilder(emberClientBuilder, customizeRequest, defaultCompressionHandlers[F](_: Async[F])) | ||
|
|
||
| def usingEmberClientBuilder[F[_]: Async]( | ||
| emberClientBuilder: EmberClientBuilder[F], | ||
| customizeRequest: Http4sRequest[F] => Http4sRequest[F], | ||
| compressionHandlers: Async[F] => CompressionHandlers[Fs2Streams[F], EntityBody[F]] | ||
| ): Resource[F, StreamBackend[F, Fs2Streams[F]]] = | ||
| emberClientBuilder.build.map(c => usingClient(c, customizeRequest, compressionHandlers)) | ||
|
|
||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any reason why Platform* classes are abstract?