From 09c5e4e3c0159d697ed4fdb09bb084118c5d5437 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Tue, 15 Mar 2016 20:53:17 +0200 Subject: [PATCH] Add initial reposotory abstraction with auth methods. --- src/uxbox/repo.cljs | 45 ++++--------------------- src/uxbox/repo/auth.cljs | 39 ++++++++++++++++++++++ src/uxbox/repo/core.cljs | 72 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 118 insertions(+), 38 deletions(-) create mode 100644 src/uxbox/repo/auth.cljs create mode 100644 src/uxbox/repo/core.cljs diff --git a/src/uxbox/repo.cljs b/src/uxbox/repo.cljs index 9fe3d8ab2..2c9327203 100644 --- a/src/uxbox/repo.cljs +++ b/src/uxbox/repo.cljs @@ -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 -;; Copyright (c) 2015-2016 Juan de la Cruz +;; Copyright (c) 2016 Andrey Antukh (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))) diff --git a/src/uxbox/repo/auth.cljs b/src/uxbox/repo/auth.cljs new file mode 100644 index 000000000..199f79e7c --- /dev/null +++ b/src/uxbox/repo/auth.cljs @@ -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 + +(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))) diff --git a/src/uxbox/repo/core.cljs b/src/uxbox/repo/core.cljs new file mode 100644 index 000000000..1cb7a781c --- /dev/null +++ b/src/uxbox/repo/core.cljs @@ -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 + +(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)) +