diff options
| author | eltrac <hi@eltr.ac> | 2026-09-10 18:18:03 +0800 |
|---|---|---|
| committer | eltrac <hi@eltr.ac> | 2026-09-10 18:18:03 +0800 |
| commit | 57b796fd46ae9d1fc12c3052d9dcfe5a0da76b2c (patch) | |
| tree | 751256fdfefb0398c81880431298390decb4137a /src | |
| parent | b4acf724966b2ab960c1caa00c922884750106b9 (diff) | |
refactor(verifier, webhook): replace clj-http with hato #9
Diffstat (limited to 'src')
| -rw-r--r-- | src/weepinbell/utils.clj | 28 | ||||
| -rw-r--r-- | src/weepinbell/verifier.clj | 17 | ||||
| -rw-r--r-- | src/weepinbell/webhook.clj | 4 |
3 files changed, 13 insertions, 36 deletions
diff --git a/src/weepinbell/utils.clj b/src/weepinbell/utils.clj index 79e8add..20ade7d 100644 --- a/src/weepinbell/utils.clj +++ b/src/weepinbell/utils.clj @@ -1,7 +1,7 @@ (ns weepinbell.utils (:import [org.apache.commons.validator.routines UrlValidator] [java.net URI]) - (:require [clj-http.client :as client] + (:require [hato.client :as hc] [clojure.string :as string] [clojure.walk :as walk] [clojure.java.io :as io] @@ -23,26 +23,6 @@ validator (UrlValidator. schemes)] (.isValid validator url))) -(defn get-content-type [url] - (try - (let [response (client/head url)] - (get-in response [:headers "Content-Type"])) - (catch clojure.lang.ExceptionInfo e - (let [status (:status (ex-data e))] - (timbre/error status " error trying to get content type of " url) - nil)) - (catch Exception e - (timbre/error "Error trying to get content type of " url - (.getMessage e)) - nil))) - -(defn plain-or-html? [content-type] - (cond - (nil? content-type) false - (string/includes? content-type "text/html") :html - (string/includes? content-type "text/plain") :text - :else false)) - (defn javamap->map [map] (->> (seq map) (into {}) walk/keywordize-keys)) @@ -54,10 +34,8 @@ parent-dir (.getParentFile file)] (when (and parent-dir (not (.exists parent-dir))) (.mkdirs parent-dir)) - (let [response (client/get url {:as :stream - :conn-timeout 10000 - :socket-timeout 30000 - :throw-exceptions true}) + (let [response (hc/get url {:as :stream + :timeout 30000}) content-length (get-in response [:headers "content-length"])] (timbre/info "Downloading [" url "] -> [" dest-path "]") (when content-length diff --git a/src/weepinbell/verifier.clj b/src/weepinbell/verifier.clj index 0e7ebd6..f8c9fe1 100644 --- a/src/weepinbell/verifier.clj +++ b/src/weepinbell/verifier.clj @@ -4,14 +4,12 @@ 1) source contains target URL. 2) source respond with 2** status code. 3) source's Content-Type is either HTML or plain text." - (:require [weepinbell.utils :refer [get-content-type plain-or-html?]] - [clojure.string :refer [includes?]] - [clj-http.client :as client] + (:require [clojure.string :refer [includes?]] + [hato.client :as hc] [taoensso.timbre :as timbre])) -(defn verify-content-types [source target] - (let [validate #(-> % get-content-type plain-or-html? boolean)] - (and (validate source) (validate target)))) +(defn- verify-content-type [content-type] + (#{:text/html :text/plain} content-type)) (defn verify-mention "Get source and follow redirects to check if target link is present." @@ -19,12 +17,13 @@ (try (timbre/info (str "Verifying mention from: " source)) ;; get first 1MB of source URL and verify - (let [{:keys [status body]} (client/get source {:throw false - :headers {"Range" "bytes=0-1048575"}})] + (let [{:keys [status body content-type]} (hc/get source {:headers {"Range" "bytes=0-1048575"}}) + target-content-type (:content-type (hc/head target))] ;; Verify status code (if (and (>= status 200) (< status 300)) ;; Verify Content-Types - (if (verify-content-types source target) + (if (and (verify-content-type content-type) + (verify-content-type target-content-type)) ;; Verify inclusion of target URL ;; TODO needs to be more robust (if (includes? (str body) target) diff --git a/src/weepinbell/webhook.clj b/src/weepinbell/webhook.clj index 8e3a5a8..c5a2bae 100644 --- a/src/weepinbell/webhook.clj +++ b/src/weepinbell/webhook.clj @@ -1,5 +1,5 @@ (ns weepinbell.webhook - (:require [clj-http.client :as client] + (:require [hato.client :as hc] [taoensso.timbre :as timbre] [weepinbell.utils :refer [valid-http-url?]])) @@ -15,7 +15,7 @@ (filter string?) (filter valid-http-url?)))] (->> hooks - (map #((client/post %) + (map #((hc/post %) (timbre/info "Webhook triggered: " %))) doall)))) |
