Skip to content

Commit

Permalink
Examples of uploading adn downloading files with os-lib support.
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwkaczmarek committed Jan 14, 2025
1 parent e992977 commit 7a3cdf8
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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
),
Expand Down
1 change: 1 addition & 0 deletions examples/src/main/resources/some-text-file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
JUST SIMPLE TEXT FILE WITH SOME TEXT CONTENT
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 7a3cdf8

Please sign in to comment.