Skip to content

Commit ff91bc6

Browse files
atw-grdakrone
authored andcommitted
Add :http-client-builder option. (#490)
This adds an advanced option for specifying the `HttpClientBuilder`.
1 parent e00f06f commit ff91bc6

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

README.org

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1384,6 +1384,14 @@ Each of these variables is a sequence of functions of two arguments, the http bu
13841384

13851385
The functions are run in the order they are passed in (inside a =doseq=).
13861386

1387+
By specifying =:http-client-builder=, your own instance of
1388+
=HttpClientBuilder= will be used. A supplied =HttpClientBuilder= which
1389+
sets the connection manager, redirect strategy, retry handler, route
1390+
planner, cache, or cookie spec registry may find these overridden by
1391+
clj-http's =:connection-manager=, =:redirect-strategy=,
1392+
=:retry-handler=, =:cache=, or =:cookie-policy-registry= or
1393+
=:cookie-spec=, respectively.
1394+
13871395
* Development
13881396
:PROPERTIES:
13891397
:CUSTOM_ID: h-65bbf017-2e8b-4c43-824b-24b89cc27a70

src/clj_http/core.clj

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,15 +309,19 @@
309309
[{:keys [retry-handler request-interceptor
310310
response-interceptor proxy-host proxy-port
311311
http-builder-fns cookie-spec
312-
cookie-policy-registry]
312+
cookie-policy-registry
313+
^HttpClientBuilder http-client-builder]
313314
:as req}
314315
caching?
315316
conn-mgr
316317
& [http-url proxy-ignore-hosts]]
317318
;; have to let first, otherwise we get a reflection warning on (.build)
318319
(let [cache? (opt req :cache)
319-
builder (-> (if caching?
320+
builder (-> (cond
321+
http-client-builder http-client-builder
322+
caching?
320323
^HttpClientBuilder (CachingHttpClientBuilder/create)
324+
:else
321325
^HttpClientBuilder (HttpClients/custom))
322326
(.setConnectionManager conn-mgr)
323327
(.setRedirectStrategy

test/clj_http/test/core_test.clj

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,23 @@
740740
"Headers should have included the new default headers")
741741
(is (not (realized? error)))))
742742

743+
(deftest ^:integration test-custom-http-client-builder
744+
(run-server)
745+
(let [methods (atom nil)
746+
resp (client/get
747+
(localhost "/get")
748+
{:http-client-builder
749+
(-> (org.apache.http.impl.client.HttpClientBuilder/create)
750+
(.setRequestExecutor
751+
(proxy [org.apache.http.protocol.HttpRequestExecutor] []
752+
(execute [request connection context]
753+
(->> request
754+
.getRequestLine
755+
.getMethod
756+
(swap! methods conj))
757+
(proxy-super execute request connection context)))))})]
758+
(is (= ["GET"] @methods))))
759+
743760
(deftest ^:integration test-bad-redirects
744761
(run-server)
745762
(try

0 commit comments

Comments
 (0)