-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleRestJsonCodecs.scala
192 lines (161 loc) · 4.94 KB
/
SimpleRestJsonCodecs.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package smithy4s_curl
import curl.all as C
import smithy4s.Blob
import smithy4s.client.*
import smithy4s.codecs.BlobEncoder
import smithy4s.http.HttpUriScheme.{Http, Https}
import smithy4s.http.{
HttpDiscriminator,
HttpMethod,
HttpRequest,
HttpResponse,
HttpUnaryClientCodecs,
HttpUri,
HttpUriScheme,
Metadata
}
import smithy4s.json.Json
import smithy4s_curl.*
import scala.scalanative.unsigned.*
import scala.util.{Failure, Success, Try}
import scalanative.unsafe.*
import util.chaining.*
private[smithy4s_curl] object SimpleRestJsonCodecs
extends SimpleRestJsonCodecs(1024, false, false)
private[smithy4s_curl] case class SimpleRestJsonCodecs(
maxArity: Int,
explicitDefaultsEncoding: Boolean,
hostPrefixInjection: Boolean
):
private val hintMask =
alloy.SimpleRestJson.protocol.hintMask
def fromSmithy4sHttpUri(uri: smithy4s.http.HttpUri): String =
val qp = uri.queryParams
val newValue =
uri.scheme match
case Http => "http"
case Https => "https"
val hostName = uri.host
val port =
uri.port
.filterNot(p => uri.host.endsWith(s":$p"))
.map(":" + _.toString)
.getOrElse("")
val path = "/" + uri.path.mkString("/")
val query =
if qp.isEmpty then ""
else
var b = "?"
qp.zipWithIndex.map:
case ((key, values), idx) =>
if idx != 0 then b += "&"
b += key
for
i <- 0 until values.length
value = values(i)
do
if i == 0 then b += "=" + value
else b += s"&$key=$value"
end for
b
s"$newValue://$hostName$port$path$query"
end fromSmithy4sHttpUri
def toSmithy4sHttpUri(
uri: String,
pathParams: Option[smithy4s.http.PathParams] = None
): smithy4s.http.HttpUri =
import C.CURLUPart.*
import C.CURLUcode.*
Zone:
implicit z =>
val url = C.curl_url()
checkU(
C.curl_url_set(
url,
CURLUPART_URL,
toCString(uri),
0.toUInt
)
)
def getPart(part: C.CURLUPart): String =
val scheme = stackalloc[Ptr[Byte]](1)
checkU(C.curl_url_get(url, part, scheme, 0.toUInt))
val str = fromCString(!scheme)
C.curl_free(!scheme)
str
end getPart
val httpScheme = getPart(CURLUPART_SCHEME) match
case "https" => HttpUriScheme.Https
case "http" => HttpUriScheme.Http
case other =>
throw UnsupportedOperationException(
s"Protocol `${other}` is not supported"
)
val port = Try(getPart(CURLUPART_PORT)) match
case Failure(CurlUrlParseException(CURLUE_NO_PORT, _)) =>
None
case Success(value) => Some(value.toInt)
case Failure(other) => throw other
val host = getPart(CURLUPART_HOST)
val path = getPart(CURLUPART_PATH)
val cleanedPath: IndexedSeq[String] =
path.tail
// drop the guaranteed leading slash, so that we don't produce an empty segment for it
.tail
// splitting an empty path would produce a single element, so we special-case to empty
.match
case "" => IndexedSeq.empty[String]
case other => other.split("/")
HttpUri(
httpScheme,
host,
port,
cleanedPath,
Map.empty,
pathParams
)
end toSmithy4sHttpUri
val jsonCodecs = Json.payloadCodecs
.withJsoniterCodecCompiler(
Json.jsoniter
.withHintMask(hintMask)
.withMaxArity(maxArity)
.withExplicitDefaultsEncoding(explicitNulls = true)
)
val payloadEncoders: BlobEncoder.Compiler =
jsonCodecs.encoders
val payloadDecoders =
jsonCodecs.decoders
val errorHeaders = List(
smithy4s.http.errorTypeHeader
)
def makeClientCodecs(
uri: String
): UnaryClientCodecs.Make[Try, HttpRequest[Blob], HttpResponse[Blob]] =
val baseRequest = HttpRequest(
HttpMethod.POST,
toSmithy4sHttpUri(uri, None),
Map.empty,
Blob.empty
)
HttpUnaryClientCodecs.builder
.withBodyEncoders(payloadEncoders)
.withSuccessBodyDecoders(payloadDecoders)
.withErrorBodyDecoders(payloadDecoders)
.withErrorDiscriminator(resp =>
Success(HttpDiscriminator.fromResponse(errorHeaders, resp))
)
.withMetadataDecoders(Metadata.Decoder)
.withMetadataEncoders(
Metadata.Encoder.withExplicitDefaultsEncoding(
explicitDefaultsEncoding
)
)
.withBaseRequest(_ => Success(baseRequest))
.withRequestMediaType("application/json")
.withRequestTransformation[HttpRequest[Blob]](Success(_))
.withResponseTransformation[HttpResponse[Blob]](Success(_))
.withHostPrefixInjection(hostPrefixInjection)
.build()
end makeClientCodecs
end SimpleRestJsonCodecs