Skip to content

Commit

Permalink
Examples of uploading and downloading files with os-lib support. (#2399)
Browse files Browse the repository at this point in the history
adamwkaczmarek authored Jan 14, 2025
1 parent 563aa56 commit 45e75c3
Showing 4 changed files with 71 additions and 0 deletions.
2 changes: 2 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -162,6 +162,7 @@ val zipkinSenderOkHttpVersion = "3.4.3"
val resilience4jVersion = "2.3.0"
val http4s_ce2_version = "0.22.15"
val http4s_ce3_version = "0.23.30"
val osLibVersion = "0.11.3"

val tethysVersion = "0.29.3"

@@ -976,6 +977,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" % osLibVersion,
pekkoStreams,
logback
),
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// {cat=Other; effects=Direct; backend=HttpClient}: Command output streaming with os-lib support

//> using dep com.lihaoyi::os-lib:0.11.3
//> using dep com.softwaremill.sttp.client4::core:4.0.0-M22

package sttp.client4.examples

import sttp.client4.*
import os.*

private val backend: SyncBackend = DefaultSyncBackend()
private val path: os.Path = os.Path("/tmp/example-file.txt")

@main def cmdOutputStreamingWithOsLib(): Unit = {
os.remove(path)
os.write(path, "CONTENT OF THE SIMPLE FILE USED IN THIS EXAMPLE")
val process = os.proc("cat", path.toString).spawn()
val request = basicRequest
.post(uri"http://httpbin.org/post")
.body(process.stdout.wrapped)
.response(asString)
val response = request.send(backend)
println(response)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// {cat=Other; effects=Direct; backend=HttpClient}: Download file with os-lib support

//> using dep com.lihaoyi::os-lib:0.11.3
//> using dep com.softwaremill.sttp.client4::core:4.0.0-M22

package sttp.client4.examples

import sttp.client4.*
import os.*

private val fileSize = 8192
private val dest: os.Path = os.Path(s"/tmp/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,23 @@
// {cat=Other; effects=Direct; backend=HttpClient}: Download file with os-lib support

//> using dep com.lihaoyi::os-lib:0.11.3
//> using dep com.softwaremill.sttp.client4::core:4.0.0-M22

package sttp.client4.examples

import sttp.client4.*
import os.*

private val path: os.Path = os.Path("/tmp/example-file.txt")
private val backend: SyncBackend = DefaultSyncBackend()

@main def uploadFileWithOsLib(): Unit = {
os.remove(path)
os.write(path, "THIS IS CONTENT OF TEST FILE")
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 45e75c3

Please sign in to comment.