Skip to content

Commit 68363ba

Browse files
committed
JS/native compat
1 parent 4e81207 commit 68363ba

File tree

7 files changed

+27
-16
lines changed

7 files changed

+27
-16
lines changed

core/src/main/scala/sttp/client4/compression/Compressor.scala

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import sttp.client4._
44
import sttp.model.Encodings
55

66
import Compressor._
7-
import java.io.FileInputStream
87
import java.nio.ByteBuffer
98
import java.util.zip.DeflaterInputStream
109
import java.util.zip.Deflater
@@ -30,7 +29,7 @@ class GZipDefaultCompressor[R] extends Compressor[R] {
3029
InputStreamBody(new GZIPCompressingInputStream(b), defaultContentType)
3130
case StreamBody(b) => streamsNotSupported
3231
case FileBody(f, defaultContentType) =>
33-
InputStreamBody(new GZIPCompressingInputStream(new FileInputStream(f.toFile)), defaultContentType)
32+
InputStreamBody(new GZIPCompressingInputStream(f.openStream()), defaultContentType)
3433
case MultipartStreamBody(parts) => compressingMultipartBodiesNotSupported
3534
case BasicMultipartBody(parts) => compressingMultipartBodiesNotSupported
3635
}
@@ -59,7 +58,7 @@ class DeflateDefaultCompressor[R] extends Compressor[R] {
5958
InputStreamBody(new DeflaterInputStream(b), defaultContentType)
6059
case StreamBody(b) => streamsNotSupported
6160
case FileBody(f, defaultContentType) =>
62-
InputStreamBody(new DeflaterInputStream(new FileInputStream(f.toFile)), defaultContentType)
61+
InputStreamBody(new DeflaterInputStream(f.openStream()), defaultContentType)
6362
case MultipartStreamBody(parts) => compressingMultipartBodiesNotSupported
6463
case BasicMultipartBody(parts) => compressingMultipartBodiesNotSupported
6564
}
@@ -114,7 +113,7 @@ private[client4] object Compressor {
114113
case ByteArrayBody(b, _) => Some(b.length.toLong)
115114
case ByteBufferBody(b, _) => None
116115
case InputStreamBody(b, _) => None
117-
case FileBody(f, _) => Some(f.toFile.length())
116+
case FileBody(f, _) => Some(f.length())
118117
case StreamBody(_) => None
119118
case MultipartStreamBody(parts) => None
120119
case BasicMultipartBody(parts) => None

core/src/main/scala/sttp/client4/testing/package.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ package object testing {
1515
case ByteArrayBody(b, _) => new String(b)
1616
case ByteBufferBody(b, _) => new String(b.array())
1717
case InputStreamBody(b, _) => new String(toByteArray(b))
18-
case FileBody(f, _) => f.readAsString
18+
case FileBody(f, _) => f.readAsString()
1919
case StreamBody(_) =>
2020
throw new IllegalArgumentException("The body of this request is a stream, cannot convert to String")
2121
case _: MultipartBody[_] =>
@@ -32,7 +32,7 @@ package object testing {
3232
case ByteArrayBody(b, _) => b
3333
case ByteBufferBody(b, _) => b.array()
3434
case InputStreamBody(b, _) => toByteArray(b)
35-
case FileBody(f, _) => f.readAsByteArray
35+
case FileBody(f, _) => f.readAsByteArray()
3636
case StreamBody(_) =>
3737
throw new IllegalArgumentException("The body of this request is a stream, cannot convert to String")
3838
case _: MultipartBody[_] =>

core/src/main/scalajs/sttp/client4/internal/SttpFileExtensions.scala

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@ package sttp.client4.internal
22

33
import org.scalajs.dom.File
44

5+
import java.io.FileInputStream
6+
import java.io.InputStream
7+
58
// wrap a DomFile
69
trait SttpFileExtensions { self: SttpFile =>
710

811
def toDomFile: File = underlying.asInstanceOf[File]
912

10-
def readAsString: String = throw new UnsupportedOperationException()
11-
def readAsByteArray: Array[Byte] = throw new UnsupportedOperationException()
13+
def readAsString(): String = throw new UnsupportedOperationException()
14+
def readAsByteArray(): Array[Byte] = throw new UnsupportedOperationException()
15+
def openStream(): InputStream = throw new UnsupportedOperationException()
16+
def length(): Long = throw new UnsupportedOperationException()
1217
}
1318

1419
trait SttpFileCompanionExtensions {

core/src/main/scalajvm/sttp/client4/internal/SttpFileExtensions.scala

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,23 @@ import java.nio.file.Files
44
import java.nio.file.Path
55

66
import scala.io.Source
7+
import java.io.FileInputStream
8+
import java.io.InputStream
79

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

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

14-
def readAsString: String = {
16+
def readAsString(): String = {
1517
val s = Source.fromFile(toFile, "UTF-8");
1618
try s.getLines().mkString("\n")
1719
finally s.close()
1820
}
19-
20-
def readAsByteArray: Array[Byte] = Files.readAllBytes(toPath)
21+
def readAsByteArray(): Array[Byte] = Files.readAllBytes(toPath)
22+
def openStream(): InputStream = new FileInputStream(toFile)
23+
def length(): Long = toFile.length()
2124
}
2225

2326
trait SttpFileCompanionExtensions {

core/src/main/scalajvm/sttp/client4/internal/httpclient/BodyToHttpClient.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package sttp.client4.internal.httpclient
33
import sttp.capabilities.Streams
44
import sttp.client4.internal.SttpToJavaConverters.toJavaSupplier
55
import sttp.client4.internal.{throwNestedMultipartNotAllowed, Utf8}
6-
import sttp.client4.internal.compression.{Compressor, DeflateDefaultCompressor, GZipDefaultCompressor}
6+
import sttp.client4.compression.{Compressor, DeflateDefaultCompressor, GZipDefaultCompressor}
77
import sttp.client4._
88
import sttp.model.{Header, HeaderNames, Part}
99
import sttp.monad.MonadError

core/src/main/scalanative/sttp/client4/internal/SttpFileExtensions.scala

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,21 @@ import java.nio.file.Path
55

66
import scala.io.Source
77

8+
import java.io.FileInputStream
9+
import java.io.InputStream
10+
811
trait SttpFileExtensions { self: SttpFile =>
912
def toPath: Path = underlying.asInstanceOf[Path]
1013
def toFile: java.io.File = toPath.toFile
1114

12-
def readAsString: String = {
15+
def readAsString(): String = {
1316
val s = Source.fromFile(toFile, "UTF-8");
1417
try s.getLines().mkString("\n")
1518
finally s.close()
1619
}
17-
18-
def readAsByteArray: Array[Byte] = Files.readAllBytes(toPath)
20+
def readAsByteArray(): Array[Byte] = Files.readAllBytes(toPath)
21+
def openStream(): InputStream = new FileInputStream(toFile)
22+
def length(): Long = toFile.length()
1923
}
2024

2125
trait SttpFileCompanionExtensions {

effects/cats/src/main/scalajvm/sttp/client4/httpclient/cats/HttpClientCatsBackend.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class HttpClientCatsBackend[F[_]: Async] private (
4040

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

43-
override protected val bodyToHttpClient: BodyToHttpClient[F, Nothing, R] = new BodyToHttpClient[F, Nothing, Ra] {
43+
override protected val bodyToHttpClient: BodyToHttpClient[F, Nothing, R] = new BodyToHttpClient[F, Nothing, R] {
4444
override val streams: NoStreams = NoStreams
4545
override implicit val monad: MonadError[F] = self.monad
4646

0 commit comments

Comments
 (0)