Effectful Light-Weight Scala HttpClient
Generate a effectful response from a given http request
# Define your entities
case class Payload(value: String)
case class Response(value: String)
# Define unit/identity function
implicit def unit[T]: T => Future[T] = t => Future(t)
# Create your request
val request = HttpRequest(
Post,
"https://localhost:8080/lalal",
Map("aHeader" -> "value"),
Payload("abc")
)
val future: Future[Either[Throwable, HttpResponse[Response]]] = HttpRequestSender.send(request)
# Get you response
val eitherResponse: Either[Throwable, HttpResponse[Response]] = Await.result(future, 5 seconds)
eitherResponse.right.get.Entity
# Create Xml Request
val request = XmlHttpRequest(
Post,
"https://localhost:8080/lalal",
Map("aHeader" -> "value"),
Payload("abc")
)
val future: Future[Either[Throwable, HttpResponse[Response]]] = HttpRequestSender.send(request)
# Get you response
val eitherResponse: Either[Throwable, HttpResponse[Response]] = result(future, 5 seconds)
eitherResponse.right.get.Entity
# Create Json Request
val request = JsonHttpRequest(
Post,
"https://localhost:8080/lalal",
Map("aHeader" -> "value"),
Payload("abc")
)
# Use a different Monad
implicit def unit[T]: T => Option[T] = t => Some(t)
val option: Option[Either[Throwable, HttpResponse[Response]]] = HttpRequestSender.send(request)
# Get you response
option.get.right.get.Entity