forked from dakrone/clj-http
/
client.clj
217 lines (175 loc) · 6.02 KB
/
client.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
(ns clj-http.client
"Batteries-included HTTP client."
(:import (java.net URL))
(:require [clojure.contrib.string :as str])
(:require [clj-http.core :as core])
(:require [clj-http.util :as util])
(:refer-clojure :exclude (get)))
(defn update [m k f & args]
(assoc m k (apply f (m k) args)))
(defn if-pos [v]
(if (and v (pos? v)) v))
(defn parse-url [url]
(let [url-parsed (URL. url)]
{:scheme (.getProtocol url-parsed)
:server-name (.getHost url-parsed)
:server-port (if-pos (.getPort url-parsed))
:uri (.getPath url-parsed)
:query-string (.getQuery url-parsed)}))
(def unexceptional-status?
#{200 201 202 203 204 205 206 207 300 301 302 303 307})
(defn wrap-exceptions [client]
(fn [req]
(let [{:keys [status] :as resp} (client req)]
(if (unexceptional-status? status)
resp
(throw (Exception. (str status)))))))
(defn follow-redirect [client req resp]
(let [url (get-in resp [:headers "location"])]
(client (merge req (parse-url url)))))
(defn wrap-redirects [client]
(fn [{:keys [request-method] :as req}]
(let [{:keys [status] :as resp} (client req)]
(cond
(and (#{301 302 307} status) (#{:get :head} request-method))
(follow-redirect client req resp)
(and (= 303 status) (= :head request-method))
(follow-redirect client (assoc req :request-method :get) resp)
:else
resp))))
(defn wrap-decompression [client]
(fn [req]
(if (get-in req [:headers "Accept-Encoding"])
(client req)
(let [req-c (update req :headers assoc "Accept-Encoding" "gzip, deflate")
resp-c (client req)]
(case (get-in resp-c [:headers "Content-Encoding"])
"gzip"
(update resp-c :body util/gunzip)
"deflate"
(update resp-c :body util/inflate)
resp-c)))))
(defn wrap-output-coercion [client]
(fn [{:keys [as] :as req}]
(let [{:keys [body] :as resp} (client req)]
(cond
(or (nil? body) (= :bytes as))
resp
(nil? as)
(assoc resp :body (String. body "UTF-8"))))))
(defn wrap-input-coercion [client]
(fn [{:keys [body] :as req}]
(if (string? body)
(client (-> req (assoc :body (.getBytes body "UTF-8")
:character-encoding "UTF-8")))
(client req))))
(defn content-type-value [type]
(if (keyword? type)
(str "application/" (name type))
type))
(defn wrap-content-type [client]
(fn [{:keys [content-type] :as req}]
(if content-type
(client (-> req (assoc :content-type
(content-type-value content-type))))
(client req))))
(defn wrap-accept [client]
(fn [{:keys [accept] :as req}]
(if accept
(client (-> req (dissoc :accept)
(assoc-in [:headers "Accept"]
(content-type-value accept))))
(client req))))
(defn accept-encoding-value [accept-encoding]
(str/join ", " (map name accept-encoding)))
(defn wrap-accept-encoding [client]
(fn [{:keys [accept-encoding] :as req}]
(if accept-encoding
(client (-> req (dissoc :accept-encoding)
(assoc-in [:headers "Accept-Encoding"]
(accept-encoding-value accept-encoding))))
(client req))))
(defn generate-query-string [params]
(str/join "&"
(map (fn [[k v]] (str (util/url-encode (name k)) "="
(util/url-encode (str v))))
params)))
(defn wrap-query-params [client]
(fn [{:keys [query-params] :as req}]
(if query-params
(client (-> req (dissoc :query-params)
(assoc :query-string
(generate-query-string query-params))))
(client req))))
(defn basic-auth-value [user password]
(str "Basic "
(util/base64-encode (.getBytes (str user ":" password) "UTF-8"))))
(defn wrap-basic-auth [client]
(fn [req]
(if-let [[user password] (:basic-auth req)]
(client (-> req (dissoc :basic-auth)
(assoc-in [:headers "Authorization"]
(basic-auth-value user password))))
(client req))))
(defn wrap-method [client]
(fn [req]
(if-let [m (:method req)]
(client (-> req (dissoc :method)
(assoc :request-method m)))
(client req))))
(defn wrap-url [client]
(fn [req]
(if-let [url (:url req)]
(client (-> req (dissoc :url) (merge (parse-url url))))
(client req))))
(def #^{:doc
"Executes the HTTP request corresponding to the given map and returns the
response map for corresponding to the resulting HTTP response.
In addition to the standard Ring request keys, the following keys are also
recognized:
* :url
* :method
* :query-params
* :basic-auth
* :content-type
* :accept
* :accept-encoding
* :as
The following additional behaviors over also automatically enabled:
* string request bodies are converted to byte arrays
* exceptions are thrown for status codes other than 200-207, 300-303, or 307
* gzip and deflate responses are accepted and decompressed"}
request
(-> #'core/request
wrap-redirects
wrap-exceptions
wrap-decompression
wrap-input-coercion
wrap-output-coercion
wrap-query-params
wrap-basic-auth
wrap-accept
wrap-accept-encoding
wrap-content-type
wrap-method
wrap-url))
(defn get
"Like #'request, but sets the :method and :url as appropriate."
[url & [req]]
(request (merge req {:method :get :url url})))
(defn head
"Like #'request, but sets the :method and :url as appropriate."
[url & [req]]
(request (merge req {:method :head :url url})))
(defn post
"Like #'request, but sets the :method and :url as appropriate."
[url & [req]]
(request (merge req {:method :put :url url})))
(defn put
"Like #'request, but sets the :method and :url as appropriate."
[url & [req]]
(request (merge req {:method :post :url url})))
(defn delete
"Like #'request, but sets the :method and :url as appropriate."
[url & [req]]
(request (merge req {:method :delete :url url})))