diff --git a/build.sbt b/build.sbt index 52eae0e8a..eee4fa940 100644 --- a/build.sbt +++ b/build.sbt @@ -976,6 +976,7 @@ lazy val examples = (projectMatrix in file("examples")) "com.github.plokhotnyuk.jsoniter-scala" %%% "jsoniter-scala-macros" % jsoniterVersion, "io.github.resilience4j" % "resilience4j-circuitbreaker" % resilience4jVersion, "io.github.resilience4j" % "resilience4j-ratelimiter" % resilience4jVersion, + "com.lihaoyi" %% "os-lib" % "0.11.3", pekkoStreams, logback ), diff --git a/examples/src/main/resources/some-text-file.txt b/examples/src/main/resources/some-text-file.txt new file mode 100644 index 000000000..ce249e84e --- /dev/null +++ b/examples/src/main/resources/some-text-file.txt @@ -0,0 +1 @@ +JUST SIMPLE TEXT FILE WITH SOME TEXT CONTENT \ No newline at end of file diff --git a/examples/src/main/scala/sttp/client4/examples/DowloadFileWitOsLib.scala b/examples/src/main/scala/sttp/client4/examples/DowloadFileWitOsLib.scala new file mode 100644 index 000000000..4fc276438 --- /dev/null +++ b/examples/src/main/scala/sttp/client4/examples/DowloadFileWitOsLib.scala @@ -0,0 +1,18 @@ +package sttp.client4.examples + +import sttp.client4.* +import os.* + +private val fileSize = 8192 +private val dest: os.Path = + os.pwd / "examples" / "src" / "main" / "resources" / s"file-example-$fileSize-bytes" +private val backend: SyncBackend = DefaultSyncBackend() + +@main def downloadFileWithOsLib(): Unit = { + os.remove(dest) + val request = basicRequest + .get(uri"https://httpbin.org/bytes/$fileSize") + .response(asInputStream(i => os.write(dest, i))) + val response = request.send(backend) + println(response.headers) +} diff --git a/examples/src/main/scala/sttp/client4/examples/UploadFileWithOsLib.scala b/examples/src/main/scala/sttp/client4/examples/UploadFileWithOsLib.scala new file mode 100644 index 000000000..41fa02b6e --- /dev/null +++ b/examples/src/main/scala/sttp/client4/examples/UploadFileWithOsLib.scala @@ -0,0 +1,17 @@ +package sttp.client4.examples + +import sttp.client4.* +import os.* + +private val path: os.Path = + os.pwd / "examples" / "src" / "main" / "resources" / "some-text-file.txt" +private val backend: SyncBackend = DefaultSyncBackend() + +@main def uploadFileWithOsLib(): Unit = { + val request = basicRequest + .post(uri"http://httpbin.org/post") + .body(os.read.inputStream(path)) + .response(asString) + val response = request.send(backend) + println(response) +}