/
http.clj
446 lines (371 loc) · 14.7 KB
/
http.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
;; This source code is dual-licensed under the Apache License, version
;; 2.0, and the Eclipse Public License, version 1.0.
;;
;; The APL v2.0:
;;
;; ----------------------------------------------------------------------------------
;; Copyright (c) 2011-2020 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team
;;
;; 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.
;; ----------------------------------------------------------------------------------
;;
;; The EPL v1.0:
;;
;; ----------------------------------------------------------------------------------
;; Copyright (c) 2011-2020 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team.
;; All rights reserved.
;;
;; This program and the accompanying materials are made available under the terms of
;; the Eclipse Public License Version 1.0,
;; which accompanies this distribution and is available at
;; http://www.eclipse.org/legal/epl-v10.html.
;; ----------------------------------------------------------------------------------
(ns langohr.http
"RabbitMQ HTTP API client.
Relevant documentation guides:
* http://www.rabbitmq.com/management.html
* https://raw.githack.com/rabbitmq/rabbitmq-management/rabbitmq_v3_5_6/priv/www/api/index.html"
(:refer-clojure :exclude [get])
(:require [clj-http.client :as http]
[cheshire.core :as json]
[clojure.string :as s])
(:import java.net.URLEncoder))
;;
;; Implementation
;;
(def ^:dynamic *endpoint* "http://127.0.0.1:15672")
(def ^:dynamic *username* "guest")
(def ^:dynamic *password* "guest")
(def ^:const throw-exceptions false)
(def ^:dynamic *default-http-options* {:accept :json
:content-type "application/json"
:throw-exceptions throw-exceptions})
(defn url-with-path
[path]
(str *endpoint* path))
(defn safe-json-decode
"Try to parse json response. If the content-type is not json, just return the body (string)."
[{body :body {content-type "content-type"} :headers}]
(if (or (nil? body) (.isEmpty ^String body))
nil
(if (.contains (.toLowerCase ^String content-type) "json")
(json/decode body true)
body)))
(defn ^{:private true} post
([^String uri]
(post uri {}))
([^String uri {:keys [body] :as options}]
(io! (:body (http/post uri (merge *default-http-options* options {:basic-auth [*username* *password*]
:body (json/encode body)}))) true)))
(defn ^{:private true} put
[^String uri {:keys [body] :as options}]
(io! (:body (http/put uri (merge *default-http-options* options {:basic-auth [*username* *password*]
:body (json/encode body)}))) true))
(defn ^{:private true} get
([^String uri]
(get uri {}))
([^String uri options]
(io! (http/get uri (merge *default-http-options* options {:basic-auth [*username* *password*]})))))
(defn ^{:private true} get-and-decode-json
([^String uri]
(safe-json-decode (get uri)))
([^String uri options]
(safe-json-decode (get uri options))))
(defn ^{:private true} head
([^String uri]
(head uri {}))
([^String uri options]
(io! (http/head uri (merge *default-http-options* options {:basic-auth [*username* *password*]})))))
(defn ^{:private true} delete
([^String uri]
(delete uri {}))
([^String uri {:keys [body] :as options}]
(io! (:body (http/delete uri (merge *default-http-options* options {:basic-auth [*username* *password*]
:body (json/encode body)}))) true)))
(defn ^{:private true} missing?
[status]
(= status 404))
(defn ^{:private true} normalize-protocol
[proto]
(case proto
"amqp/ssl" "amqps"
(.toLowerCase ^String (str proto))))
;;
;; API
;;
(defn connect!
([^String endpoint ^String username ^String password]
(connect! endpoint username password {}))
([^String endpoint ^String username ^String password opts]
(alter-var-root (var *endpoint*) (constantly endpoint))
(alter-var-root (var *username*) (constantly username))
(alter-var-root (var *password*) (constantly password))
(alter-var-root (var *default-http-options*) (fn [m]
(merge m opts)))))
(defn get-overview
([]
(get-overview {}))
([m]
(get-and-decode-json (url-with-path "/api/overview") m)))
(defn list-enabled-protocols
([]
(list-enabled-protocols {}))
([m]
(set (map :protocol (:listeners (get-overview m))))))
(defn protocol-ports
([]
(protocol-ports {}))
([m]
(let [xs (:listeners (get-overview m))]
(reduce (fn [acc lnr]
(assoc acc (normalize-protocol (:protocol lnr)) (:port lnr)))
{}
xs))))
(defn list-nodes
([]
(list-nodes {}))
([m]
(get-and-decode-json (url-with-path "/api/nodes") m)))
(defn get-node
([^String node]
(get-node node {}))
([^String node m]
(get-and-decode-json (url-with-path (str "/api/nodes/" node)) m)))
(defn list-extensions
([]
(list-extensions {}))
([m]
(get-and-decode-json (url-with-path "/api/extensions") m)))
(defn list-definitions
([]
(list-definitions {}))
([m]
(get-and-decode-json (url-with-path "/api/definitions") m)))
(defn list-connections
([]
(list-connections {}))
([m]
(get-and-decode-json (url-with-path "/api/connections") m)))
(defn list-connections-from
([^String user]
(list-connections-from user {}))
([^String user m]
(let [xs (list-connections m)]
(filter (fn [m]
(= user (:user m)))
xs))))
(defn get-connection
([^String id]
(get-connection id {}))
([^String id m]
(get-and-decode-json (url-with-path (str "/api/connections/" id)) m)))
(defn close-connection
([^String id]
(close-connection id {}))
([^String id m]
(delete (url-with-path (str "/api/connections/" id)) m)))
(defn close-connections-from
([^String user]
(close-connections-from user {}))
([^String user m]
(doseq [^String cn (map :name (list-connections-from user m))]
(close-connection cn m))))
(defn close-all-connections
[]
(doseq [^String cn (map :name (list-connections))]
(close-connection cn {})))
(defn list-channels
([]
(list-channels {}))
([m]
(get-and-decode-json (url-with-path "/api/channels") m)))
(defn list-exchanges
([]
(get-and-decode-json (url-with-path "/api/exchanges")))
([^String vhost]
(get-and-decode-json (url-with-path (str "/api/exchanges/" (URLEncoder/encode vhost))))))
(defn get-exchange
[^String vhost ^String exchange]
(get-and-decode-json (url-with-path (format "/api/exchanges/%s/%s" (URLEncoder/encode vhost) (URLEncoder/encode exchange)))))
(defn declare-exchange
[^String vhost ^String exchange properties]
(put (url-with-path (format "/api/exchanges/%s/%s" (URLEncoder/encode vhost) (URLEncoder/encode exchange))) {:body properties}))
(defn delete-exchange
([^String vhost ^String exchange]
(delete-exchange vhost exchange {}))
([^String vhost ^String exchange m]
(delete (url-with-path (format "/api/exchanges/%s/%s" (URLEncoder/encode vhost) (URLEncoder/encode exchange))) m)))
(defn list-bindings-for-which-exchange-is-the-source
[^String vhost ^String exchange]
(get-and-decode-json (url-with-path (format "/api/exchanges/%s/%s/bindings/source" (URLEncoder/encode vhost) (URLEncoder/encode exchange)))))
(defn list-bindings-for-which-exchange-is-the-destination
[^String vhost ^String exchange]
(get-and-decode-json (url-with-path (format "/api/exchanges/%s/%s/bindings/destination" (URLEncoder/encode vhost) (URLEncoder/encode exchange)))))
(defn publish
[^String vhost ^String exchange]
)
(defn list-queues
([]
(get-and-decode-json (url-with-path "/api/queues")))
([^String vhost]
(list-queues vhost {}))
([^String vhost m]
(get-and-decode-json (url-with-path (format "/api/queues/%s" (URLEncoder/encode vhost))) m)))
(defn get-queue
[^String vhost ^String queue]
(get-and-decode-json (url-with-path (format "/api/queues/%s/%s" (URLEncoder/encode vhost) (URLEncoder/encode queue)))))
(defn declare-queue
[^String vhost ^String queue properties]
(put (url-with-path (format "/api/queues/%s/%s" (URLEncoder/encode vhost) (URLEncoder/encode queue))) {:body properties}))
(defn delete-queue
([^String vhost ^String queue]
(delete-queue vhost queue {}))
([^String vhost ^String queue m]
(delete (url-with-path (format "/api/queues/%s/%s" (URLEncoder/encode vhost) (URLEncoder/encode queue))) m)))
(defn purge-queue
([^String vhost ^String queue]
(purge-queue vhost queue {}))
([^String vhost ^String queue m]
(delete (url-with-path (format "/api/queues/%s/%s/contents" (URLEncoder/encode vhost) (URLEncoder/encode queue))) m)))
(defn get-message
[^String vhost ^String queue]
)
(defn list-bindings
([]
(get-and-decode-json (url-with-path "/api/bindings")))
([^String vhost]
(get-and-decode-json (url-with-path (format "/api/bindings/%s" (URLEncoder/encode vhost))) {}))
([^String vhost ^String queue]
(list-bindings vhost queue {}))
([^String vhost ^String queue m]
(get-and-decode-json (url-with-path (format "/api/queues/%s/%s/bindings"
(URLEncoder/encode vhost)
(URLEncoder/encode queue))) m)))
(defn bind
([^String vhost ^String exchange ^String queue]
(bind vhost exchange queue {} {}))
([^String vhost ^String exchange ^String queue properties]
(bind vhost exchange queue properties {}))
([^String vhost ^String exchange ^String queue properties m]
(post (url-with-path (format "/api/bindings/%s/e/%s/q/%s"
(URLEncoder/encode vhost)
(URLEncoder/encode exchange)
(URLEncoder/encode queue))) (merge m {:body properties}))))
(defn list-vhosts
([]
(list-vhosts {}))
([m]
(get-and-decode-json (url-with-path "/api/vhosts") m)))
(defn vhost-exists?
([^String vhost]
(vhost-exists? vhost {}))
([^String vhost m]
(let [{:keys [status]} (head (url-with-path (format "/api/vhosts/%s" (URLEncoder/encode vhost))) m)]
(not (missing? status)))))
(defn get-vhost
([^String vhost]
(get-vhost vhost {}))
([^String vhost m]
(get-and-decode-json (url-with-path (format "/api/vhosts/%s" (URLEncoder/encode vhost))) m)))
(defn add-vhost
([^String vhost]
(add-vhost vhost {}))
([^String vhost m]
(put (url-with-path (format "/api/vhosts/%s" (URLEncoder/encode vhost))) (merge m {:body {:name vhost}}))))
(defn ^{:deprecated true} declare-vhost
"Deprecated. Use add-vhost."
[^String vhost]
(add-vhost vhost))
(defn delete-vhost
([^String vhost]
(delete-vhost vhost {}))
([^String vhost m]
(delete (url-with-path (format "/api/vhosts/%s" (URLEncoder/encode vhost))) m)))
(defn list-permissions
([]
(get-and-decode-json (url-with-path "/api/permissions")))
([^String vhost]
(list-permissions vhost {}))
([^String vhost m]
(get-and-decode-json (url-with-path (format "/api/vhosts/%s/permissions" (URLEncoder/encode vhost))) m)))
(defn get-permissions
([^String vhost ^String username]
(get-permissions vhost username {}))
([^String vhost ^String username m]
(get-and-decode-json (url-with-path (format "/api/permissions/%s/%s" (URLEncoder/encode vhost) (URLEncoder/encode username))) m)))
(defn set-permissions
([^String vhost ^String username {:keys [configure write read] :as options}]
{:pre [(every? string? [configure write read])]}
(put (url-with-path (format "/api/permissions/%s/%s" (URLEncoder/encode vhost) (URLEncoder/encode username))) {:body options})))
(defn ^{:deprecated true} declare-permissions
"Deprecated. Use set-permissions."
[^String vhost ^String username {:keys [configure write read] :as body}]
{:pre [(every? string? [configure write read])]}
(set-permissions vhost username body))
(defn delete-permissions
[^String vhost ^String username]
(delete (url-with-path (format "/api/permissions/%s/%s" (URLEncoder/encode vhost) (URLEncoder/encode username)))))
(defn list-users
([]
(list-users {}))
([m]
(get-and-decode-json (url-with-path "/api/users") m)))
(defn get-user
([^String user]
(get-user user {}))
([^String user m]
(get-and-decode-json (url-with-path (format "/api/users/%s" (URLEncoder/encode user))) m)))
(defn user-exists?
([^String user]
(user-exists? user {}))
([^String user m]
(let [{:keys [status]} (head (url-with-path (format "/api/users/%s" (URLEncoder/encode user))) m)]
(not (missing? status)))))
(defn add-user
([^String user password tags]
(add-user user password tags {}))
([^String user password tags m]
(put (url-with-path (format "/api/users/%s" (URLEncoder/encode user))) (merge m {:body {:username user :password password :tags tags :has-password true}}))))
(defn ^{:deprecated true} declare-user
"Deprecated. Use add-user."
[^String user password tags]
(add-user user password tags))
(defn delete-user
([^String user]
(delete-user user {}))
([^String user m]
(delete (url-with-path (format "/api/users/%s" (URLEncoder/encode user))) m)))
(defn list-policies
[]
(get-and-decode-json (url-with-path "/api/policies")))
(defn get-policies
([^String vhost]
(get-and-decode-json (url-with-path (format "/api/policies/%s" (URLEncoder/encode vhost)))))
([^String vhost ^String name]
(get-and-decode-json (url-with-path (format "/api/policies/%s/%s" (URLEncoder/encode vhost) (URLEncoder/encode name))))))
(defn set-policy
[^String vhost ^String name policy]
(put (url-with-path (format "/api/policies/%s/%s" (URLEncoder/encode vhost) (URLEncoder/encode name))) {:body policy}))
(defn ^{:deprecated true} declare-policy
"Deprecated. Use set-policy."
[^String vhost ^String name policy]
(set-policy vhost name policy))
(defn delete-policy
[^String vhost]
(delete (url-with-path (format "/api/policies/%s" (URLEncoder/encode vhost)))))
(defn whoami
[]
(get-and-decode-json (url-with-path "/api/whoami")))
(defn aliveness-test
[^String vhost]
(get-and-decode-json (url-with-path (format "/api/aliveness-test/%s" (URLEncoder/encode vhost)))))