-
Notifications
You must be signed in to change notification settings - Fork 312
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Examples of uploading and downloading files with os-lib support. (#2399)
1 parent
563aa56
commit 45e75c3
Showing
4 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
examples/src/main/scala/sttp/client4/examples/cmdOutputStreamingWithOsLib.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
22 changes: 22 additions & 0 deletions
22
examples/src/main/scala/sttp/client4/examples/downloadFileWitOsLib.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
23 changes: 23 additions & 0 deletions
23
examples/src/main/scala/sttp/client4/examples/uploadFileWithOsLib.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |