diff options
| author | eltrac <hi@eltr.ac> | 2026-09-14 18:12:39 +0800 |
|---|---|---|
| committer | eltrac <hi@eltr.ac> | 2026-09-15 11:30:50 +0800 |
| commit | 9d30e52d4fd7c921683fd8a55c818ff7fd3ef7fe (patch) | |
| tree | 18a35d736635271c40e17288d7babce65a32d04d /src/weepinbell/http/client.clj | |
| parent | 2ddf4e3fe155b1e4ff5dde8a587758d37d665ed4 (diff) | |
httpclient, webhook, verifier: add weepinbell.http.client to wrap org.httpkit.client
comment
Diffstat (limited to 'src/weepinbell/http/client.clj')
| -rw-r--r-- | src/weepinbell/http/client.clj | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/src/weepinbell/http/client.clj b/src/weepinbell/http/client.clj new file mode 100644 index 0000000..34cd458 --- /dev/null +++ b/src/weepinbell/http/client.clj @@ -0,0 +1,69 @@ +(ns weepinbell.http.client
+ "HTTP client adapter. Only this adapter depends directly on external HTTP client library.
+ The rest of the codebase uses this adapter to make HTTP requests.
+ All HTTP client related changeability and complexity is containend within here,
+ for instance, all Exceptions are caught inside and returned as normal value."
+ (:refer-clojure :exclude [get])
+ (:require [org.httpkit.client :as hk]))
+
+(def default-options
+ {:user-agent "WebmentionBot"
+ :timeout 15000})
+
+(defn- wrap-response [response]
+ (assoc response :content-length (or (get-in response [:headers "Content-Length"])
+ (get-in response [:headers "content-length"]))))
+
+(defn- wrap-promise [promised-response]
+ (future (wrap-response @promised-response)))
+
+(defn post
+ "Sends POST request asynchronously. Returns a promise/future."
+ ([url]
+ (post url nil))
+ ([url options]
+ (post url options nil))
+ ([url options callbacks]
+ (-> url
+ (hk/post (merge default-options options) callbacks)
+ wrap-promise)))
+
+(defn get
+ "Sends GET request asynchronously. Returns a promise/future."
+ ([url]
+ (get url nil))
+ ([url options]
+ (get url options nil))
+ ([url options callbacks]
+ (-> url
+ (hk/get (merge default-options options) callbacks)
+ wrap-promise)))
+
+(defn get!
+ "Sends GET request synchronously. Returns response."
+ ([url]
+ @(get url nil))
+ ([url options]
+ @(get url options nil))
+ ([url options callbacks]
+ @(get url options callbacks)))
+
+(defn head
+ "Sends HEAD request asynchronously. Returns a promise/future."
+ ([url]
+ (head url nil))
+ ([url options]
+ (head url options nil))
+ ([url options callbacks]
+ (-> url
+ (hk/head (merge default-options options) callbacks)
+ wrap-promise)))
+
+(defn head!
+ "Sends HEAD request synchronously. Returns a response."
+ ([url]
+ @(head url nil))
+ ([url options]
+ @(head url options nil))
+ ([url options callbacks]
+ @(head url options callbacks)))
|
