Skip to content
This repository has been archived by the owner on Sep 4, 2023. It is now read-only.

Latest commit

 

History

History
76 lines (47 loc) · 1.83 KB

README.md

File metadata and controls

76 lines (47 loc) · 1.83 KB

HttpClient

Effectful Light-Weight Scala HttpClient

Build Status

Maven Central

Usage

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