This repository has been archived by the owner on Oct 10, 2023. It is now read-only.
generated from JannikArndt/scala-start
-
Notifications
You must be signed in to change notification settings - Fork 1
/
SimpleExample.scala
50 lines (43 loc) · 1.86 KB
/
SimpleExample.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
49
50
package io.moia.scalaHttpClient
import akka.actor.typed.ActorSystem
import akka.actor.typed.scaladsl.Behaviors
import akka.http.scaladsl.model._
import akka.http.scaladsl.unmarshalling.{Unmarshal, Unmarshaller}
import io.moia.scalaHttpClient.ExampleModel.{DomainErrorObject, GatewayException, MySuccessObject}
import java.time.Clock
import scala.concurrent.duration._
import scala.concurrent.{ExecutionContext, Future}
object ExampleModel {
final case class MySuccessObject(foo: String)
final case class DomainErrorObject(errorMessage: String)
final case class GatewayException(msg: String) extends RuntimeException(msg)
}
object SimpleExample {
implicit val system: ActorSystem[_] = ActorSystem(Behaviors.empty, "test")
implicit val executionContext: ExecutionContext = system.executionContext
implicit val um1: Unmarshaller[HttpResponse, MySuccessObject] = ???
implicit val um2: Unmarshaller[HttpResponse, DomainErrorObject] = ???
// create the client
val httpClient = new HttpClient(
config = HttpClientConfig("http", "127.0.0.1", 8888),
name = "TestClient",
httpMetrics = HttpMetrics.none,
retryConfig = RetryConfig.default,
clock = Clock.systemUTC(),
awsRequestSigner = None
)
// make a request
val response: Future[HttpClientResponse] = httpClient.request(
method = HttpMethods.POST,
entity = HttpEntity("Example"),
path = "/test",
headers = Seq.empty,
deadline = Deadline.now + 10.seconds
)
// map the response to your model
response.flatMap {
case HttpClientSuccess(content) => Unmarshal(content).to[MySuccessObject].map(Right(_))
case DomainError(content) => Unmarshal(content).to[DomainErrorObject].map(Left(_))
case failure: HttpClientFailure => throw GatewayException(failure.toString)
}
}