-
Notifications
You must be signed in to change notification settings - Fork 30
/
sync.clj
126 lines (111 loc) · 5.3 KB
/
sync.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
;; This namespace provides synchronous versions of the request functions
;; defined in puppetlabs.http.client
(ns puppetlabs.http.client.sync
(:require [puppetlabs.http.client.async :as async]
[schema.core :as schema]
[puppetlabs.http.client.common :as common]
[puppetlabs.http.client.metrics :as metrics])
(:refer-clojure :exclude (get)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Private utility functions
(schema/defn extract-client-opts :- common/ClientOptions
[opts :- common/RawUserRequestClientOptions]
(select-keys opts [:ssl-context :ssl-ca-cert :ssl-cert :ssl-key
:ssl-protocols :cipher-suites
:force-redirects :follow-redirects
:connect-timeout-milliseconds
:socket-timeout-milliseconds
:max-connections-per-route
:max-connections-total]))
(schema/defn extract-request-opts :- common/RawUserRequestOptions
[opts :- common/RawUserRequestClientOptions]
(select-keys opts [:url :method :headers :body
:decompress-body :compress-request-body
:as :query-params]))
(defn request-with-client
([req client]
(request-with-client req client nil nil true))
([req client metric-registry metric-namespace]
(request-with-client req client metric-registry metric-namespace true))
([req client metric-registry metric-namespace enable-url-metrics?]
(let [{:keys [error] :as resp} @(async/request-with-client
req nil client metric-registry metric-namespace enable-url-metrics?)]
(if error
(throw error)
resp))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Public
(defn request
[req]
(with-open [client (async/create-default-client (extract-client-opts req))]
(request-with-client (extract-request-opts req) client)))
(schema/defn create-client :- (schema/protocol common/HTTPClient)
[opts :- common/ClientOptions]
(let [client (async/create-default-client opts)
metric-registry (:metric-registry opts)
metric-namespace (metrics/build-metric-namespace (:metric-prefix opts) (:server-id opts))
enable-url-metrics? (clojure.core/get opts :enable-url-metrics? true)]
(reify common/HTTPClient
(get [this url] (common/get this url {}))
(get [this url opts] (common/make-request this url :get opts))
(head [this url] (common/head this url {}))
(head [this url opts] (common/make-request this url :head opts))
(post [this url] (common/post this url {}))
(post [this url opts] (common/make-request this url :post opts))
(put [this url] (common/put this url {}))
(put [this url opts] (common/make-request this url :put opts))
(delete [this url] (common/delete this url {}))
(delete [this url opts] (common/make-request this url :delete opts))
(trace [this url] (common/trace this url {}))
(trace [this url opts] (common/make-request this url :trace opts))
(options [this url] (common/options this url {}))
(options [this url opts] (common/make-request this url :post opts))
(patch [this url] (common/patch this url {}))
(patch [this url opts] (common/make-request this url :patch opts))
(make-request [this url method] (common/make-request this url method {}))
(make-request [_ url method opts] (request-with-client
(assoc opts :method method :url url)
client metric-registry metric-namespace enable-url-metrics?))
(close [_] (.close client))
(get-client-metric-registry [_] metric-registry)
(get-client-metric-namespace [_] metric-namespace))))
(defn get
"Issue a synchronous HTTP GET request. This will raise an exception if an
error is returned."
([url] (get url {}))
([url opts] (request (assoc opts :method :get :url url))))
(defn head
"Issue a synchronous HTTP head request. This will raise an exception if an
error is returned."
([url] (head url {}))
([url opts] (request (assoc opts :method :head :url url))))
(defn post
"Issue a synchronous HTTP POST request. This will raise an exception if an
error is returned."
([url] (post url {}))
([url opts] (request (assoc opts :method :post :url url))))
(defn put
"Issue a synchronous HTTP PUT request. This will raise an exception if an
error is returned."
([url] (put url {}))
([url opts] (request (assoc opts :method :put :url url))))
(defn delete
"Issue a synchronous HTTP DELETE request. This will raise an exception if an
error is returned."
([url] (delete url {}))
([url opts] (request (assoc opts :method :delete :url url))))
(defn trace
"Issue a synchronous HTTP TRACE request. This will raise an exception if an
error is returned."
([url] (trace url {}))
([url opts] (request (assoc opts :method :trace :url url))))
(defn options
"Issue a synchronous HTTP OPTIONS request. This will raise an exception if an
error is returned."
([url] (options url {}))
([url opts] (request (assoc opts :method :options :url url))))
(defn patch
"Issue a synchronous HTTP PATCH request. This will raise an exception if an
error is returned."
([url] (patch url {}))
([url opts] (request (assoc opts :method :patch :url url))))