diff --git a/src/clj/puppetlabs/http/client/common.clj b/src/clj/puppetlabs/http/client/common.clj new file mode 100644 index 0000000..132d918 --- /dev/null +++ b/src/clj/puppetlabs/http/client/common.clj @@ -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])) diff --git a/src/clj/puppetlabs/http/client/persistent_async.clj b/src/clj/puppetlabs/http/client/persistent_async.clj index bf69c82..5408f44 100644 --- a/src/clj/puppetlabs/http/client/persistent_async.clj +++ b/src/clj/puppetlabs/http/client/persistent_async.clj @@ -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))))) \ No newline at end of file diff --git a/src/clj/puppetlabs/http/client/persistent_sync.clj b/src/clj/puppetlabs/http/client/persistent_sync.clj index ebb3f22..f6e94f0 100644 --- a/src/clj/puppetlabs/http/client/persistent_sync.clj +++ b/src/clj/puppetlabs/http/client/persistent_sync.clj @@ -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))))) \ No newline at end of file diff --git a/test/puppetlabs/http/client/async_plaintext_test.clj b/test/puppetlabs/http/client/async_plaintext_test.clj index d7392dc..12f24d3 100644 --- a/test/puppetlabs/http/client/async_plaintext_test.clj +++ b/test/puppetlabs/http/client/async_plaintext_test.clj @@ -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))))) \ No newline at end of file + (common/close client))))) \ No newline at end of file diff --git a/test/puppetlabs/http/client/sync_plaintext_test.clj b/test/puppetlabs/http/client/sync_plaintext_test.clj index 233e6d5..9a2e84b 100644 --- a/test/puppetlabs/http/client/sync_plaintext_test.clj +++ b/test/puppetlabs/http/client/sync_plaintext_test.clj @@ -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