From 7883ac36d80f109ea8d39f0b9ab56c6d7bb77934 Mon Sep 17 00:00:00 2001 From: Philipp Meier Date: Fri, 9 Jul 2021 10:26:14 +0200 Subject: [PATCH] Append the path to the github url with slash when necessary --- src/clj_github/httpkit_client.clj | 14 ++++++++++---- test/clj_github/httpkit_client_test.clj | 4 ++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/clj_github/httpkit_client.clj b/src/clj_github/httpkit_client.clj index 1d1ddf0..d9d86bd 100644 --- a/src/clj_github/httpkit_client.clj +++ b/src/clj_github/httpkit_client.clj @@ -11,12 +11,17 @@ (defn get-installation-token [{:keys [token-fn]}] (token-fn)) +(defn- append-url-path [baseurl path] + (str baseurl (when-not (or (.endsWith baseurl "/") + (.startsWith path "/")) + "/") path)) + (defn- prepare - [{:keys [token-fn]} {:keys [path method body] :or {method :get} :as request}] + [{:keys [token-fn]} {:keys [path method body] :or {method :get path ""} :as request}] (-> request (assoc :method method) (assoc-some :body (and body (cheshire/generate-string body))) - (assoc :url (str github-url path)) + (assoc :url (append-url-path github-url path)) (assoc-in [:headers "Content-Type"] "application/json") (assoc-in [:headers "Authorization"] (str "Bearer " (token-fn))))) @@ -30,12 +35,13 @@ (get-in response [:headers "Content-Type"]))) (defn request [client req-map] - (let [response @(httpkit/request (prepare client req-map))] + (let [request (prepare client req-map) + response @(httpkit/request request)] (if (success-codes (:status response)) (update response :body (partial parse-body (content-type response))) (throw (ex-info "Request to GitHub failed" {:response (select-keys response [:status :body])}))))) -(defn new-client [{:keys [app-id private-key token org] :as opts}] +(defn new-client [{:keys [app-id private-key token org token-fn] :as opts}] (cond token {:token-fn (constantly token)} diff --git a/test/clj_github/httpkit_client_test.clj b/test/clj_github/httpkit_client_test.clj index b499112..b461609 100644 --- a/test/clj_github/httpkit_client_test.clj +++ b/test/clj_github/httpkit_client_test.clj @@ -24,6 +24,10 @@ (with-fake-http [{:url "https://api.github.com/path"} {:status 200}] (is (match? {:status 200} (sut/request client {:path "/path"}))))) + (testing "path is appended to url with optional slash" + (with-fake-http [{:url "https://api.github.com/path"} + {:status 200}] + (is (match? {:status 200} (sut/request client {:path "path"}))))) (testing "github token is added to authorization header" (with-fake-http [{:headers {"Authorization" "Bearer token" "Content-Type" "application/json"}}