Skip to content

Commit

Permalink
yo
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude committed Apr 18, 2024
1 parent 9a71681 commit 0e06738
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 34 deletions.
31 changes: 16 additions & 15 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ Options used to create the (implicit) default client.
```

Convenience wrapper for [`request`](#babashka.http-client/request) with method `:delete`
<p><sub><a href="https://github.com/babashka/http-client/blob/main/src/babashka/http_client.clj#L133-L138">Source</a></sub></p>
<p><sub><a href="https://github.com/babashka/http-client/blob/main/src/babashka/http_client.clj#L134-L139">Source</a></sub></p>

## <a name="babashka.http-client/get">`get`</a><a name="babashka.http-client/get"></a>
``` clojure
Expand All @@ -190,7 +190,7 @@ Convenience wrapper for [`request`](#babashka.http-client/request) with method `
```

Convenience wrapper for [`request`](#babashka.http-client/request) with method `:get`
<p><sub><a href="https://github.com/babashka/http-client/blob/main/src/babashka/http_client.clj#L126-L131">Source</a></sub></p>
<p><sub><a href="https://github.com/babashka/http-client/blob/main/src/babashka/http_client.clj#L127-L132">Source</a></sub></p>

## <a name="babashka.http-client/head">`head`</a><a name="babashka.http-client/head"></a>
``` clojure
Expand All @@ -200,7 +200,7 @@ Convenience wrapper for [`request`](#babashka.http-client/request) with method `
```

Convenience wrapper for [`request`](#babashka.http-client/request) with method `:head`
<p><sub><a href="https://github.com/babashka/http-client/blob/main/src/babashka/http_client.clj#L140-L145">Source</a></sub></p>
<p><sub><a href="https://github.com/babashka/http-client/blob/main/src/babashka/http_client.clj#L141-L146">Source</a></sub></p>

## <a name="babashka.http-client/patch">`patch`</a><a name="babashka.http-client/patch"></a>
``` clojure
Expand All @@ -210,7 +210,7 @@ Convenience wrapper for [`request`](#babashka.http-client/request) with method `
```

Convenience wrapper for [`request`](#babashka.http-client/request) with method `:patch`
<p><sub><a href="https://github.com/babashka/http-client/blob/main/src/babashka/http_client.clj#L154-L161">Source</a></sub></p>
<p><sub><a href="https://github.com/babashka/http-client/blob/main/src/babashka/http_client.clj#L155-L162">Source</a></sub></p>

## <a name="babashka.http-client/post">`post`</a><a name="babashka.http-client/post"></a>
``` clojure
Expand All @@ -220,7 +220,7 @@ Convenience wrapper for [`request`](#babashka.http-client/request) with method `
```

Convenience wrapper for [`request`](#babashka.http-client/request) with method `:post`
<p><sub><a href="https://github.com/babashka/http-client/blob/main/src/babashka/http_client.clj#L147-L152">Source</a></sub></p>
<p><sub><a href="https://github.com/babashka/http-client/blob/main/src/babashka/http_client.clj#L148-L153">Source</a></sub></p>

## <a name="babashka.http-client/put">`put`</a><a name="babashka.http-client/put"></a>
``` clojure
Expand All @@ -230,7 +230,7 @@ Convenience wrapper for [`request`](#babashka.http-client/request) with method `
```

Convenience wrapper for [`request`](#babashka.http-client/request) with method `:put`
<p><sub><a href="https://github.com/babashka/http-client/blob/main/src/babashka/http_client.clj#L163-L170">Source</a></sub></p>
<p><sub><a href="https://github.com/babashka/http-client/blob/main/src/babashka/http_client.clj#L164-L171">Source</a></sub></p>

## <a name="babashka.http-client/request">`request`</a><a name="babashka.http-client/request"></a>
``` clojure
Expand All @@ -247,7 +247,9 @@ Perform request. Returns map with at least `:body`, `:status`
* `:headers` - a map of headers
* `:method` - the request method: `:get`, `:post`, `:head`, `:delete`, `:patch` or `:put`
* `:interceptors` - custom interceptor chain
* `:client` - a client as produced by [`client`](#babashka.http-client/client). If not provided a default client will be used.
* `:client` - a client as produced by [`client`](#babashka.http-client/client) or a clojure function. If not provided a default client will be used.
When providing :client with a a clojure function, it will be called with the Clojure representation of
the request which can be useful for testing.
* `:query-params` - a map of query params. The values can be a list to send multiple params with the same key.
* `:form-params` - a map of form params to send in the request body.
* `:body` - a file, inputstream or string to send as the request body.
Expand All @@ -259,8 +261,7 @@ Perform request. Returns map with at least `:body`, `:status`
* `:timeout` - request timeout in milliseconds
* `:throw` - throw on exceptional status codes, all other than `#{200 201 202 203 204 205 206 207 300 301 302 303 304 307}`
* `:version` - the HTTP version: `:http1.1` or `:http2`.

<p><sub><a href="https://github.com/babashka/http-client/blob/main/src/babashka/http_client.clj#L100-L124">Source</a></sub></p>
<p><sub><a href="https://github.com/babashka/http-client/blob/main/src/babashka/http_client.clj#L100-L125">Source</a></sub></p>

-----
# <a name="babashka.http-client.interceptors">babashka.http-client.interceptors</a>
Expand Down Expand Up @@ -293,15 +294,15 @@ Request: adds `:authorization` header based on `:basic-auth` (a map


Request: construct uri from map
<p><sub><a href="https://github.com/babashka/http-client/blob/main/src/babashka/http_client/interceptors.clj#L220-L226">Source</a></sub></p>
<p><sub><a href="https://github.com/babashka/http-client/blob/main/src/babashka/http_client/interceptors.clj#L222-L228">Source</a></sub></p>

## <a name="babashka.http-client.interceptors/decode-body">`decode-body`</a><a name="babashka.http-client.interceptors/decode-body"></a>




Response: based on the value of `:as` in request, decodes as `:string`, `:stream` or `:bytes`. Defaults to `:string`.
<p><sub><a href="https://github.com/babashka/http-client/blob/main/src/babashka/http_client/interceptors.clj#L208-L218">Source</a></sub></p>
<p><sub><a href="https://github.com/babashka/http-client/blob/main/src/babashka/http_client/interceptors.clj#L208-L220">Source</a></sub></p>

## <a name="babashka.http-client.interceptors/decompress-body">`decompress-body`</a><a name="babashka.http-client.interceptors/decompress-body"></a>

Expand All @@ -317,7 +318,7 @@ Response: decompresses body based on "content-encoding" header. Valid values: `


Default interceptor chain. Interceptors are called in order for request and in reverse order for response.
<p><sub><a href="https://github.com/babashka/http-client/blob/main/src/babashka/http_client/interceptors.clj#L253-L264">Source</a></sub></p>
<p><sub><a href="https://github.com/babashka/http-client/blob/main/src/babashka/http_client/interceptors.clj#L256-L267">Source</a></sub></p>

## <a name="babashka.http-client.interceptors/form-params">`form-params`</a><a name="babashka.http-client.interceptors/form-params"></a>

Expand All @@ -333,7 +334,7 @@ Request: encodes `:form-params` map and adds `:body`.


Adds appropriate body and header if making a multipart request.
<p><sub><a href="https://github.com/babashka/http-client/blob/main/src/babashka/http_client/interceptors.clj#L241-L251">Source</a></sub></p>
<p><sub><a href="https://github.com/babashka/http-client/blob/main/src/babashka/http_client/interceptors.clj#L244-L254">Source</a></sub></p>

## <a name="babashka.http-client.interceptors/oauth-token">`oauth-token`</a><a name="babashka.http-client.interceptors/oauth-token"></a>

Expand All @@ -358,13 +359,13 @@ Request: encodes `:query-params` map and appends to `:uri`.


Response: throw on exceptional status codes
<p><sub><a href="https://github.com/babashka/http-client/blob/main/src/babashka/http_client/interceptors.clj#L231-L239">Source</a></sub></p>
<p><sub><a href="https://github.com/babashka/http-client/blob/main/src/babashka/http_client/interceptors.clj#L233-L242">Source</a></sub></p>

## <a name="babashka.http-client.interceptors/unexceptional-statuses">`unexceptional-statuses`</a><a name="babashka.http-client.interceptors/unexceptional-statuses"></a>



<p><sub><a href="https://github.com/babashka/http-client/blob/main/src/babashka/http_client/interceptors.clj#L228-L229">Source</a></sub></p>
<p><sub><a href="https://github.com/babashka/http-client/blob/main/src/babashka/http_client/interceptors.clj#L230-L231">Source</a></sub></p>

## <a name="babashka.http-client.interceptors/uri-with-query">`uri-with-query`</a><a name="babashka.http-client.interceptors/uri-with-query"></a>
``` clojure
Expand Down
7 changes: 4 additions & 3 deletions src/babashka/http_client.clj
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@
* `:headers` - a map of headers
* `:method` - the request method: `:get`, `:post`, `:head`, `:delete`, `:patch` or `:put`
* `:interceptors` - custom interceptor chain
* `:client` - a client as produced by `client`. If not provided a default client will be used.
* `:client` - a client as produced by `client` or a clojure function. If not provided a default client will be used.
When providing :client with a a clojure function, it will be called with the Clojure representation of
the request which can be useful for testing.
* `:query-params` - a map of query params. The values can be a list to send multiple params with the same key.
* `:form-params` - a map of form params to send in the request body.
* `:body` - a file, inputstream or string to send as the request body.
Expand All @@ -118,8 +120,7 @@
* `:async-catch` - a function that is called on the async result if exceptional
* `:timeout` - request timeout in milliseconds
* `:throw` - throw on exceptional status codes, all other than `#{200 201 202 203 204 205 206 207 300 301 302 303 304 307}`
* `:version` - the HTTP version: `:http1.1` or `:http2`.
"
* `:version` - the HTTP version: `:http1.1` or `:http2`."
[opts]
(i/request opts))

Expand Down
15 changes: 9 additions & 6 deletions src/babashka/http_client/interceptors.clj
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,12 @@
:response (fn [resp]
(let [as (or (-> resp :request :as) :string)
body (:body resp)
body (case as
:string (slurp body)
:stream body
:bytes (stream-bytes body))]
body (if (instance? java.io.InputStream body)
(case as
:string (slurp body)
:stream body
:bytes (stream-bytes body))
body)]
(assoc resp :body body)))})

(def construct-uri
Expand All @@ -232,11 +234,12 @@
"Response: throw on exceptional status codes"
{:name ::throw-on-exceptional-status-code
:response (fn [resp]
(let [status (:status resp)]
(if-let [status (:status resp)]
(if (or (false? (some-> resp :request :throw))
(contains? unexceptional-statuses status))
resp
(throw (ex-info (str "Exceptional status code: " status) resp)))))})
(throw (ex-info (str "Exceptional status code: " status) resp)))
resp))})

