mirror of
https://github.com/penpot/penpot.git
synced 2025-01-10 08:50:57 -05:00
✨ Minor improvement of http client api.
Make it more compatible to be executed in worker.
This commit is contained in:
parent
22975f4f7d
commit
6a6ead0d63
3 changed files with 72 additions and 63 deletions
|
@ -2,64 +2,17 @@
|
||||||
;; License, v. 2.0. If a copy of the MPL was not distributed with this
|
;; 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/.
|
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
;;
|
;;
|
||||||
;; Copyright (c) 2019 Andrey Antukh <niwi@niwi.nz>
|
;; This Source Code Form is "Incompatible With Secondary Licenses", as
|
||||||
|
;; defined by the Mozilla Public License, v. 2.0.
|
||||||
|
;;
|
||||||
|
;; Copyright (c) 2020 UXBOX Labs SL
|
||||||
|
|
||||||
(ns uxbox.main.repo
|
(ns uxbox.main.repo
|
||||||
(:require
|
(:require
|
||||||
[beicon.core :as rx]
|
[beicon.core :as rx]
|
||||||
[cuerdas.core :as str]
|
[cuerdas.core :as str]
|
||||||
[uxbox.config :refer [url]]
|
[uxbox.config :refer [url]]
|
||||||
[uxbox.util.http :as http]
|
[uxbox.util.http-api :as http]))
|
||||||
[uxbox.util.storage :refer [storage]]
|
|
||||||
[uxbox.util.transit :as t])
|
|
||||||
(:import [goog.Uri QueryData]))
|
|
||||||
|
|
||||||
;; --- Low Level API
|
|
||||||
|
|
||||||
(defn- conditional-decode
|
|
||||||
[{:keys [body headers] :as response}]
|
|
||||||
(let [contentype (get headers "content-type")]
|
|
||||||
(if (str/starts-with? contentype "application/transit+json")
|
|
||||||
(assoc response :body (t/decode body))
|
|
||||||
response)))
|
|
||||||
|
|
||||||
(defn- handle-http-status
|
|
||||||
[{:keys [body status] :as response}]
|
|
||||||
(if (http/success? response)
|
|
||||||
(rx/of {:status status :payload body})
|
|
||||||
(rx/throw {:status status :payload body})))
|
|
||||||
|
|
||||||
(def ^:private +headers+
|
|
||||||
{"content-type" "application/transit+json"})
|
|
||||||
|
|
||||||
(defn- encode-query
|
|
||||||
[params]
|
|
||||||
(let [data (QueryData.)]
|
|
||||||
(.extend data (clj->js params))
|
|
||||||
(.toString data)))
|
|
||||||
|
|
||||||
(defn impl-send
|
|
||||||
[{:keys [body headers auth method query url response-type]
|
|
||||||
:or {auth true response-type :text}}]
|
|
||||||
(let [headers (merge {"Accept" "application/transit+json,*/*"}
|
|
||||||
(when (map? body) +headers+)
|
|
||||||
headers)
|
|
||||||
request {:method method
|
|
||||||
:url url
|
|
||||||
:headers headers
|
|
||||||
:query query
|
|
||||||
:body (if (map? body) (t/encode body) body)}
|
|
||||||
options {:response-type response-type
|
|
||||||
:credentials? true}]
|
|
||||||
(http/send! request options)))
|
|
||||||
|
|
||||||
(defn send!
|
|
||||||
[request]
|
|
||||||
(->> (impl-send request)
|
|
||||||
(rx/map conditional-decode)
|
|
||||||
(rx/mapcat handle-http-status)))
|
|
||||||
|
|
||||||
;; --- High Level API
|
|
||||||
|
|
||||||
(defn- handle-response
|
(defn- handle-response
|
||||||
[response]
|
[response]
|
||||||
|
@ -77,15 +30,13 @@
|
||||||
(defn send-query!
|
(defn send-query!
|
||||||
[id params]
|
[id params]
|
||||||
(let [url (str url "/api/w/query/" (name id))]
|
(let [url (str url "/api/w/query/" (name id))]
|
||||||
(->> (impl-send {:method :get :url url :query params})
|
(->> (http/send! {:method :get :url url :query params})
|
||||||
(rx/map conditional-decode)
|
|
||||||
(rx/mapcat handle-response))))
|
(rx/mapcat handle-response))))
|
||||||
|
|
||||||
(defn send-mutation!
|
(defn send-mutation!
|
||||||
[id params]
|
[id params]
|
||||||
(let [url (str url "/api/w/mutation/" (name id))]
|
(let [url (str url "/api/w/mutation/" (name id))]
|
||||||
(->> (impl-send {:method :post :url url :body params})
|
(->> (http/send! {:method :post :url url :body params})
|
||||||
(rx/map conditional-decode)
|
|
||||||
(rx/mapcat handle-response))))
|
(rx/mapcat handle-response))))
|
||||||
|
|
||||||
(defn- dispatch
|
(defn- dispatch
|
||||||
|
@ -138,15 +89,13 @@
|
||||||
(defmethod mutation :login
|
(defmethod mutation :login
|
||||||
[id params]
|
[id params]
|
||||||
(let [url (str url "/api/login")]
|
(let [url (str url "/api/login")]
|
||||||
(->> (impl-send {:method :post :url url :body params})
|
(->> (http/send! {:method :post :url url :body params})
|
||||||
(rx/map conditional-decode)
|
|
||||||
(rx/mapcat handle-response))))
|
(rx/mapcat handle-response))))
|
||||||
|
|
||||||
(defmethod mutation :logout
|
(defmethod mutation :logout
|
||||||
[id params]
|
[id params]
|
||||||
(let [url (str url "/api/logout")]
|
(let [url (str url "/api/logout")]
|
||||||
(->> (impl-send {:method :post :url url :body params :auth false})
|
(->> (http/send! {:method :post :url url :body params :auth false})
|
||||||
(rx/map conditional-decode)
|
|
||||||
(rx/mapcat handle-response))))
|
(rx/mapcat handle-response))))
|
||||||
|
|
||||||
(def client-error? http/client-error?)
|
(def client-error? http/client-error?)
|
||||||
|
|
|
@ -2,15 +2,20 @@
|
||||||
;; License, v. 2.0. If a copy of the MPL was not distributed with this
|
;; 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/.
|
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
;;
|
;;
|
||||||
;; Copyright (c) 2016 Andrey Antukh <niwi@niwi.nz>
|
;; This Source Code Form is "Incompatible With Secondary Licenses", as
|
||||||
|
;; defined by the Mozilla Public License, v. 2.0.
|
||||||
|
;;
|
||||||
|
;; Copyright (c) 2020 UXBOX Labs SL
|
||||||
|
|
||||||
(ns uxbox.util.http
|
(ns uxbox.util.http
|
||||||
"A http client with rx streams interface."
|
"A http client with rx streams interface."
|
||||||
(:refer-clojure :exclude [get])
|
(:refer-clojure :exclude [get])
|
||||||
(:require
|
(:require
|
||||||
|
[cljs.core :as c]
|
||||||
[beicon.core :as rx]
|
[beicon.core :as rx]
|
||||||
|
[clojure.string :as str]
|
||||||
[goog.events :as events]
|
[goog.events :as events]
|
||||||
[clojure.string :as str])
|
[uxbox.util.transit :as t])
|
||||||
(:import
|
(:import
|
||||||
[goog.net ErrorCode EventType]
|
[goog.net ErrorCode EventType]
|
||||||
[goog.net.XhrIo ResponseType]
|
[goog.net.XhrIo ResponseType]
|
||||||
|
@ -78,7 +83,6 @@
|
||||||
(letfn [(on-complete [event]
|
(letfn [(on-complete [event]
|
||||||
(let [type (translate-error-code (.getLastErrorCode xhr))
|
(let [type (translate-error-code (.getLastErrorCode xhr))
|
||||||
status (.getStatus xhr)]
|
status (.getStatus xhr)]
|
||||||
;; (prn "on-complete" type method url)
|
|
||||||
(if (pos? status)
|
(if (pos? status)
|
||||||
(sink (rx/end
|
(sink (rx/end
|
||||||
{:status status
|
{:status status
|
||||||
|
|
56
frontend/src/uxbox/util/http_api.cljs
Normal file
56
frontend/src/uxbox/util/http_api.cljs
Normal file
|
@ -0,0 +1,56 @@
|
||||||
|
;; 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/.
|
||||||
|
;;
|
||||||
|
;; This Source Code Form is "Incompatible With Secondary Licenses", as
|
||||||
|
;; defined by the Mozilla Public License, v. 2.0.
|
||||||
|
;;
|
||||||
|
;; Copyright (c) 2020 UXBOX Labs SL
|
||||||
|
|
||||||
|
(ns uxbox.util.http-api
|
||||||
|
"A specific customizations of http client for api access."
|
||||||
|
(:require
|
||||||
|
[beicon.core :as rx]
|
||||||
|
[cuerdas.core :as str]
|
||||||
|
[uxbox.util.http :as http]
|
||||||
|
[uxbox.util.transit :as t]))
|
||||||
|
|
||||||
|
(defn- conditional-decode
|
||||||
|
[{:keys [body headers] :as response}]
|
||||||
|
(let [contentype (get headers "content-type")]
|
||||||
|
(if (str/starts-with? contentype "application/transit+json")
|
||||||
|
(assoc response :body (t/decode body))
|
||||||
|
response)))
|
||||||
|
|
||||||
|
(defn- handle-http-status
|
||||||
|
[{:keys [body status] :as response}]
|
||||||
|
(if (http/success? response)
|
||||||
|
(rx/of {:status status :payload body})
|
||||||
|
(rx/throw {:status status :payload body})))
|
||||||
|
|
||||||
|
(def ^:private default-headers
|
||||||
|
{"content-type" "application/transit+json"})
|
||||||
|
|
||||||
|
(defn- impl-send
|
||||||
|
[{:keys [body headers auth method query url response-type]
|
||||||
|
:or {auth true response-type :text}}]
|
||||||
|
(let [headers (merge {"Accept" "application/transit+json,*/*"}
|
||||||
|
default-headers
|
||||||
|
headers)
|
||||||
|
request {:method method
|
||||||
|
:url url
|
||||||
|
:headers headers
|
||||||
|
:query query
|
||||||
|
:body (when (not= :get method) (t/encode body))}
|
||||||
|
options {:response-type response-type
|
||||||
|
:credentials? auth}]
|
||||||
|
(http/send! request options)))
|
||||||
|
|
||||||
|
(defn send!
|
||||||
|
[request]
|
||||||
|
(->> (impl-send request)
|
||||||
|
(rx/map conditional-decode)))
|
||||||
|
|
||||||
|
(def success? http/success?)
|
||||||
|
(def client-error? http/client-error?)
|
||||||
|
(def server-error? http/server-error?)
|
Loading…
Reference in a new issue