Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Append the path to the github url with slash when necessary #10

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 0.4.2

- Join path with baseurl using a `/` where necessary
- Allow to specify the Content-Type and Authorization request headers manually

## 0.4.1

- Include httpkit error in exception when available
Expand Down
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject dev.nubank/clj-github "0.4.1"
(defproject dev.nubank/clj-github "0.4.2"
:description "A Clojure library for interacting with the github developer API"
:url "https://github.com/nubank/clj-github"
:license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0"
Expand Down
20 changes: 14 additions & 6 deletions src/clj_github/httpkit_client.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,20 @@
(defn get-installation-token [{:keys [token-fn]}]
(token-fn))

(defn- append-url-path [baseurl path]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wdyt about adding some unit tests for these cases

(append-url-path "github.com" "path")
(append-url-path "github.com/" "path")
(append-url-path "github.com" "/path")

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

baseurl -> base-url

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gabrielgiussi this is a private function and implicitly tested in https://github.com/nubank/clj-github/pull/10/files#diff-3d82479c9bb9ffd50aac8b0bc3dacf8754ae47fb4c95de6794a0f7d25e870a4cR27. Also the github base url is defined as a constant, I don't think adding these tests add much value.

(str baseurl (when-not (or (.endsWith baseurl "/")
(.startsWith path "/"))
"/") path))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To help make it distinguishable that path has nothing to do with the when-not expression could you either have one arg per line for the str function, or let-bind the when-not expr?


(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-in [:headers "Content-Type"] "application/json")
(assoc-in [:headers "Authorization"] (str "Bearer " (token-fn)))))
(assoc :url (append-url-path github-url path))
;; TODO http headers are case insensitive, also this could be keywords
(update-in [:headers "Content-Type"] #(or % "application/json"))
phmeier-nubank marked this conversation as resolved.
Show resolved Hide resolved
(update-in [:headers "Authorization"] #(or % (str "Bearer " (token-fn))))))

(defn- parse-body [content-type body]
(if (and content-type (re-find #"application/json" content-type))
Expand All @@ -30,14 +36,16 @@
(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])}
(:error response))))))

(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}]
{:pre [(or token app-id token-fn)]}
Copy link
Contributor

@gabrielgiussi gabrielgiussi Jul 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of just app-id shouldn't be (and app-id private-key org)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, I think it's actually (or token token-fn (and app-id private-key)).

(cond
token
{:token-fn (constantly token)}
Expand Down
30 changes: 29 additions & 1 deletion 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 All @@ -43,4 +47,28 @@
(with-fake-http [{} {:error cause :status nil}]
(let [e (try (sut/request client {}) (catch Exception e e))]
(is (re-matches #"(?i)Request to GitHub failed" (.getMessage e)))
(is (= cause (.getCause e)))))))))
(is (= cause (.getCause e)))))))
(testing "request headers"
(testing "Can use custom headers"
(with-fake-http [(fn [req]
(is (= "test" (get-in req [:headers "Test-Header"]))))
{:status 200}]
;; assertion is in fake http callback
(sut/request client {:headers {"Test-Header" "test"}})))
(testing "Can override authentication"
;; clj-github sets authorization header unless specified. Beware that the
;; implementation here is case sensitive whereas HTTP headers are not.
(with-fake-http [(fn [req]
(is (= "Test SomeValue" (get-in req [:headers "Authorization"]))))
{:status 200}]
;; assertion is in fake http callback
(sut/request client {:headers {"Authorization" "Test SomeValue"}})))
(testing "Can override content-type"
;; clj-github sets the content-type header unless specified. Beware that the
;; implementation here is case sensitive whereas HTTP headers are not.
(with-fake-http [(fn [req]
(is (= "test/test" (get-in req [:headers "Content-Type"]))))
{:status 200}]
;; assertion is in fake http callback
(sut/request client {:headers {"Content-Type" "test/test"}}))))))