mirror of
https://github.com/penpot/penpot.git
synced 2025-04-14 07:51:35 -05:00
Add initial reposotory abstraction with auth methods.
This commit is contained in:
parent
e9a51c14b8
commit
09c5e4e3c0
3 changed files with 118 additions and 38 deletions
|
@ -2,51 +2,20 @@
|
|||
;; License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
;;
|
||||
;; Copyright (c) 2015-2016 Andrey Antukh <niwi@niwi.nz>
|
||||
;; Copyright (c) 2015-2016 Juan de la Cruz <delacruzgarciajuan@gmail.com>
|
||||
;; Copyright (c) 2016 Andrey Antukh <niwi@niwi.nz>
|
||||
|
||||
(ns uxbox.repo
|
||||
"A main interface for access to remote resources."
|
||||
(:refer-clojure :exclude [do])
|
||||
(:require [postal.client :as ps]
|
||||
(:require [uxbox.repo.core :as urc]
|
||||
[uxbox.repo.auth]
|
||||
[uxbox.repo.projects]
|
||||
[beicon.core :as rx]))
|
||||
|
||||
(def ^:private ^:const +client+
|
||||
"https://test.uxbox.io")
|
||||
|
||||
(defn novelty
|
||||
[type data]
|
||||
(rx/from-promise
|
||||
(ps/novelty +client+ type data)))
|
||||
|
||||
(defn query
|
||||
([type]
|
||||
(rx/from-promise
|
||||
(ps/query +client+ type)))
|
||||
([type data]
|
||||
(rx/from-promise
|
||||
(ps/query +client+ type data))))
|
||||
|
||||
(defmulti -do
|
||||
(fn [type data] type))
|
||||
|
||||
(defn do
|
||||
"Perform a side effectfull action accesing
|
||||
remote remote resources."
|
||||
remote resources."
|
||||
([type]
|
||||
(-do type nil))
|
||||
(urc/-do type nil))
|
||||
([type data]
|
||||
(-do type data)))
|
||||
|
||||
;; (defmethod do :login
|
||||
;; [type data]
|
||||
;; (rx/from-promise
|
||||
;; (ps/novelty :login data)))
|
||||
|
||||
(defmethod -do :login
|
||||
[type data]
|
||||
(->> (rx/of {:fullname "Cirilla Fiona"
|
||||
:photo "/images/favicon.png"
|
||||
:username "cirilla"
|
||||
:email "cirilla@uxbox.io"})
|
||||
(rx/delay 100)))
|
||||
(urc/-do type data)))
|
||||
|
|
39
src/uxbox/repo/auth.cljs
Normal file
39
src/uxbox/repo/auth.cljs
Normal file
|
@ -0,0 +1,39 @@
|
|||
;; This Source Code Form is subject to the terms of the Mozilla Public
|
||||
;; License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
;;
|
||||
;; Copyright (c) 2016 Andrey Antukh <niwi@niwi.nz>
|
||||
|
||||
(ns uxbox.repo.auth
|
||||
"A main interface for access to remote resources."
|
||||
(:refer-clojure :exclude [do])
|
||||
(:require [httpurr.client.xhr :as http]
|
||||
[httpurr.status :as http.status]
|
||||
[promesa.core :as p :include-macros true]
|
||||
[beicon.core :as rx]
|
||||
[uxbox.repo.core :as urc]
|
||||
[uxbox.state :as ust]))
|
||||
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
;; Login
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
|
||||
(defn- request-token
|
||||
[params]
|
||||
(urc/req! {:url (str urc/+uri+ "/auth/token")
|
||||
:method :post
|
||||
:auth false
|
||||
:body params}))
|
||||
|
||||
(defn- request-profile
|
||||
[]
|
||||
(p/resolved {:fullname "Cirilla Fiona"
|
||||
:photo "/images/favicon.png"
|
||||
:username "cirilla"
|
||||
:email "cirilla@uxbox.io"}))
|
||||
|
||||
(defmethod urc/-do :login
|
||||
[type data]
|
||||
(p/alet [authdata (p/await (request-token data))
|
||||
profile (p/await (request-profile))]
|
||||
(merge profile authdata)))
|
72
src/uxbox/repo/core.cljs
Normal file
72
src/uxbox/repo/core.cljs
Normal file
|
@ -0,0 +1,72 @@
|
|||
;; This Source Code Form is subject to the terms of the Mozilla Public
|
||||
;; License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
;;
|
||||
;; Copyright (c) 2016 Andrey Antukh <niwi@niwi.nz>
|
||||
|
||||
(ns uxbox.repo.core
|
||||
"A main interface for access to remote resources."
|
||||
(:refer-clojure :exclude [do])
|
||||
(:require [httpurr.client.xhr :as http]
|
||||
[httpurr.status :as http.status]
|
||||
[hodgepodge.core :refer (local-storage)]
|
||||
[promesa.core :as p :include-macros true]
|
||||
[beicon.core :as rx]
|
||||
[uxbox.transit :as t]
|
||||
[uxbox.state :as ust]))
|
||||
|
||||
(def ^:private ^:const +uri+
|
||||
"http://127.0.0.1:5050/api")
|
||||
|
||||
(def ^:private +storage+
|
||||
local-storage)
|
||||
|
||||
(defn- conditional-decode
|
||||
[{:keys [body headers] :as response}]
|
||||
(if (= (get headers "content-type") "application/transit+json")
|
||||
(assoc response :body (t/decode body))
|
||||
response))
|
||||
|
||||
(defn- handle-http-status
|
||||
[response]
|
||||
(if (http.status/success? response)
|
||||
(p/resolved response)
|
||||
(p/rejected response)))
|
||||
|
||||
(def ^:private ^:const +headers+
|
||||
{"content-type" "application/transit+json"})
|
||||
|
||||
(defn auth-headers
|
||||
[]
|
||||
(when-let [auth (:auth @ust/state)]
|
||||
{"authorization" (str "Token " (:token auth "no-token"))}))
|
||||
|
||||
(defn- send!
|
||||
[{:keys [body headers auth method] :or {auth true} :as request}]
|
||||
(let [headers (merge {}
|
||||
(when (not= method :get) +headers+)
|
||||
headers
|
||||
(when auth (auth-headers)))
|
||||
request (merge (assoc request :headers headers)
|
||||
(when body {:body (t/encode body)}))]
|
||||
(-> (http/send! request)
|
||||
(p/catch (fn [err]
|
||||
(println "[error]:" err)
|
||||
(throw err)))
|
||||
(p/then conditional-decode)
|
||||
(p/then handle-http-status))))
|
||||
|
||||
(defn- req!
|
||||
[request]
|
||||
(let [on-success #(p/resolved (:body %))
|
||||
on-error #(if (map? %)
|
||||
(p/rejected (:body %))
|
||||
(p/rejected %))]
|
||||
|
||||
(-> (send! request)
|
||||
(p/then on-success)
|
||||
(p/catch on-error))))
|
||||
|
||||
(defmulti -do
|
||||
(fn [type data] type))
|
||||
|
Loading…
Add table
Reference in a new issue