-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleRestJsonCurlClient.scala
84 lines (73 loc) · 2.33 KB
/
SimpleRestJsonCurlClient.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package smithy4s_curl
import smithy4s.Endpoint.Middleware
import smithy4s.client.*
import smithy4s.http.{HttpRequest, HttpResponse}
import smithy4s.{Blob, Endpoint}
import scala.util.Try
class SimpleRestJsonCurlClient[
Alg[_[_, _, _, _, _]]
] private[smithy4s_curl] (
service: smithy4s.Service[Alg],
uri: String,
client: SyncCurlClient,
middleware: Endpoint.Middleware[SyncCurlClient],
codecs: SimpleRestJsonCodecs
):
def withMaxArity(maxArity: Int): SimpleRestJsonCurlClient[Alg] =
changeCodecs(_.copy(maxArity = maxArity))
def withExplicitDefaultsEncoding(
explicitDefaultsEncoding: Boolean
): SimpleRestJsonCurlClient[Alg] =
changeCodecs(_.copy(explicitDefaultsEncoding = explicitDefaultsEncoding))
def withHostPrefixInjection(
hostPrefixInjection: Boolean
): SimpleRestJsonCurlClient[Alg] =
changeCodecs(_.copy(hostPrefixInjection = hostPrefixInjection))
def make: Alg[[I, E, O, SI, SO] =>> Try[O]] =
service.impl[Try](
UnaryClientCompiler[
Alg,
Try,
SyncCurlClient,
smithy4s.http.HttpRequest[Blob],
smithy4s.http.HttpResponse[Blob]
](
service = service,
toSmithy4sClient = SimpleRestJsonCurlClient.lowLevelClient(_),
client = client,
middleware = middleware,
makeClientCodecs = codecs.makeClientCodecs(uri),
isSuccessful = _.isSuccessful
)
)
private def changeCodecs(
f: SimpleRestJsonCodecs => SimpleRestJsonCodecs
): SimpleRestJsonCurlClient[Alg] =
new SimpleRestJsonCurlClient(
service,
uri,
client,
middleware,
f(codecs)
)
end SimpleRestJsonCurlClient
object SimpleRestJsonCurlClient:
def apply[Alg[_[_, _, _, _, _]]](
service: smithy4s.Service[Alg],
url: String,
client: SyncCurlClient
) =
new SimpleRestJsonCurlClient(
service = service,
uri = url,
client = client,
codecs = SimpleRestJsonCodecs,
middleware = Endpoint.Middleware.noop
)
private def lowLevelClient(fetch: SyncCurlClient) =
new UnaryLowLevelClient[Try, HttpRequest[Blob], HttpResponse[Blob]]:
override def run[Output](request: HttpRequest[Blob])(
responseCB: HttpResponse[Blob] => Try[Output]
): Try[Output] =
fetch.send(request).flatMap(responseCB)
end SimpleRestJsonCurlClient