Skip to content

Commit

Permalink
Append the path to the github url with slash when necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
phmeier-nubank committed Jul 9, 2021
1 parent 91edb17 commit 7883ac3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/clj_github/httpkit_client.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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)))))

Expand All @@ -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)}
Expand Down
4 changes: 4 additions & 0 deletions test/clj_github/httpkit_client_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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"}}
Expand Down

0 comments on commit 7883ac3

Please sign in to comment.