(TK-27) Put persistent client in own ns

Put the persistent client in its own namespace. Remove the
persistent- prefix from the front of the persistent client
http request functions.
This commit is contained in:
Preben Ingvaldsen 2014-07-15 15:04:28 -07:00
parent 54fa3d77fd
commit 27d5838ce3
4 changed files with 112 additions and 81 deletions

View file

@ -31,20 +31,6 @@
[clojure.tools.logging :as log])
(:refer-clojure :exclude (get)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Async Client protocol
(defprotocol async-client
(persist-get [this url] [this url opts])
(persist-head [this url] [this url opts])
(persist-post [this url] [this url opts])
(persist-put [this url] [this url opts])
(persist-delete [this url] [this url opts])
(persist-trace [this url] [this url opts])
(persist-options [this url] [this url opts])
(persist-patch [this url] [this url opts])
(close [this]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Private SSL configuration functions
@ -315,32 +301,6 @@
(future-callback client result opts callback))
result)))
(schema/defn create-client :- async-client
[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 async-client
(persist-get [this url] (persist-get this url {}))
(persist-get [_ url opts] (request (assoc opts :method :get :url url) nil client))
(persist-head [this url] (persist-head this url {}))
(persist-head [_ url opts] (request (assoc opts :method :head :url url) nil client))
(persist-post [this url] (persist-post this url {}))
(persist-post [_ url opts] (request (assoc opts :method :post :url url) nil client))
(persist-put [this url] (persist-put this url {}))
(persist-put [_ url opts] (request (assoc opts :method :put :url url) nil client))
(persist-delete [this url] (persist-delete this url {}))
(persist-delete [_ url opts] (request (assoc opts :method :delete :url url) nil client))
(persist-trace [this url] (persist-trace this url {}))
(persist-trace [_ url opts] (request (assoc opts :method :trace :url url) nil client))
(persist-options [this url] (persist-options this url {}))
(persist-options [_ url opts] (request (assoc opts :method :options :url url) nil client))
(persist-patch [this url] (persist-patch this url {}))
(persist-patch [_ url opts] (request (assoc opts :method :patch :url url) nil client))
(close [_] (.close client)))))
(defn get
"Issue an asynchronous HTTP GET request. This will raise an exception if an
error is returned."

View file

@ -0,0 +1,49 @@
(ns puppetlabs.http.client.persistent-async
(:import (org.apache.http.impl.nio.client HttpAsyncClients))
(:require [schema.core :as schema]
[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
[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 {}))
(get [_ url opts] (request (assoc opts :method :get :url url) nil client))
(head [this url] (head this url {}))
(head [_ url opts] (request (assoc opts :method :head :url url) nil client))
(post [this url] (post this url {}))
(post [_ url opts] (request (assoc opts :method :post :url url) nil client))
(put [this url] (put this url {}))
(put [_ url opts] (request (assoc opts :method :put :url url) nil client))
(delete [this url] (delete this url {}))
(delete [_ url opts] (request (assoc opts :method :delete :url url) nil client))
(trace [this url] (trace this url {}))
(trace [_ url opts] (request (assoc opts :method :trace :url url) nil client))
(options [this url] (options this url {}))
(options [_ url opts] (request (assoc opts :method :options :url url) nil client))
(patch [this url] (patch this url {}))
(patch [_ url opts] (request (assoc opts :method :patch :url url) nil client))
(close [_] (.close client)))))

View file

@ -72,44 +72,4 @@
(basic-test "OPTIONS" #(AsyncHttpClient/options %) async/options))
(deftest async-client-patch-test
(basic-test "PATCH" #(AsyncHttpClient/patch %) async/patch))
(deftest persistent-async-client-test
(testlogging/with-test-logging
(testutils/with-app-with-config app
[jetty9/jetty9-service test-web-service]
{:webserver {:port 10000}}
(let [client (async/create-client {})]
(testing "HEAD request with persistent async client"
(let [response (async/persist-head client "http://localhost:10000/hello/")]
(is (= 200 (:status @response)))
(is (= nil (:body @response)))))
(testing "GET request with persistent async client"
(let [response (async/persist-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 (async/persist-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 (async/persist-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 (async/persist-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 (async/persist-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 (async/persist-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 (async/persist-patch client "http://localhost:10000/hello/")]
(is (= 200 (:status @response)))
(is (= "Hello, World!" (slurp (:body @response))))))
(.close client)))))
(basic-test "PATCH" #(AsyncHttpClient/patch %) async/patch))

View file

@ -0,0 +1,62 @@
(ns puppetlabs.http.client.persistent-client-test
(:import (com.puppetlabs.http.client AsyncHttpClient RequestOptions))
(:require [clojure.test :refer :all]
[puppetlabs.trapperkeeper.core :as tk]
[puppetlabs.trapperkeeper.testutils.bootstrap :as testutils]
[puppetlabs.trapperkeeper.testutils.logging :as testlogging]
[puppetlabs.trapperkeeper.services.webserver.jetty9-service :as jetty9]
[puppetlabs.http.client.persistent-async :as p-async]
[schema.test :as schema-test]))
(use-fixtures :once schema-test/validate-schemas)
(defn app
[req]
{:status 200
:body "Hello, World!"})
(tk/defservice test-web-service
[[:WebserverService add-ring-handler]]
(init [this context]
(add-ring-handler app "/hello")
context))
(deftest persistent-async-client-test
(testlogging/with-test-logging
(testutils/with-app-with-config app
[jetty9/jetty9-service test-web-service]
{: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/")]
(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/")]
(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/")]
(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/")]
(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/")]
(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/")]
(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/")]
(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/")]
(is (= 200 (:status @response)))
(is (= "Hello, World!" (slurp (:body @response))))))
(.close client)))))