-
Notifications
You must be signed in to change notification settings - Fork 4
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,14 +11,20 @@ | |
(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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To help make it distinguishable that |
||
|
||
(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)) | ||
|
@@ -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)]} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instead of just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch, I think it's actually |
||
(cond | ||
token | ||
{:token-fn (constantly token)} | ||
|
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
baseurl
->base-url
There was a problem hiding this comment.
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.