-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathHttpClient.scala
48 lines (38 loc) · 1.11 KB
/
HttpClient.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package app.impl.finagle
import com.twitter.finagle.http.{Request, Response}
import com.twitter.finagle.{Http, Service, http}
import com.twitter.util.{Await, Future}
/**
* Created by pabloperezgarcia on 08/04/2017.
*
* A really easy way to implement a client without almost any code
* The Service class will receive and response a Future[Response] the type that you specify
* Service[Req,Rep]
*/
object HttpClient extends App {
HttpServers.start()
Thread.sleep(1000)
println("Running client")
val client: Service[Request, Response] = Http.newService("localhost:1982")
val request = http.Request(http.Method.Get, "/")
val response = makeRequest
defineOnFailure
defineOnSuccess
private val result = Await.result(response)
println(result)
private def makeRequest: Future[Response] = {
client(request)
}
private def defineOnFailure = {
response.onFailure { t =>
print(s"Error:${t.getMessage}")
makeRequest
}
}
private def defineOnSuccess = {
response.onSuccess { rep =>
print("Response: " + rep)
println(s"Header:${rep.headerMap}")
}
}
}