-
Notifications
You must be signed in to change notification settings - Fork 39
/
HttpClient.scala
174 lines (160 loc) · 6.5 KB
/
HttpClient.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
/*
* Copyright (c) 2012-2022 Snowplow Analytics Ltd. All rights reserved.
*
* This program is licensed to you under the Apache License Version 2.0,
* and you may not use this file except in compliance with the Apache License Version 2.0.
* You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the Apache License Version 2.0 is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
*/
package com.snowplowanalytics.snowplow.enrich.common.utils
import scala.util.control.NonFatal
import cats.{Applicative, Id}
import cats.effect.Sync
import cats.implicits._
import fs2.Stream
import org.http4s.client.{Client => Http4sClient}
import org.http4s.headers.Authorization
import org.http4s.{BasicCredentials, EmptyBody, EntityBody, Header, Headers, Method, Request, Status, Uri}
import scalaj.http._
trait HttpClient[F[_]] {
def getResponse(
uri: String,
authUser: Option[String],
authPassword: Option[String],
body: Option[String],
method: String,
connectionTimeout: Option[Long],
readTimeout: Option[Long]
): F[Either[Throwable, String]]
}
object HttpClient {
def apply[F[_]](implicit ev: HttpClient[F]): HttpClient[F] = ev
private[utils] def getHeaders(authUser: Option[String], authPassword: Option[String]): Headers = {
val alwaysIncludedHeaders = List(Header("content-type", "application/json"), Header("accept", "*/*"))
if (authUser.isDefined || authPassword.isDefined)
Headers(Authorization(BasicCredentials(authUser.getOrElse(""), authPassword.getOrElse(""))) :: alwaysIncludedHeaders)
else Headers(alwaysIncludedHeaders)
}
implicit def syncHttpClient[F[_]: Sync](implicit http4sClient: Http4sClient[F]): HttpClient[F] =
new HttpClient[F] {
/**
* Only uri, method and body are used for syncHttpClient
* Other parameters exist for compatibility with Id instance
* and they aren't used here
* Corresponding configurations come from http4s client configuration
*/
override def getResponse(
uri: String,
authUser: Option[String],
authPassword: Option[String],
body: Option[String],
method: String,
connectionTimeout: Option[Long],
readTimeout: Option[Long]
): F[Either[Throwable, String]] =
Uri.fromString(uri) match {
case Left(parseFailure) =>
Applicative[F].pure(new IllegalArgumentException(s"uri [$uri] is not valid: ${parseFailure.sanitized}").asLeft[String])
case Right(validUri) =>
val request = Request[F](
uri = validUri,
method = Method.fromString(method).getOrElse(Method.GET),
body = body.fold[EntityBody[F]](EmptyBody)(s => Stream.emits(s.getBytes)),
headers = getHeaders(authUser, authPassword)
)
http4sClient
.run(request)
.use[F, Either[Throwable, String]] { response =>
val body = response.bodyText.compile.string
response.status.responseClass match {
case Status.Successful => body.map(_.asRight[Throwable])
case _ => Applicative[F].pure(new Exception(s"Request failed with status ${response.status.code}").asLeft[String])
}
}
.handleError(_.asLeft[String])
}
}
implicit val idHttpClient: HttpClient[Id] =
new HttpClient[Id] {
override def getResponse(
uri: String,
authUser: Option[String],
authPassword: Option[String],
body: Option[String],
method: String,
connectionTimeout: Option[Long],
readTimeout: Option[Long]
): Id[Either[Throwable, String]] =
getBody(
buildRequest(
uri,
authUser,
authPassword,
body,
method,
connectionTimeout,
readTimeout
)
)
}
// The defaults are from scalaj library
val DEFAULT_CONNECTION_TIMEOUT_MS = 1000
val DEFAULT_READ_TIMEOUT_MS = 5000
/**
* Blocking method to get body of HTTP response
* @param request assembled request object
* @return validated body of HTTP request
*/
private def getBody(request: HttpRequest): Either[Throwable, String] =
try {
val res = request.asString
if (res.isSuccess) res.body.asRight
else new Exception(s"Request failed with status ${res.code} and body ${res.body}").asLeft
} catch {
case NonFatal(e) => e.asLeft
}
/**
* Build HTTP request object
* @param uri full URI to request
* @param authUser optional username for basic auth
* @param authPassword optional password for basic auth
* @param body optional request body
* @param method HTTP method
* @param connectionTimeout connection timeout, if not set default is 1000ms
* @param readTimeout read timeout, if not set default is 5000ms
* @return HTTP request
*/
def buildRequest(
uri: String,
authUser: Option[String],
authPassword: Option[String],
body: Option[String],
method: String = "GET",
connectionTimeout: Option[Long],
readTimeout: Option[Long]
): HttpRequest = {
val req: HttpRequest = Http(uri).method(method).maybeTimeout(connectionTimeout, readTimeout)
req.maybeAuth(authUser, authPassword).maybePostData(body)
}
implicit class RichHttpRequest(request: HttpRequest) {
def maybeAuth(user: Option[String], password: Option[String]): HttpRequest =
if (user.isDefined || password.isDefined)
request.auth(user.getOrElse(""), password.getOrElse(""))
else request
def maybeTimeout(connectionTimeout: Option[Long], readTimeout: Option[Long]): HttpRequest =
(connectionTimeout, readTimeout) match {
case (Some(ct), Some(rt)) => request.timeout(ct.toInt, rt.toInt)
case (Some(ct), None) => request.timeout(ct.toInt, DEFAULT_READ_TIMEOUT_MS)
case (None, Some(rt)) => request.timeout(DEFAULT_CONNECTION_TIMEOUT_MS, rt.toInt)
case _ => request.timeout(DEFAULT_CONNECTION_TIMEOUT_MS, DEFAULT_READ_TIMEOUT_MS)
}
def maybePostData(body: Option[String]): HttpRequest =
body
.map(data => request.postData(data).header("content-type", "application/json").header("accept", "*/*"))
.getOrElse(request)
}
}