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

WIP: b0.3.1 #157

Open
wants to merge 2 commits into
base: master
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
3 changes: 2 additions & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject br.bsb.liberdade.baas.api "0.3.0"
(defproject br.bsb.liberdade.baas.api "0.3.1"
:description "Backend as a Service API"
:url "https://www.liberdade.bsb.br/"
:license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0"
Expand All @@ -15,6 +15,7 @@
[com.github.seancorfield/next.jdbc "1.3.939"]
[org.xerial/sqlite-jdbc "3.46.1.0"]
[middlesphere/clj-compress "0.1.0"]
[clojure-msgpack "1.2.1"]
[clj-http "3.13.0"]]
:min-lein-version "2.9.8"
:main ^:skip-aot br.bsb.liberdade.baas.api
Expand Down
151 changes: 88 additions & 63 deletions src/br/bsb/liberdade/baas/api.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
[compojure.core :refer :all]
[jumblerg.middleware.cors :refer [wrap-cors]]
[selmer.parser :refer :all]
[msgpack.core :as msgpack]
[br.bsb.liberdade.baas.db :as db]
[br.bsb.liberdade.baas.business :as biz]
[br.bsb.liberdade.baas.proxies :as proxies]
Expand All @@ -15,12 +16,23 @@
; #############
; # UTILITIES #
; #############
(defn- boilerplate [body]
{:status (if (-> body (get :error) nil?) 200 400)
:headers {"Content-Type" "text/json"
"Access-Control-Allow-Origin" "*"
"Access-Control-Expose-Headers" "*"}
:body (str (json/write-str body))})
(defn boilerplate-in [req body]
(if (= "application/vnd.msgpack" (-> req :headers (get "accept")))
(msgpack/unpack body)
(json/read-str body)))

(defn- boilerplate-out [req body]
(let [msgpack? (= "application/vnd.msgpack"
(-> req :headers (get "accept")))]
{:status (if (-> body (get :error) nil?) 200 400)
:headers {"Content-Type" (if msgpack?
"application/vnd.msgpack"
"text/json")
"Access-Control-Allow-Origin" "*"
"Access-Control-Expose-Headers" "*"}
:body (if msgpack?
(msgpack/pack body)
(str (json/write-str body)))}))

(defn- url-search-params [raw]
(->> (string/split raw #"&")
Expand All @@ -31,138 +43,142 @@
; # ROUTES #
; ##########
(defn check-health [req]
(boilerplate {"api" "ok"
(boilerplate-out req
{"api" "ok"
"db" (db/check-health)
"scripting" (proxies/check-scripting-engine-health)
"version" "0.3.0"}))
"version" "0.3.1"}))

(defn clients-signup [req]
(let [params (json/read-str (slurp (:body req)))
(let [params (boilerplate-in req (slurp (:body req)))
email (get params "email")
password (get params "password")]
(boilerplate (biz/new-client email password false))))
(boilerplate-out req (biz/new-client email password false))))

(defn clients-login [req]
(let [params (json/read-str (slurp (:body req)))
(let [params (boilerplate-in req (slurp (:body req)))
email (get params "email")
password (get params "password")]
(boilerplate (biz/auth-client email password))))
(boilerplate-out req (biz/auth-client email password))))

(defn create-app [req]
(let [params (json/read-str (slurp (:body req)))
(let [params (boilerplate-in req (slurp (:body req)))
auth-key (get params "auth_key")
app-name (get params "app_name")]
(boilerplate (biz/new-app auth-key app-name))))
(boilerplate-out req (biz/new-app auth-key app-name))))

(defn list-apps [req]
(let [query-string (:query-string req)
search-params (url-search-params query-string)
auth-key (get search-params "auth_key")]
(boilerplate (biz/get-clients-apps auth-key))))
(boilerplate-out req (biz/get-clients-apps auth-key))))

(defn delete-app [req]
(let [params (json/read-str (slurp (:body req)))
(let [params (boilerplate-in req (slurp (:body req)))
client-auth-key (get params "client_auth_key")
app-auth-key (get params "app_auth_key")]
(boilerplate (biz/delete-app client-auth-key app-auth-key))))
(boilerplate-out req (biz/delete-app client-auth-key app-auth-key))))

(defn invite-to-app [req]
(let [params (-> req :body slurp json/read-str)
(let [params (->> req :body slurp (boilerplate-in req))
inviter-auth-key (get params "inviter_auth_key" nil)
app-auth-key (get params "app_auth_key" nil)
invitee-email (get params "invitee_email" nil)
invitee-role (get params "invitee_role" "contributor")]
(boilerplate (biz/invite-to-app-by-email inviter-auth-key
(boilerplate-out req
(biz/invite-to-app-by-email inviter-auth-key
app-auth-key
invitee-email
invitee-role))))

(defn revoke-from-app [req]
(let [params (-> req :body slurp json/read-str)
(let [params (->> req :body slurp (boilerplate-in req))
revoker-auth-key (get params "revoker_auth_key" nil)
app-auth-key (get params "app_auth_key" nil)
revokee-email (get params "revokee_email" nil)]
(boilerplate (biz/revoke-from-app-by-email revoker-auth-key
(boilerplate-out req
(biz/revoke-from-app-by-email revoker-auth-key
app-auth-key
revokee-email))))

(defn update-client-password [req]
(let [params (-> req :body slurp json/read-str)
(let [params (->> req :body slurp (boilerplate-in req))
client-auth-key (get params "auth_key" nil)
old-password (get params "old_password" nil)
new-password (get params "new_password" nil)]
(boilerplate (biz/change-client-password client-auth-key
(boilerplate-out req
(biz/change-client-password client-auth-key
old-password
new-password))))
(defn delete-client [req]
(let [params (-> req :body slurp json/read-str)
(let [params (->> req :body slurp (boilerplate-in req))
auth-key (get params "auth_key" nil)
password (get params "password" nil)]
(boilerplate (biz/delete-client auth-key password))))
(boilerplate-out req (biz/delete-client auth-key password))))

(defn users-signup [req]
(let [params (-> req :body slurp json/read-str)
(let [params (->> req :body slurp (boilerplate-in req))
app-auth-key (get params "app_auth_key" nil)
email (get params "email" nil)
password (get params "password" nil)]
(boilerplate (biz/new-user app-auth-key email password))))
(boilerplate-out req (biz/new-user app-auth-key email password))))

(defn users-login [req]
(let [params (-> req :body slurp json/read-str)
(let [params (->> req :body slurp (boilerplate-in req))
app-auth-key (get params "app_auth_key" nil)
email (get params "email" nil)
password (get params "password" nil)]
(boilerplate (biz/auth-user app-auth-key email password))))
(boilerplate-out req (biz/auth-user app-auth-key email password))))

(defn delete-user [req]
(let [params (-> req :body slurp json/read-str)
(let [params (->> req :body slurp (boilerplate-in req))
user-auth-key (get params "user_auth_key" nil)
password (get params "password" nil)]
(boilerplate (biz/delete-user user-auth-key password))))
(boilerplate-out req (biz/delete-user user-auth-key password))))

(defn update-user-password [req]
(let [params (-> req :body slurp json/read-str)
(let [params (->> req :body slurp (boilerplate-in req))
user-auth-key (get params "user_auth_key" nil)
old-password (get params "old_password" nil)
new-password (get params "new_password" nil)]
(boilerplate (biz/update-user-password user-auth-key old-password new-password))))
(boilerplate-out req
(biz/update-user-password user-auth-key
old-password
new-password))))

(defn list-app-users [req]
(let [params (-> req :query-string url-search-params)
client-auth-key (get params "client_auth_key" nil)
app-auth-key (get params "app_auth_key" nil)]
(boilerplate (biz/list-app-users client-auth-key app-auth-key))))
(boilerplate-out req (biz/list-app-users client-auth-key app-auth-key))))

(defn upload-user-file [req]
(let [user-auth-key (-> req :headers (get "x-user-auth-key"))
filename (-> req :headers (get "x-filename"))
contents (-> req :body slurp)]
(boilerplate (biz/upload-user-file user-auth-key filename contents))))
(boilerplate-out req (biz/upload-user-file user-auth-key filename contents))))

(defn download-user-file [req]
(let [user-auth-key (-> req :headers (get "x-user-auth-key"))
filename (-> req :headers (get "x-filename"))]
(biz/download-user-file user-auth-key filename)))

(defn list-user-files [req]
(-> req
:headers
(get "x-user-auth-key")
biz/list-user-files
boilerplate))
(let [auth-key (-> req :headers (get "x-user-auth-key"))]
(boilerplate-out req (biz/list-user-files auth-key))))

(defn delete-user-file [req]
(let [user-auth-key (-> req :headers (get "x-user-auth-key"))
filename (-> req :headers (get "x-filename"))]
(boilerplate (biz/delete-user-file user-auth-key filename))))
(boilerplate-out req (biz/delete-user-file user-auth-key filename))))

(defn upload-app-file [req]
(let [client-auth-key (-> req :headers (get "x-client-auth-key"))
app-auth-key (-> req :headers (get "x-app-auth-key"))
filename (-> req :headers (get "x-filename"))
contents (-> req :body slurp)]
(-> (biz/upload-app-file client-auth-key app-auth-key filename contents)
boilerplate)))
(->> (biz/upload-app-file client-auth-key app-auth-key filename contents)
(boilerplate-out req))))

(defn download-app-file [req]
(let [client-auth-key (-> req :headers (get "x-client-auth-key"))
Expand All @@ -174,48 +190,54 @@
(let [client-auth-key (-> req :headers (get "x-client-auth-key"))
app-auth-key (-> req :headers (get "x-app-auth-key"))
filename (-> req :headers (get "x-filename"))]
(boilerplate (biz/delete-app-file client-auth-key app-auth-key filename))))
(boilerplate-out req
(biz/delete-app-file client-auth-key
app-auth-key
filename))))

(defn list-app-files [req]
(let [params (-> req :query-string url-search-params)
client-auth-key (get params "client_auth_key")
app-auth-key (get params "app_auth_key")]
(boilerplate (biz/list-app-files client-auth-key app-auth-key))))
(boilerplate-out req (biz/list-app-files client-auth-key app-auth-key))))

(defn list-app-managers [req]
(let [params (-> req :query-string url-search-params)
client-auth-key (get params "client_auth_key" nil)
app-auth-key (get params "app_auth_key" nil)]
(boilerplate (biz/list-app-managers client-auth-key app-auth-key))))
(boilerplate-out req
(biz/list-app-managers client-auth-key app-auth-key))))

