-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.clj
239 lines (214 loc) · 8.5 KB
/
core.clj
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
(ns java-http-clj.core
(:refer-clojure :exclude [get])
(:require [clojure.string :as str]
[cheshire.core :as json]
[java-http-clj.client :refer [default-client]]
[java-http-clj.util :refer [convert-timeout convert-version version->sym]])
(:import [java.net URI]
[java.net.http
HttpClient
HttpRequest
HttpRequest$BodyPublishers
HttpRequest$Builder
HttpResponse
HttpResponse$BodyHandlers]
[java.time Duration]
[java.util.concurrent CompletableFuture Executor]
[java.util.function Function Supplier]
[javax.net.ssl SSLContext SSLParameters]))
(def ^:private http-methods [:get :head :post :put :delete])
(defonce ^:private NANOSECOND_MILLIS 1000000.0)
(defn- convert-body-publisher [body]
(letfn [(input-stream-supplier [s]
(reify Supplier
(get [this] s)))]
(let [byte-array-class (Class/forName "[B")]
(cond
(nil? body)
(HttpRequest$BodyPublishers/noBody)
(string? body)
(HttpRequest$BodyPublishers/ofString body)
(instance? java.io.InputStream body)
(HttpRequest$BodyPublishers/ofInputStream (input-stream-supplier body))
(instance? byte-array-class body)
(HttpRequest$BodyPublishers/ofByteArray body)))))
(def ^:private convert-headers-xf
(mapcat
(fn [[k v :as p]]
(if (sequential? v)
(interleave (repeat k) v)
p))))
(defn params->query-string
"Converts a map of query parameter options into a URL encoded query string that
can be added to a URI"
[m]
(str/join "&"
(for [[k v] m]
(str (name k) "=" (java.net.URLEncoder/encode v)))))
(defn build-uri [uri query-parameters]
(if (nil? query-parameters)
(URI/create uri))
(let [qs (params->query-string query-parameters)]
(URI/create (str uri "?" qs))))
(defn request-builder
^HttpRequest$Builder [opts]
(let [{:keys [expect-continue?
headers
query-parameters
method
timeout
url
version
body]} opts]
(cond-> (HttpRequest/newBuilder)
(some? expect-continue?) (.expectContinue expect-continue?)
(seq headers) (.headers (into-array String (eduction convert-headers-xf headers)))
method (.method (str/upper-case (name method)) (convert-body-publisher body))
timeout (.timeout (convert-timeout timeout))
url (.uri (build-uri url query-parameters))
version (.version (convert-version version)))))
(defn build-request
([] (.build (request-builder {})))
([req-map]
(.build (request-builder req-map))))
(defn response->map [^HttpResponse resp]
(letfn [(version-string->version-keyword [version]
(case (.name version)
"HTTP_1_1" :http1.1
"HTTP_2" :http2))]
{:status (.statusCode resp)
:body (.body resp)
:version (-> resp .version version-string->version-keyword)
:headers (into {}
(map
(fn [[k v]]
[k (if (> (count v) 1)
(vec v)
(first v))]))
(.map (.headers resp)))}))
(defmacro fn->java-function
"Turns a Clojure fn form into a Java function"
^Function
[f]
`(reify Function
(apply [_# x#] (~f x#))))
(defn- convert-request
[req]
(cond
(map? req)
(build-request req)
(string? req)
(build-request {:url req})
(instance? HttpRequest req)
req))
(defn- convert-body-handler
"Maps a clojure body handler keyword to a Java form"
[mode]
(case mode
nil (HttpResponse$BodyHandlers/ofString)
:string (HttpResponse$BodyHandlers/ofString)
:input-stream (HttpResponse$BodyHandlers/ofInputStream)
:byte-array (HttpResponse$BodyHandlers/ofByteArray)))
;; ***************************************
;; Public API
;; ***************************************
(defn request
"Sends a HTTP request and blocks until a response is returned or the request
takes longer than the specified `timeout`.
If the request times out, a [HttpTimeoutException]
(https://docs.oracle.com/en/java/javase/11/docs/api/java.net.http/java/net/http/HttpTimeoutException.html) is thrown.
The request map takes the following keys:
- `:method` - HTTP method as a keyword (e.g `:get`, `:put`, `:post`)
- `:url` - the request url
- `:headers` - HTTP headers as a map where keys are strings and values are strings or a list of strings
- `:body` - the request body. Can be a string, a primitive Java byte array or a java.io.InputStream.
- `:timeout` - the request timeout in milliseconds or a `java.time.Duration`
- `:version` - the HTTP protocol version, one of `:http1.1` or `:http2`
The `opts` param is a map containing one of the following keys:
- `:as` - converts the response body to one of the following formats:
- `:string` - a java.lang.String (default)
- `:byte-array` - a Java primitive byte array.
- `:input-stream` - a java.io.InputStream.
- `:client` - the [HttpClient](https://docs.oracle.com/en/java/javase/11/docs/api/java.net.http/java/net/http/HttpClient.html)
to use for the request. If not provided the [[default-client]] will be used.
- `:raw?` - if true, skip the Ring format conversion and return the
raw [HttpResponse](https://docs.oracle.com/en/java/javase/11/docs/api/java.net.http/java/net/http/HttpResponse.html"
([req]
(request req {}))
([req {:keys [as client raw?] :as opts}]
(let [^HttpClient client (or client @default-client)
req' (convert-request req)
start (. System (nanoTime))
resp (if (= :json as)
(.send client req' (convert-body-handler :string))
(.send client req' (convert-body-handler as)))]
(let [request-time-ms (/ (double (- (. System (nanoTime)) start)) NANOSECOND_MILLIS)]
(if raw?
resp
(with-meta
(let [response-map (response->map resp)]
(if (= :json as)
(update-in response-map [:body] json/parse-string)
response-map))
{:request-time-ms request-time-ms}))))))
(defn async-request
([req]
(async-request req {} nil nil))
([req opts]
(async-request req opts nil nil))
([req opts callback]
(async-request req opts callback nil))
([req {:keys [as client raw?]} callback ex-handler]
(let [^HttpClient client (or client @default-client)
req' (convert-request req)]
(cond-> (.sendAsync client req' (convert-body-handler as))
(not raw?) (.thenApply (fn->java-function response->map))
callback (.thenApply (fn->java-function callback))
ex-handler (.exceptionally (fn->java-function ex-handler))))))
;; ***************************************
;; Sugar Methods
;; ***************************************
(defn- method-docstring
"Generate the doc string for the sync auto generated helper methods"
[method]
(str "Sends a synchronous "
(str/upper-case (name method)) " request to `uri`.
See [[send]] for a description of `req-map` and `opts`."))
(defn- define-method [method]
`(defn ~(symbol (name method))
~(method-docstring method)
(~['url]
(request
~{:url 'url :method method} {}))
(~['url 'req-map]
(request
(merge ~'req-map ~{:url 'url :method method}) {}))
(~['url 'req-map 'opts]
(request
(merge ~'req-map ~{:url 'url :method method}) ~'opts))))
(defn- method-async-docstring
"Generate the doc string for the async auto generated helper methods"
[method]
(str "Sends an asynchronous "
(str/upper-case (name method))
" request to `uri`."))
(defn- define-method-async
"Build shorthand methods for async requests"
[method]
`(defn ~(symbol (str "async-" (name method)))
~(method-async-docstring method)
(~['url 'callback]
(async-request
~{:url 'url :method method} {} ~'callback))
(~['url 'req-map 'callback]
(async-request
(merge ~'req-map ~{:url 'url :method method}) {} ~'callback))
(~['url 'req-map 'opts 'callback]
(async-request
(merge ~'req-map ~{:url 'url :method method}) ~'opts ~'callback))))
(defmacro ^:private def-all-methods []
`(do ~@(map define-method http-methods)))
(defmacro ^:private def-all-async-methods []
`(do ~@(map define-method-async http-methods)))
(def-all-methods)
(def-all-async-methods)