Last active
May 25, 2024 10:19
-
-
Save dacr/175623e5edf658a60b743e9353acc3c1 to your computer and use it in GitHub Desktop.
Simplest sttp http client library basic usage examples. / published by https://github.com/dacr/code-examples-manager #52677756-47e4-413e-a4f1-14c7851d007c/82fbef4ae8893b678955e05fff5583740f87a5c5
This file contains hidden or 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
// summary : Simplest sttp http client library basic usage examples. | |
// keywords : scala, sttp, http-client, @testable | |
// publish : gist | |
// authors : David Crosson | |
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2) | |
// id : 52677756-47e4-413e-a4f1-14c7851d007c | |
// created-on : 2021-05-28T16:32:20Z | |
// managed-by : https://github.com/dacr/code-examples-manager | |
// run-with : scala-cli $file | |
// --------------------- | |
//> using scala "3.4.2" | |
//> using dep "com.softwaremill.sttp.client3::core:3.7.0" | |
//> using dep "org.scalatest::scalatest:3.2.16" | |
//> using objectWrapper | |
// --------------------- | |
import org.scalatest.* | |
import flatspec.* | |
import matchers.* | |
import OptionValues.* | |
import sttp.client3.* | |
import java.io.IOException | |
import scala.concurrent.Future | |
class SttpSpec extends AsyncFlatSpec with should.Matchers { | |
override def suiteName = "SttpSpec" | |
// ----------------------------------------------------------------------------------------------- | |
"quick sttp" should "be able to execute get requests" in Future { | |
val response = | |
quickRequest | |
.get(uri"http://mapland.fr/ip") | |
.response(asStringAlways) | |
.send(quick.backend) | |
response.body should include("\"ip\"") | |
} | |
// ----------------------------------------------------------------------------------------------- | |
"basic sttp" should "be able to execute get requests" in Future { | |
val backend = HttpURLConnectionBackend() | |
val response = | |
basicRequest | |
.get(uri"http://mapland.fr/clientInfo") | |
.response(asString) | |
.send(backend) | |
response.body.toOption.value should include("\"userAgent\"") | |
} | |
// ----------------------------------------------------------------------------------------------- | |
it should "allow asynchronous request handling using futures" in { | |
val backend = HttpClientFutureBackend() | |
val responseFuture = | |
basicRequest | |
.get(uri"http://mapland.fr/clientInfo") | |
.response(asString) | |
.send(backend) | |
responseFuture.map( | |
_.body.toOption.value should include("\"userAgent\"") | |
) | |
} | |
// ----------------------------------------------------------------------------------------------- | |
it should "be easy to get request execution issues" in { | |
val backend = HttpClientFutureBackend() | |
basicRequest | |
.get(uri"https://httpbin.org/status/404") | |
.response(asString) | |
.send(backend) | |
.map(_.code.code shouldBe 404) | |
basicRequest | |
.get(uri"https://truch.blah.gla") | |
.response(asString) | |
.send(backend) | |
.map(_ => fail("code not reachable")) | |
.recoverWith{case x:Exception => succeed } | |
} | |
} | |
org.scalatest.tools.Runner.main(Array("-oDF", "-s", classOf[SttpSpec].getName)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment