Reorganize API endpoints

This commit is contained in:
Teemu Patja 2017-01-30 21:26:02 +02:00
parent fb136e656d
commit c60db00f6d
No known key found for this signature in database
GPG Key ID: F5B7035E6580FD4C
2 changed files with 70 additions and 66 deletions

View File

@ -36,65 +36,69 @@
:description "commitETH API"}}}} :description "commitETH API"}}}}
(context "/api" [] (context "/api" []
(POST "/user/address" []
:auth-rules authenticated? (context "/bounties" []
:body-params [user-id :- String, address :- String] (GET "/all" []
:summary "Update user address" (ok (bounties-db/list-all-bounties))))
(let [result (users/update-user-address (Integer/parseInt user-id) address)]
(if (= 1 result) (context "/user" []
(ok) (GET "/" []
(internal-server-error)))) :auth-rules authenticated?
(GET "/user" [] :current-user user
:auth-rules authenticated? (ok {:user (users/get-user (:id user))}))
:current-user user (POST "/address" []
(ok {:user (users/get-user (:id user))})) :auth-rules authenticated?
(GET "/user/repositories" [] :body-params [user-id :- String, address :- String]
:auth-rules authenticated? :summary "Update user address"
:current-user user (let [result (users/update-user-address (Integer/parseInt user-id) address)]
(ok {:repositories (->> (github/list-repos (:token user)) (if (= 1 result)
(map #(select-keys % (ok)
[:id :html_url :name :full_name :description])))})) (internal-server-error))))
(GET "/repositories" [] (GET "/repositories" []
:auth-rules authenticated? :auth-rules authenticated?
:current-user user :current-user user
(ok (repositories/get-enabled (:id user)))) (ok {:repositories (->> (github/list-repos (:token user))
(GET "/bounties" [] (map #(select-keys %
(ok (bounties-db/list-all-bounties))) [:id :html_url :name :full_name :description])))}))
(POST "/bounty/:issue{[0-9]{1,9}}/payout" {:keys [params]} (GET "/enabled-repositories" []
:auth-rules authenticated? :auth-rules authenticated?
:current-user user :current-user user
(let [{issue :issue (ok (repositories/get-enabled (:id user))))
payout-hash :payout-hash} params (POST "/bounty/:issue{[0-9]{1,9}}/payout" {:keys [params]}
result (bounties-db/update-payout-hash :auth-rules authenticated?
(Integer/parseInt issue) :current-user user
payout-hash)] (let [{issue :issue
(if (= 1 result) payout-hash :payout-hash} params
(ok) result (bounties-db/update-payout-hash
(internal-server-error)))) (Integer/parseInt issue)
(GET "/user/bounties" [] payout-hash)]
:auth-rules authenticated? (if (= 1 result)
:current-user user (ok)
(ok (map #(conj % (let [balance (:balance %)] (internal-server-error))))
{:balance-eth (eth/hex->eth balance 6) (GET "/bounties" []
:balance-wei (eth/hex->big-integer balance)})) :auth-rules authenticated?
(bounties-db/list-owner-bounties (:id user))))) :current-user user
(POST "/repository/toggle" {:keys [params]} (ok (map #(conj % (let [balance (:balance %)]
:auth-rules authenticated? {:balance-eth (eth/hex->eth balance 6)
:current-user user :balance-wei (eth/hex->big-integer balance)}))
(ok (let [{repo-id :id (bounties-db/list-owner-bounties (:id user)))))
repo :full_name} params (POST "/repository/toggle" {:keys [params]}
{token :token :auth-rules authenticated?
login :login :current-user user
user-id :id} user (ok (let [{repo-id :id
result (or repo :full_name} params
(repositories/create (merge params {:user_id user-id})) {token :token
(repositories/toggle repo-id))] login :login
(if (:enabled result) user-id :id} user
(let [created-hook (github/add-webhook repo token)] result (or
(log/debug "Created webhook:" created-hook) (repositories/create (merge params {:user_id user-id}))
(future (repositories/toggle repo-id))]
(github/create-label repo token) (if (:enabled result)
(repositories/update-hook-id repo-id (:id created-hook)) (let [created-hook (github/add-webhook repo token)]
(bounties/add-bounties-for-existing-issues result))) (log/debug "Created webhook:" created-hook)
(github/remove-webhook repo (:hook_id result) token)) (future
result))))) (github/create-label repo token)
(repositories/update-hook-id repo-id (:id created-hook))
(bounties/add-bounties-for-existing-issues result)))
(github/remove-webhook repo (:hook_id result) token))
result))))))

View File

@ -64,7 +64,7 @@
(fn [{:keys [db]} [_]] (fn [{:keys [db]} [_]]
{:db db {:db db
:http {:method GET :http {:method GET
:url "/api/bounties" :url "/api/bounties/all"
:on-success #(dispatch [:set-bounties %])}})) :on-success #(dispatch [:set-bounties %])}}))
(reg-event-fx (reg-event-fx
@ -72,7 +72,7 @@
(fn [{:keys [db]} [_ issue-id payout-hash]] (fn [{:keys [db]} [_ issue-id payout-hash]]
{:db db {:db db
:http {:method POST :http {:method POST
:url (str/format "/api/bounty/%s/payout" issue-id) :url (str/format "/api/user/bounty/%s/payout" issue-id)
:on-success #(println %) :on-success #(println %)
:params {:payout-hash payout-hash}}})) :params {:payout-hash payout-hash}}}))
@ -133,7 +133,7 @@
(fn [{:keys [db]} [_]] (fn [{:keys [db]} [_]]
{:db db {:db db
:http {:method GET :http {:method GET
:url "/api/repositories" :url "/api/user/enabled-repositories"
:on-success #(dispatch [:set-enabled-repos %])}})) :on-success #(dispatch [:set-enabled-repos %])}}))
(reg-event-fx (reg-event-fx
@ -142,7 +142,7 @@
(println "toggle-repo" repo) (println "toggle-repo" repo)
{:db db {:db db
:http {:method POST :http {:method POST
:url "/api/repository/toggle" :url "/api/user/repository/toggle"
:on-success #(println %) :on-success #(println %)
:params (select-keys repo [:id :login :full_name :name])}})) :params (select-keys repo [:id :login :full_name :name])}}))