Skip to content

Commit

Permalink
JS/native compat
Browse files Browse the repository at this point in the history
  • Loading branch information
adamw committed Dec 27, 2024
1 parent 4e81207 commit 68363ba
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 16 deletions.
7 changes: 3 additions & 4 deletions core/src/main/scala/sttp/client4/compression/Compressor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import sttp.client4._
import sttp.model.Encodings

import Compressor._
import java.io.FileInputStream
import java.nio.ByteBuffer
import java.util.zip.DeflaterInputStream
import java.util.zip.Deflater
Expand All @@ -30,7 +29,7 @@ class GZipDefaultCompressor[R] extends Compressor[R] {
InputStreamBody(new GZIPCompressingInputStream(b), defaultContentType)
case StreamBody(b) => streamsNotSupported
case FileBody(f, defaultContentType) =>
InputStreamBody(new GZIPCompressingInputStream(new FileInputStream(f.toFile)), defaultContentType)
InputStreamBody(new GZIPCompressingInputStream(f.openStream()), defaultContentType)
case MultipartStreamBody(parts) => compressingMultipartBodiesNotSupported
case BasicMultipartBody(parts) => compressingMultipartBodiesNotSupported
}
Expand Down Expand Up @@ -59,7 +58,7 @@ class DeflateDefaultCompressor[R] extends Compressor[R] {
InputStreamBody(new DeflaterInputStream(b), defaultContentType)
case StreamBody(b) => streamsNotSupported
case FileBody(f, defaultContentType) =>
InputStreamBody(new DeflaterInputStream(new FileInputStream(f.toFile)), defaultContentType)
InputStreamBody(new DeflaterInputStream(f.openStream()), defaultContentType)
case MultipartStreamBody(parts) => compressingMultipartBodiesNotSupported
case BasicMultipartBody(parts) => compressingMultipartBodiesNotSupported
}
Expand Down Expand Up @@ -114,7 +113,7 @@ private[client4] object Compressor {
case ByteArrayBody(b, _) => Some(b.length.toLong)
case ByteBufferBody(b, _) => None
case InputStreamBody(b, _) => None
case FileBody(f, _) => Some(f.toFile.length())
case FileBody(f, _) => Some(f.length())
case StreamBody(_) => None
case MultipartStreamBody(parts) => None
case BasicMultipartBody(parts) => None
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/scala/sttp/client4/testing/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ package object testing {
case ByteArrayBody(b, _) => new String(b)
case ByteBufferBody(b, _) => new String(b.array())
case InputStreamBody(b, _) => new String(toByteArray(b))
case FileBody(f, _) => f.readAsString
case FileBody(f, _) => f.readAsString()
case StreamBody(_) =>
throw new IllegalArgumentException("The body of this request is a stream, cannot convert to String")
case _: MultipartBody[_] =>
Expand All @@ -32,7 +32,7 @@ package object testing {
case ByteArrayBody(b, _) => b
case ByteBufferBody(b, _) => b.array()
case InputStreamBody(b, _) => toByteArray(b)
case FileBody(f, _) => f.readAsByteArray
case FileBody(f, _) => f.readAsByteArray()
case StreamBody(_) =>
throw new IllegalArgumentException("The body of this request is a stream, cannot convert to String")
case _: MultipartBody[_] =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ package sttp.client4.internal

import org.scalajs.dom.File

import java.io.FileInputStream
import java.io.InputStream

// wrap a DomFile
trait SttpFileExtensions { self: SttpFile =>

def toDomFile: File = underlying.asInstanceOf[File]

def readAsString: String = throw new UnsupportedOperationException()
def readAsByteArray: Array[Byte] = throw new UnsupportedOperationException()
def readAsString(): String = throw new UnsupportedOperationException()
def readAsByteArray(): Array[Byte] = throw new UnsupportedOperationException()
def openStream(): InputStream = throw new UnsupportedOperationException()
def length(): Long = throw new UnsupportedOperationException()
}

trait SttpFileCompanionExtensions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@ import java.nio.file.Files
import java.nio.file.Path

import scala.io.Source
import java.io.FileInputStream
import java.io.InputStream

// wrap a Path
trait SttpFileExtensions { self: SttpFile =>

def toPath: Path = underlying.asInstanceOf[Path]
def toFile: java.io.File = toPath.toFile

def readAsString: String = {
def readAsString(): String = {
val s = Source.fromFile(toFile, "UTF-8");
try s.getLines().mkString("\n")
finally s.close()
}

def readAsByteArray: Array[Byte] = Files.readAllBytes(toPath)
def readAsByteArray(): Array[Byte] = Files.readAllBytes(toPath)
def openStream(): InputStream = new FileInputStream(toFile)
def length(): Long = toFile.length()
}

trait SttpFileCompanionExtensions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package sttp.client4.internal.httpclient
import sttp.capabilities.Streams
import sttp.client4.internal.SttpToJavaConverters.toJavaSupplier
import sttp.client4.internal.{throwNestedMultipartNotAllowed, Utf8}
import sttp.client4.internal.compression.{Compressor, DeflateDefaultCompressor, GZipDefaultCompressor}
import sttp.client4.compression.{Compressor, DeflateDefaultCompressor, GZipDefaultCompressor}
import sttp.client4._
import sttp.model.{Header, HeaderNames, Part}
import sttp.monad.MonadError
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,21 @@ import java.nio.file.Path

import scala.io.Source

import java.io.FileInputStream
import java.io.InputStream

trait SttpFileExtensions { self: SttpFile =>
def toPath: Path = underlying.asInstanceOf[Path]
def toFile: java.io.File = toPath.toFile

def readAsString: String = {
def readAsString(): String = {
val s = Source.fromFile(toFile, "UTF-8");
try s.getLines().mkString("\n")
finally s.close()
}

def readAsByteArray: Array[Byte] = Files.readAllBytes(toPath)
def readAsByteArray(): Array[Byte] = Files.readAllBytes(toPath)
def openStream(): InputStream = new FileInputStream(toFile)
def length(): Long = toFile.length()
}

trait SttpFileCompanionExtensions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class HttpClientCatsBackend[F[_]: Async] private (

override protected def createSequencer: F[Sequencer[F]] = CatsSequencer.create

override protected val bodyToHttpClient: BodyToHttpClient[F, Nothing, R] = new BodyToHttpClient[F, Nothing, Ra] {
override protected val bodyToHttpClient: BodyToHttpClient[F, Nothing, R] = new BodyToHttpClient[F, Nothing, R] {
override val streams: NoStreams = NoStreams
override implicit val monad: MonadError[F] = self.monad

Expand Down

0 comments on commit 68363ba

Please sign in to comment.