-
Notifications
You must be signed in to change notification settings - Fork 4
/
ZIOBackend.scala
148 lines (134 loc) · 4.22 KB
/
ZIOBackend.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
/*
* Copyright 2022 Hossein Naderi
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dev.hnaderi.k8s.client
import dev.hnaderi.k8s.utils.Decoder
import dev.hnaderi.k8s.utils.Encoder
import dev.hnaderi.k8s.zioJson._
import io.k8s.apimachinery.pkg.apis.meta.v1
import zio._
import zio.http._
import zio.json._
import ZIOBackend._
final case class ZIOBackend(
client: Client
) extends HttpBackend[Task] {
override def send[O: Decoder](
url: String,
verb: APIVerb,
headers: Seq[(String, String)],
params: Seq[(String, String)],
cookies: Seq[(String, String)]
): Task[O] = for {
u <- urlFor(url, params)
con <- contentType(verb)
req = Request(
method = methodFor(verb),
url = u,
body = Body.empty,
headers = Headers(con) ++ cookiesFor(cookies),
version = Version.`HTTP/1.1`,
remoteAddress = None
)
o <- expect(req)
} yield o
override def send[I: Encoder, O: Decoder](
url: String,
verb: APIVerb,
body: I,
headers: Seq[(String, String)],
params: Seq[(String, String)],
cookies: Seq[(String, String)]
): Task[O] = for {
u <- urlFor(url, params)
con <- contentType(verb)
req = Request(
method = methodFor(verb),
url = u,
body = Body.fromString(body.toJson),
headers = Headers(con) ++ cookiesFor(cookies),
version = Version.`HTTP/1.1`,
remoteAddress = None
)
o <- expect(req)
} yield o
private def cookiesFor(values: Seq[(String, String)]) = NonEmptyChunk
.fromIterableOption(values.map { case (k, v) =>
Cookie.Request(k, v)
})
.map(Header.Cookie(_))
.fold(Headers.empty)(Headers(_))
private def methodFor: APIVerb => Method = {
case APIVerb.GET => Method.GET
case APIVerb.POST => Method.POST
case APIVerb.DELETE => Method.DELETE
case APIVerb.PUT => Method.PUT
case APIVerb.PATCH(_) => Method.PATCH
}
private def contentType(verb: APIVerb) =
ZIO.fromEither(
Header.ContentType
.parse(
verb match {
case APIVerb.PATCH(ptype) => ptype.contentType
case _ => "application/json"
}
)
.left
.map(new IllegalArgumentException(_))
)
private def urlFor(
url: String,
params: Seq[(String, String)]
): Task[http.URL] = for {
u <- ZIO.fromEither(URL.decode(url))
qp = params.foldLeft(Map.empty[String, Chunk[String]]) {
case (qs, (k, v)) =>
qs.get(k) match {
case None => qs.updated(k, Chunk(v))
case Some(value) => qs.updated(k, Chunk(v) ++ value)
}
}
} yield u.withQueryParams(qp)
private def expect[O: Decoder](req: http.Request): Task[O] =
client.request(req).flatMap { res =>
def readBody[T: Decoder]: Task[T] = res.body.asString.flatMap(body =>
ZIO.fromEither(
JsonDecoder[T]
.decodeJson(body)
.left
.map(DecodeError(_))
)
)
if (res.status.isSuccess) readBody[O]
else {
val err = res.status match {
case http.Status.Conflict => ErrorStatus.Conflict
case http.Status.BadRequest => ErrorStatus.BadRequest
case http.Status.Unauthorized => ErrorStatus.Unauthorized
case http.Status.NotFound => ErrorStatus.NotFound
case s => ErrorStatus.Other(s.code)
}
val status = readBody[v1.Status]
status.map(ErrorResponse(err, _)).flatMap(ZIO.die(_))
}
}
}
object ZIOBackend {
final case class DecodeError(msg: String) extends Exception(msg)
def make: ZLayer[Client, Nothing, ZIOBackend] = ZLayer {
ZIO.service[Client].map(ZIOBackend(_))
}
}