Merge pull request #46 from adreyer/add-request

Add a general request function to the client protocol.
This commit is contained in:
Preben Ingvaldsen 2016-02-02 10:37:49 -08:00
commit 9d032c1a69
5 changed files with 31 additions and 18 deletions

View file

@ -334,7 +334,7 @@
opts :- common/RequestOptions opts :- common/RequestOptions
callback :- common/ResponseCallbackFn callback :- common/ResponseCallbackFn
http-context :- HttpContext] http-context :- HttpContext]
(let [;; Create an Apache AsyncResponseConsumer that will return the response to us as soon as it is (let [;; Create an Apache AsyncResponseConsumer that will return the response to us as soon as it is
;; available then send the response body asynchronously ;; available then send the response body asynchronously
consumer (StreamingAsyncResponseConsumer. consumer (StreamingAsyncResponseConsumer.
(FnDeliverable. (FnDeliverable.
@ -451,19 +451,21 @@
(let [client (create-default-client opts)] (let [client (create-default-client opts)]
(reify common/HTTPClient (reify common/HTTPClient
(get [this url] (common/get this url {})) (get [this url] (common/get this url {}))
(get [_ url opts] (request-with-client (assoc opts :method :get :url url) nil client)) (get [this url opts] (common/make-request this url :get opts))
(head [this url] (common/head this url {})) (head [this url] (common/head this url {}))
(head [_ url opts] (request-with-client (assoc opts :method :head :url url) nil client)) (head [this url opts] (common/make-request this url :head opts))
(post [this url] (common/post this url {})) (post [this url] (common/post this url {}))
(post [_ url opts] (request-with-client (assoc opts :method :post :url url) nil client)) (post [this url opts] (common/make-request this url :post opts))
(put [this url] (common/put this url {})) (put [this url] (common/put this url {}))
(put [_ url opts] (request-with-client (assoc opts :method :put :url url) nil client)) (put [this url opts] (common/make-request this url :put opts))
(delete [this url] (common/delete this url {})) (delete [this url] (common/delete this url {}))
(delete [_ url opts] (request-with-client (assoc opts :method :delete :url url) nil client)) (delete [this url opts] (common/make-request this url :delete opts))
(trace [this url] (common/trace this url {})) (trace [this url] (common/trace this url {}))
(trace [_ url opts] (request-with-client (assoc opts :method :trace :url url) nil client)) (trace [this url opts] (common/make-request this url :trace opts))
(options [this url] (common/options this url {})) (options [this url] (common/options this url {}))
(options [_ url opts] (request-with-client (assoc opts :method :options :url url) nil client)) (options [this url opts] (common/make-request this url :post opts))
(patch [this url] (common/patch this url {})) (patch [this url] (common/patch this url {}))
(patch [_ url opts] (request-with-client (assoc opts :method :patch :url url) nil client)) (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) nil client))
(close [_] (.close client))))) (close [_] (.close client)))))

View file

@ -20,6 +20,7 @@
(trace [this url] [this url opts]) (trace [this url] [this url opts])
(options [this url] [this url opts]) (options [this url] [this url opts])
(patch [this url] [this url opts]) (patch [this url] [this url opts])
(make-request [this url method] [this url method opts])
(close [this])) (close [this]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

View file

@ -44,21 +44,23 @@
(let [client (async/create-default-client opts)] (let [client (async/create-default-client opts)]
(reify common/HTTPClient (reify common/HTTPClient
(get [this url] (common/get this url {})) (get [this url] (common/get this url {}))
(get [_ url opts] (request-with-client (assoc opts :method :get :url url) client)) (get [this url opts] (common/make-request this url :get opts))
(head [this url] (common/head this url {})) (head [this url] (common/head this url {}))
(head [_ url opts] (request-with-client (assoc opts :method :head :url url) client)) (head [this url opts] (common/make-request this url :head opts))
(post [this url] (common/post this url {})) (post [this url] (common/post this url {}))
(post [_ url opts] (request-with-client (assoc opts :method :post :url url) client)) (post [this url opts] (common/make-request this url :post opts))
(put [this url] (common/put this url {})) (put [this url] (common/put this url {}))
(put [_ url opts] (request-with-client (assoc opts :method :put :url url) client)) (put [this url opts] (common/make-request this url :put opts))
(delete [this url] (common/delete this url {})) (delete [this url] (common/delete this url {}))
(delete [_ url opts] (request-with-client (assoc opts :method :delete :url url) client)) (delete [this url opts] (common/make-request this url :delete opts))
(trace [this url] (common/trace this url {})) (trace [this url] (common/trace this url {}))
(trace [_ url opts] (request-with-client (assoc opts :method :trace :url url) client)) (trace [this url opts] (common/make-request this url :trace opts))
(options [this url] (common/options this url {})) (options [this url] (common/options this url {}))
(options [_ url opts] (request-with-client (assoc opts :method :options :url url) client)) (options [this url opts] (common/make-request this url :post opts))
(patch [this url] (common/patch this url {})) (patch [this url] (common/patch this url {}))
(patch [_ url opts] (request-with-client (assoc opts :method :patch :url url) client)) (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))
(close [_] (.close client))))) (close [_] (.close client)))))
(defn get (defn get

View file

@ -105,6 +105,10 @@
(let [response (common/patch client "http://localhost:10000/hello/")] (let [response (common/patch client "http://localhost:10000/hello/")]
(is (= 200 (:status @response))) (is (= 200 (:status @response)))
(is (= "Hello, World!" (slurp (:body @response)))))) (is (= "Hello, World!" (slurp (:body @response))))))
(testing "GET request via request function with persistent async client"
(let [response (common/make-request client "http://localhost:10000/hello/" :get)]
(is (= 200 (:status @response)))
(is (= "Hello, World!" (slurp (:body @response))))))
(testing "client closes properly" (testing "client closes properly"
(common/close client) (common/close client)
(is (thrown? IllegalStateException (is (thrown? IllegalStateException

View file

@ -162,6 +162,10 @@
(let [response (common/patch client "http://localhost:10000/hello/")] (let [response (common/patch client "http://localhost:10000/hello/")]
(is (= 200 (:status response))) (is (= 200 (:status response)))
(is (= "Hello, World!" (slurp (:body response)))))) (is (= "Hello, World!" (slurp (:body response))))))
(testing "GET request via request function with persistent sync client"
(let [response (common/make-request client "http://localhost:10000/hello/" :get)]
(is (= 200 (:status response)))
(is (= "Hello, World!" (slurp (:body response))))))
(testing "client closes properly" (testing "client closes properly"
(common/close client) (common/close client)
(is (thrown? IllegalStateException (is (thrown? IllegalStateException
@ -713,4 +717,4 @@
{:socket-timeout-milliseconds 2000})] {:socket-timeout-milliseconds 2000})]
(let [response (common/get client url {:as :text})] (let [response (common/get client url {:as :text})]
(is (= 200 (:status response))) (is (= 200 (:status response)))
(is (= "Hello, World!" (:body response))))))))))) (is (= "Hello, World!" (:body response)))))))))))