Skip to content

Commit 02863d2

Browse files
committed
Remodeled towards a blocking HTTP call. Makes sense for the CLI actually.
1 parent 430b2b8 commit 02863d2

File tree

2 files changed

+46
-20
lines changed

2 files changed

+46
-20
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package de.upb.cs.swt.delphi.cli
2+
3+
import akka.actor.ActorSystem
4+
import akka.http.scaladsl.Http
5+
import akka.http.scaladsl.model.{HttpMethods, HttpRequest, HttpResponse, Uri}
6+
import akka.stream.{ActorMaterializer, ActorMaterializerSettings}
7+
import akka.util.ByteString
8+
9+
import scala.concurrent.{Await, Future}
10+
import scala.concurrent.duration.Duration
11+
import scala.util.{Failure, Success, Try}
12+
13+
/**
14+
* Created by benhermann on 12.02.18.
15+
*/
16+
object BlockingHttpClient {
17+
18+
def doGet(uri : Uri) : Try[String] = {
19+
implicit val system = ActorSystem()
20+
implicit val executionContext = system.dispatcher
21+
implicit val materializer: ActorMaterializer = ActorMaterializer(ActorMaterializerSettings(system))
22+
23+
try {
24+
val req: Future[HttpResponse] = Http(system).singleRequest(HttpRequest(method = HttpMethods.GET, uri = uri))
25+
Await.result(req, Duration.Inf)
26+
27+
val f = req.value.get.get.entity.dataBytes.runFold(ByteString(""))(_ ++ _)
28+
Await.result(f, Duration.Inf)
29+
30+
Success(f.value.get.get.utf8String)
31+
} catch {
32+
case e : Exception => Failure(e)
33+
} finally {
34+
system.terminate()
35+
}
36+
37+
}
38+
39+
}
40+

src/main/scala/de/upb/cs/swt/delphi/cli/TestCommand.scala

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,17 @@ import scala.util.{Failure, Success}
1515
object TestCommand extends Command {
1616
override def execute(config: Config): Unit = {
1717

18-
implicit val system = ActorSystem()
19-
implicit val executionContext = system.dispatcher
20-
implicit val materializer: ActorMaterializer = ActorMaterializer(ActorMaterializerSettings(system))
21-
2218
val uri = Uri(config.server)
2319
println(s"Contacting server ${uri}...")
24-
val resp = Http().singleRequest(HttpRequest(uri = uri.withPath(uri.path + "/version")))
20+
val resp = BlockingHttpClient.doGet(uri.withPath(uri.path + "/version"))
2521

26-
resp.onComplete {
27-
case Success(res) =>
28-
res.status match {
29-
case x if x.isSuccess() => {
30-
res.entity.dataBytes.runFold(ByteString(""))(_ ++ _).foreach { body =>
31-
println("Successfully contacted Delphi server. ")
32-
println("Server version: " + body.utf8String)
33-
} }
34-
case _ => {
35-
println(s"Could not validate server ${config.server}. Error: ${res.status}.")
36-
}
37-
}
38-
system.terminate()
22+
resp match {
23+
case Success(res) => {
24+
println("Successfully contacted Delphi server. ")
25+
println("Server version: " + res)
26+
}
3927
case Failure(_) => {
4028
println(s"Could not reach server ${config.server}.")
41-
42-
system.terminate()
4329
}
4430
}
4531

0 commit comments

Comments
 (0)