(TK-27) Add request-with-client functions

Add request-with-client functions to the async and sync
namespaces. These now do all the work of the request functions,
so that the request functions cannot directly be passed a client
by the user.
This commit is contained in:
Preben Ingvaldsen 2014-07-17 13:28:33 -07:00
parent d7e283df42
commit e586ea53c8
3 changed files with 61 additions and 48 deletions

View file

@ -236,6 +236,28 @@
(.start client)
client))
(schema/defn ^:always-validate request-with-client :- common/ResponsePromise
[opts :- common/RawUserRequestOptions
callback :- common/ResponseCallbackFn
client]
(let [persistent (not (nil? client))
defaults {:headers {}
:body nil
:decompress-body true
:as :stream}
opts (assoc (merge defaults opts) :persistent persistent)
client-opts (extract-client-opts opts)
client (or client (create-default-client client-opts))
{:keys [method url body] :as coerced-opts} (coerce-opts opts)
request (construct-request method url)
result (promise)]
(.setHeaders request (:headers coerced-opts))
(when body
(.setEntity request body))
(.execute client request
(future-callback client result opts callback))
result))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Public
@ -273,27 +295,7 @@
(request opts nil))
([opts :- common/RawUserRequestOptions
callback :- common/ResponseCallbackFn]
(request opts callback nil))
([opts :- common/RawUserRequestOptions
callback :- common/ResponseCallbackFn
client]
(let [persistent (not (nil? client))
defaults {:headers {}
:body nil
:decompress-body true
:as :stream}
opts (assoc (merge defaults opts) :persistent persistent)
client-opts (extract-client-opts opts)
client (or client (create-default-client client-opts))
{:keys [method url body] :as coerced-opts} (coerce-opts opts)
request (construct-request method url)
result (promise)]
(.setHeaders request (:headers coerced-opts))
(when body
(.setEntity request body))
(.execute client request
(future-callback client result opts callback))
result)))
(request-with-client opts callback nil)))
(schema/defn create-client :- common/HTTPClient
[opts :- common/ClientOptions]
@ -304,21 +306,21 @@
(.start client)
(reify common/HTTPClient
(get [this url] (common/get this url {}))
(get [_ url opts] (request (assoc opts :method :get :url url) nil client))
(get [_ url opts] (request-with-client (assoc opts :method :get :url url) nil client))
(head [this url] (common/head this url {}))
(head [_ url opts] (request (assoc opts :method :head :url url) nil client))
(head [_ url opts] (request-with-client (assoc opts :method :head :url url) nil client))
(post [this url] (common/post this url {}))
(post [_ url opts] (request (assoc opts :method :post :url url) nil client))
(post [_ url opts] (request-with-client (assoc opts :method :post :url url) nil client))
(put [this url] (common/put this url {}))
(put [_ url opts] (request (assoc opts :method :put :url url) nil client))
(put [_ url opts] (request-with-client (assoc opts :method :put :url url) nil client))
(delete [this url] (common/delete this url {}))
(delete [_ url opts] (request (assoc opts :method :delete :url url) nil client))
(delete [_ url opts] (request-with-client (assoc opts :method :delete :url url) nil client))
(trace [this url] (common/trace this url {}))
(trace [_ url opts] (request (assoc opts :method :trace :url url) nil client))
(trace [_ url opts] (request-with-client (assoc opts :method :trace :url url) nil client))
(options [this url] (common/options this url {}))
(options [_ url opts] (request (assoc opts :method :options :url url) nil client))
(options [_ url opts] (request-with-client (assoc opts :method :options :url url) nil client))
(patch [this url] (common/patch this url {}))
(patch [_ url opts] (request (assoc opts :method :patch :url url) nil client))
(patch [_ url opts] (request-with-client (assoc opts :method :patch :url url) nil client))
(close [_] (.close client)))))
(defn get

View file

@ -8,17 +8,26 @@
[puppetlabs.http.client.common :as common])
(:refer-clojure :exclude (get)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Private utility functions
(defn request-with-client
[req client]
(let [{:keys [error] :as resp} @(async/request-with-client req nil client)]
(if error
(throw error)
resp)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Public
(defn request
([req]
[req]
(let [{:keys [error] :as resp} @(async/request req nil)]
(if error
(throw error)
resp)))
([req client]
(let [{:keys [error] :as resp} @(async/request req nil client)]
(if error
(throw error)
resp))))
(schema/defn create-client :- common/HTTPClient
[opts :- common/ClientOptions]
@ -29,21 +38,21 @@
(.start client)
(reify common/HTTPClient
(get [this url] (common/get this url {}))
(get [_ url opts] (request (assoc opts :method :get :url url) client))
(get [_ url opts] (request-with-client (assoc opts :method :get :url url) client))
(head [this url] (common/head this url {}))
(head [_ url opts] (request (assoc opts :method :head :url url) client))
(head [_ url opts] (request-with-client (assoc opts :method :head :url url) client))
(post [this url] (common/post this url {}))
(post [_ url opts] (request (assoc opts :method :post :url url) client))
(post [_ url opts] (request-with-client (assoc opts :method :post :url url) client))
(put [this url] (common/put this url {}))
(put [_ url opts] (request (assoc opts :method :put :url url) client))
(put [_ url opts] (request-with-client (assoc opts :method :put :url url) client))
(delete [this url] (common/delete this url {}))
(delete [_ url opts] (request (assoc opts :method :delete :url url) client))
(delete [_ url opts] (request-with-client (assoc opts :method :delete :url url) client))
(trace [this url] (common/trace this url {}))
(trace [_ url opts] (request (assoc opts :method :trace :url url) client))
(trace [_ url opts] (request-with-client (assoc opts :method :trace :url url) client))
(options [this url] (common/options this url {}))
(options [_ url opts] (request (assoc opts :method :options :url url) client))
(options [_ url opts] (request-with-client (assoc opts :method :options :url url) client))
(patch [this url] (common/patch this url {}))
(patch [_ url opts] (request (assoc opts :method :patch :url url) client))
(patch [_ url opts] (request-with-client (assoc opts :method :patch :url url) client))
(close [_] (.close client)))))
(defn get

View file

@ -113,4 +113,6 @@
(let [response (common/patch client "http://localhost:10000/hello/")]
(is (= 200 (:status @response)))
(is (= "Hello, World!" (slurp (:body @response))))))
(common/close client)))))
(testing "client closes properly"
(common/close client)
(is (thrown? IllegalStateException (common/get client "http://localhost:10000/hello/"))))))))