Last active
May 25, 2024 08:38
-
-
Save dacr/1ee46507728c8c3cda4acce710d415cf to your computer and use it in GitHub Desktop.
Fully asynchronous http client call using akka-http will work in all cases, even with chunked responses ! / published by https://github.com/dacr/code-examples-manager #287b4108-ff5a-4abf-af9c-ec3d1422a883/8d8bc69d0de207bba697c28fd920fe2744ff2e9b
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 : Fully asynchronous http client call using akka-http will work in all cases, even with chunked responses ! | |
// keywords : scala, actors, akka, http-client, client | |
// 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 : 287b4108-ff5a-4abf-af9c-ec3d1422a883 | |
// created-on : 2018-06-19T19:10:04Z | |
// managed-by : https://github.com/dacr/code-examples-manager | |
// run-with : scala-cli $file | |
// --------------------- | |
//> using scala "2.13.14" | |
//> using dep "com.typesafe.akka::akka-http:10.2.7" | |
//> using dep "com.typesafe.akka::akka-stream:2.6.18" | |
// --------------------- | |
import akka.http.scaladsl._ | |
import akka.http.scaladsl.model._ | |
import akka.http.scaladsl.unmarshalling.Unmarshal | |
import scala.concurrent._ | |
import scala.concurrent.duration._ | |
object TestThat { | |
implicit val system = akka.actor.ActorSystem("MySystem") | |
implicit val executionContext = system.dispatcher | |
val uri = s"http://www.chezmoicamarche.fr" | |
val futureResponse = Http().singleRequest(HttpRequest(uri = uri)) | |
val futureResult = futureResponse.flatMap { response => Unmarshal(response.entity).to[String] } | |
val lastFuture = futureResult.map(println).andThen(_ => system.terminate()) | |
// Do not exit before the future has completed ;) | |
def andWait():Unit = Await.ready(lastFuture, 5.seconds) | |
} | |
TestThat.andWait() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment