(TK-27) Refactor client protocols

Refactor the async and sync client protocols into a single
protocol which resides in a new file, common.clj.
This commit is contained in:
Preben Ingvaldsen 2014-07-15 16:46:20 -07:00
parent 0b2755eef5
commit b807083c5a
5 changed files with 55 additions and 66 deletions

View file

@ -0,0 +1,13 @@
(ns puppetlabs.http.client.common
(:refer-clojure :exclude (get)))
(defprotocol HTTPClient
(get [this url] [this url opts])
(head [this url] [this url opts])
(post [this url] [this url opts])
(put [this url] [this url opts])
(delete [this url] [this url opts])
(trace [this url] [this url opts])
(options [this url] [this url opts])
(patch [this url] [this url opts])
(close [this]))

View file

@ -1,49 +1,36 @@
(ns puppetlabs.http.client.persistent-async
(:import (org.apache.http.impl.nio.client HttpAsyncClients))
(:require [schema.core :as schema]
[puppetlabs.http.client.common :as common]
[puppetlabs.http.client.schemas :as schemas]
[puppetlabs.http.client.async :refer [request configure-ssl]])
(:refer-clojure :exclude (get)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Async Client protocol
(defprotocol AsyncClient
(get [this url] [this url opts])
(head [this url] [this url opts])
(post [this url] [this url opts])
(put [this url] [this url opts])
(delete [this url] [this url opts])
(trace [this url] [this url opts])
(options [this url] [this url opts])
(patch [this url] [this url opts])
(close [this]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Public
(schema/defn create-client :- AsyncClient
(schema/defn create-client :- common/HTTPClient
[opts :- schemas/ClientOptions]
(let [opts (configure-ssl opts)
client (if (:ssl-context opts)
(.. (HttpAsyncClients/custom) (setSSLContext (:ssl-context opts)) build)
(HttpAsyncClients/createDefault))]
(.start client)
(reify AsyncClient
(get [this url] (get this url {}))
(reify common/HTTPClient
(get [this url] (common/get this url {}))
(get [_ url opts] (request (assoc opts :method :get :url url) nil client))
(head [this url] (head this url {}))
(head [this url] (common/head this url {}))
(head [_ url opts] (request (assoc opts :method :head :url url) nil client))
(post [this url] (post this url {}))
(post [this url] (common/post this url {}))
(post [_ url opts] (request (assoc opts :method :post :url url) nil client))
(put [this url] (put this url {}))
(put [this url] (common/put this url {}))
(put [_ url opts] (request (assoc opts :method :put :url url) nil client))
(delete [this url] (delete this url {}))
(delete [this url] (common/delete this url {}))
(delete [_ url opts] (request (assoc opts :method :delete :url url) nil client))
(trace [this url] (trace this url {}))
(trace [this url] (common/trace this url {}))
(trace [_ url opts] (request (assoc opts :method :trace :url url) nil client))
(options [this url] (options this url {}))
(options [this url] (common/options this url {}))
(options [_ url opts] (request (assoc opts :method :options :url url) nil client))
(patch [this url] (patch this url {}))
(patch [this url] (common/patch this url {}))
(patch [_ url opts] (request (assoc opts :method :patch :url url) nil client))
(close [_] (.close client)))))

View file

@ -1,50 +1,37 @@
(ns puppetlabs.http.client.persistent-sync
(:import (org.apache.http.impl.nio.client HttpAsyncClients))
(:require [schema.core :as schema]
[puppetlabs.http.client.common :as common]
[puppetlabs.http.client.schemas :as schemas]
[puppetlabs.http.client.sync :refer [request]]
[puppetlabs.http.client.async :refer [configure-ssl]])
(:refer-clojure :exclude (get)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Sync Client protocol
(defprotocol SyncClient
(get [this url] [this url opts])
(head [this url] [this url opts])
(post [this url] [this url opts])
(put [this url] [this url opts])
(delete [this url] [this url opts])
(trace [this url] [this url opts])
(options [this url] [this url opts])
(patch [this url] [this url opts])
(close [this]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Public
(schema/defn create-client :- SyncClient
(schema/defn create-client :- common/HTTPClient
[opts :- schemas/ClientOptions]
(let [opts (configure-ssl opts)
client (if (:ssl-context opts)
(.. (HttpAsyncClients/custom) (setSSLContext (:ssl-context opts)) build)
(HttpAsyncClients/createDefault))]
(.start client)
(reify SyncClient
(get [this url] (get this url {}))
(reify common/HTTPClient
(get [this url] (common/get this url {}))
(get [_ url opts] (request (assoc opts :method :get :url url) client))
(head [this url] (head this url {}))
(head [this url] (common/head this url {}))
(head [_ url opts] (request (assoc opts :method :head :url url) client))
(post [this url] (post this url {}))
(post [this url] (common/post this url {}))
(post [_ url opts] (request (assoc opts :method :post :url url) client))
(put [this url] (put this url {}))
(put [this url] (common/put this url {}))
(put [_ url opts] (request (assoc opts :method :put :url url) client))
(delete [this url] (delete this url {}))
(delete [this url] (common/delete this url {}))
(delete [_ url opts] (request (assoc opts :method :delete :url url) client))
(trace [this url] (trace this url {}))
(trace [this url] (common/trace this url {}))
(trace [_ url opts] (request (assoc opts :method :trace :url url) client))
(options [this url] (options this url {}))
(options [this url] (common/options this url {}))
(options [_ url opts] (request (assoc opts :method :options :url url) client))
(patch [this url] (patch this url {}))
(patch [this url] (common/patch this url {}))
(patch [_ url opts] (request (assoc opts :method :patch :url url) client))
(close [_] (.close client)))))

View file

@ -5,6 +5,7 @@
[puppetlabs.trapperkeeper.testutils.bootstrap :as testutils]
[puppetlabs.trapperkeeper.testutils.logging :as testlogging]
[puppetlabs.trapperkeeper.services.webserver.jetty9-service :as jetty9]
[puppetlabs.http.client.common :as common]
[puppetlabs.http.client.async :as async]
[puppetlabs.http.client.persistent-async :as p-async]
[schema.test :as schema-test]))
@ -82,35 +83,35 @@
{:webserver {:port 10000}}
(let [client (p-async/create-client {})]
(testing "HEAD request with persistent async client"
(let [response (p-async/head client "http://localhost:10000/hello/")]
(let [response (common/head client "http://localhost:10000/hello/")]
(is (= 200 (:status @response)))
(is (= nil (:body @response)))))
(testing "GET request with persistent async client"
(let [response (p-async/get client "http://localhost:10000/hello/")]
(let [response (common/get client "http://localhost:10000/hello/")]
(is (= 200 (:status @response)))
(is (= "Hello, World!" (slurp (:body @response))))))
(testing "POST request with persistent async client"
(let [response (p-async/post client "http://localhost:10000/hello/")]
(let [response (common/post client "http://localhost:10000/hello/")]
(is (= 200 (:status @response)))
(is (= "Hello, World!" (slurp (:body @response))))))
(testing "PUT request with persistent async client"
(let [response (p-async/put client "http://localhost:10000/hello/")]
(let [response (common/put client "http://localhost:10000/hello/")]
(is (= 200 (:status @response)))
(is (= "Hello, World!" (slurp (:body @response))))))
(testing "DELETE request with persistent async client"
(let [response (p-async/delete client "http://localhost:10000/hello/")]
(let [response (common/delete client "http://localhost:10000/hello/")]
(is (= 200 (:status @response)))
(is (= "Hello, World!" (slurp (:body @response))))))
(testing "TRACE request with persistent async client"
(let [response (p-async/trace client "http://localhost:10000/hello/")]
(let [response (common/trace client "http://localhost:10000/hello/")]
(is (= 200 (:status @response)))
(is (= "Hello, World!" (slurp (:body @response))))))
(testing "OPTIONS request with persistent async client"
(let [response (p-async/options client "http://localhost:10000/hello/")]
(let [response (common/options client "http://localhost:10000/hello/")]
(is (= 200 (:status @response)))
(is (= "Hello, World!" (slurp (:body @response))))))
(testing "PATCH request with persistent async client"
(let [response (p-async/patch client "http://localhost:10000/hello/")]
(let [response (common/patch client "http://localhost:10000/hello/")]
(is (= 200 (:status @response)))
(is (= "Hello, World!" (slurp (:body @response))))))
(.close client)))))
(common/close client)))))

View file

@ -11,6 +11,7 @@
[puppetlabs.trapperkeeper.services.webserver.jetty9-service :as jetty9]
[puppetlabs.http.client.sync :as sync]
[puppetlabs.http.client.persistent-sync :as p-sync]
[puppetlabs.http.client.common :as common]
[schema.test :as schema-test]
[clojure.java.io :as io]))
@ -87,38 +88,38 @@
{:webserver {:port 10000}}
(let [client (p-sync/create-client {})]
(testing "HEAD request with persistent sync client"
(let [response (p-sync/head client "http://localhost:10000/hello/")]
(let [response (common/head client "http://localhost:10000/hello/")]
(is (= 200 (:status response)))
(is (= nil (:body response)))))
(testing "GET request with persistent sync client"
(let [response (p-sync/get client "http://localhost:10000/hello/")]
(let [response (common/get client "http://localhost:10000/hello/")]
(is (= 200 (:status response)))
(is (= "Hello, World!" (slurp (:body response))))))
(testing "POST request with persistent sync client"
(let [response (p-sync/post client "http://localhost:10000/hello/")]
(let [response (common/post client "http://localhost:10000/hello/")]
(is (= 200 (:status response)))
(is (= "Hello, World!" (slurp (:body response))))))
(testing "PUT request with persistent sync client"
(let [response (p-sync/put client "http://localhost:10000/hello/")]
(let [response (common/put client "http://localhost:10000/hello/")]
(is (= 200 (:status response)))
(is (= "Hello, World!" (slurp (:body response))))))
(testing "DELETE request with persistent sync client"
(let [response (p-sync/delete client "http://localhost:10000/hello/")]
(let [response (common/delete client "http://localhost:10000/hello/")]
(is (= 200 (:status response)))
(is (= "Hello, World!" (slurp (:body response))))))
(testing "TRACE request with persistent sync client"
(let [response (p-sync/trace client "http://localhost:10000/hello/")]
(let [response (common/trace client "http://localhost:10000/hello/")]
(is (= 200 (:status response)))
(is (= "Hello, World!" (slurp (:body response))))))
(testing "OPTIONS request with persistent sync client"
(let [response (p-sync/options client "http://localhost:10000/hello/")]
(let [response (common/options client "http://localhost:10000/hello/")]
(is (= 200 (:status response)))
(is (= "Hello, World!" (slurp (:body response))))))
(testing "PATCH request with persistent sync client"
(let [response (p-sync/patch client "http://localhost:10000/hello/")]
(let [response (common/patch client "http://localhost:10000/hello/")]
(is (= 200 (:status response)))
(is (= "Hello, World!" (slurp (:body response))))))
(p-sync/close client)))))
(common/close client)))))
(deftest sync-client-as-test
(testlogging/with-test-logging