(def multipart
"Adds appropriate body and header if making a multipart request."
Expand Down
17 changes: 11 additions & 6 deletions src/babashka/http_client/internal.clj
Original file line number Diff line number Diff line change
Expand Up @@ -301,19 +301,24 @@
[{:keys [client raw] :as req}]
(let [client (or client @default-client)
request-defaults (:request client)
^HttpClient client (or (:client client) client)
client* (or (:client client) client)
^HttpClient client client*
ring-client (when (ifn? client*)
client*)
req (merge-with merge-opts request-defaults req)
req (update req :headers aux/prefer-string-keys)
request-interceptors (or (:interceptors req)
interceptors/default-interceptors)
req (apply-interceptors req request-interceptors :request)
req' (ring->HttpRequest req)
req' (when-not ring-client (ring->HttpRequest req))
async (:async req)
resp (if async
(.sendAsync client req' (HttpResponse$BodyHandlers/ofInputStream))
(.send client req' (HttpResponse$BodyHandlers/ofInputStream)))]
resp (if ring-client
(ring-client req)
(if async
(.sendAsync client req' (HttpResponse$BodyHandlers/ofInputStream))
(.send client req' (HttpResponse$BodyHandlers/ofInputStream))))]
(if raw resp
(let [resp (then resp response->map)
(let [resp (if ring-client resp (then resp response->map))
resp (then resp (fn [resp]
(assoc resp :request req)))
resp (reduce (fn [resp interceptor]
Expand Down
16 changes: 12 additions & 4 deletions test/babashka/http_client_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -501,16 +501,16 @@
(nil? (.getProtocols params)))))
(let [params (http/->SSLParameters {:ciphers ["SSL_NULL_WITH_NULL_NULL"]})]
(is (and (instance? javax.net.ssl.SSLParameters params)
(= (first (.getCipherSuites params)) "SSL_NULL_WITH_NULL_NULL"))))
(= "SSL_NULL_WITH_NULL_NULL" (first (.getCipherSuites params))))))
(let [params (http/->SSLParameters {:protocols ["TLSv1"]})]
(is (and (instance? javax.net.ssl.SSLParameters params)
(= (first (.getProtocols params)) "TLSv1"))))
(= "TLSv1" (first (.getProtocols params))))))
(let [params-from-opts (http/->SSLParameters {:ciphers ["SSL_NULL_WITH_NULL_NULL"]
:protocols ["TLSv1"]})
params-from-params (http/->SSLParameters params-from-opts)]
(is (and (instance? javax.net.ssl.SSLParameters params-from-params)
(= (first (.getCipherSuites params-from-params)) "SSL_NULL_WITH_NULL_NULL")
(= (first (.getProtocols params-from-params)) "TLSv1")))))
(= "SSL_NULL_WITH_NULL_NULL" (first (.getCipherSuites params-from-params)))
(= "TLSv1" (first (.getProtocols params-from-params)))))))

(deftest executor-test
(testing "nil passthrough"
Expand All @@ -534,6 +534,14 @@
(str (#'i/uri-with-query (java.net.URI. "https://borkdude:[email protected]:80/?q=1#/dude")
"q=%26moo"))))))

(deftest ring-client-test
(let [resp (http/get "https://clojure.org"
{:client (fn [req]
{:body (java.io.ByteArrayInputStream. (.getBytes "Hello"))
:clojure (= "https://clojure.org" (str (:uri req)))})})]
(is (:clojure resp))
(is (= "Hello" (:body resp)))))

(comment
(run-server)
(stop-server))

0 comments on commit 0e06738

Please sign in to comment.