(defn revoke-app-manager [req]
(let [params (-> req :body slurp json/read-str)
(let [params (->> req :body slurp (boilerplate-in req))
client-auth-key (get params "client_auth_key" nil)
app-auth-key (get params "app_auth_key" nil)
email-to-revoke (get params "email_to_revoke" nil)]
(boilerplate (biz/revoke-admin-access client-auth-key
(boilerplate-out req
(biz/revoke-admin-access client-auth-key
app-auth-key
email-to-revoke))))

(defn upload-action [req]
(let [params (-> req :body slurp json/read-str)
(let [params (->> req :body slurp (boilerplate-in req))
client-auth-key (get params "client_auth_key" nil)
app-auth-key (get params "app_auth_key" nil)
action-name (get params "action_name" nil)
action-script (get params "action_script" nil)]
(boilerplate (biz/upsert-action client-auth-key
(boilerplate-out req (biz/upsert-action client-auth-key
app-auth-key
action-name
action-script))))

(defn update-action [req]
(let [params (-> req :body slurp json/read-str)
(let [params (->> req :body slurp (boilerplate-in req))
client-auth-key (get params "client_auth_key" nil)
app-auth-key (get params "app_auth_key" nil)
old-action-name (get params "old_action_name" nil)
new-action-name (get params "new_action_name" nil)
action-script (get params "action_script" nil)]
(boilerplate (biz/update-action client-auth-key
(boilerplate-out req
(biz/update-action client-auth-key
app-auth-key
old-action-name
new-action-name
Expand All @@ -225,7 +247,8 @@
(let [client-auth-key (-> req :headers (get "x-client-auth-key"))
app-auth-key (-> req :headers (get "x-app-auth-key"))
compressed-actions (-> req :body untar/slurp-bytes)]
(boilerplate (biz/upload-actions client-auth-key
(boilerplate-out req
(biz/upload-actions client-auth-key
app-auth-key
compressed-actions))))

Expand All @@ -240,24 +263,26 @@
(let [params (-> req :query-string url-search-params)
client-auth-key (get params "client_auth_key" nil)
app-auth-key (get params "app_auth_key" nil)]
(boilerplate (biz/list-actions client-auth-key app-auth-key))))
(boilerplate-out req (biz/list-actions client-auth-key app-auth-key))))

(defn delete-action [req]
(let [params (-> req :body slurp json/read-str)
(let [params (->> req :body slurp (boilerplate-in req))
client-auth-key (get params "client_auth_key" nil)
app-auth-key (get params "app_auth_key" nil)
action-name (get params "action_name" nil)]
(boilerplate (biz/delete-action client-auth-key
(boilerplate-out req
(biz/delete-action client-auth-key
app-auth-key
action-name))))

(defn run-action [req]
(let [params (-> req :body slurp json/read-str)
(let [params (->> req :body slurp (boilerplate-in req))
user-auth-key (get params "user_auth_key" nil)
app-auth-key (get params "app_auth_key" nil)
action-name (get params "action_name" nil)
action-param (get params "action_param" nil)]
(boilerplate (proxies/run-action user-auth-key
(boilerplate-out req
(proxies/run-action user-auth-key
app-auth-key
action-name
action-param))))
Expand All @@ -267,7 +292,7 @@
:headers
(get "x-client-auth-key")
f
boilerplate))
boilerplate-out))

(defn list-all-clients [req]
(list-all-things req biz/list-all-clients))
Expand All @@ -282,16 +307,16 @@
(list-all-things req biz/list-all-admins))

(defn promote-to-admin [req]
(let [params (-> req :body slurp json/read-str)
(let [params (->> req :body slurp (boilerplate-in req))
auth-key (get params "auth_key" nil)
email (get params "email" nil)]
(boilerplate (biz/promote-to-admin auth-key email))))
(boilerplate-out req (biz/promote-to-admin auth-key email))))

(defn demote-admin [req]
(let [params (-> req :body slurp json/read-str)
(let [params (->> req :body slurp (boilerplate-in req))
auth-key (get params "auth_key" nil)
email (get params "email" nil)]
(boilerplate (biz/demote-admin auth-key email))))
(boilerplate-out req (biz/demote-admin auth-key email))))

(defn check-admin [req]
(list-all-things req biz/check-admin))
Expand Down