Added http/get event
Signed-off-by: Julien Eluard <julien.eluard@gmail.com>
This commit is contained in:
parent
382640d72c
commit
c36a0657e6
|
@ -1,4 +1,5 @@
|
||||||
(ns status-im.extensions.core
|
(ns status-im.extensions.core
|
||||||
|
(:refer-clojure :exclude [list])
|
||||||
(:require [clojure.string :as string]
|
(:require [clojure.string :as string]
|
||||||
[pluto.reader :as reader]
|
[pluto.reader :as reader]
|
||||||
[pluto.registry :as registry]
|
[pluto.registry :as registry]
|
||||||
|
@ -13,6 +14,10 @@
|
||||||
[status-im.ui.screens.navigation :as navigation]
|
[status-im.ui.screens.navigation :as navigation]
|
||||||
[status-im.utils.handlers :as handlers]
|
[status-im.utils.handlers :as handlers]
|
||||||
[status-im.utils.fx :as fx]))
|
[status-im.utils.fx :as fx]))
|
||||||
|
; TODO add list, links, radio buttons
|
||||||
|
; wallet/balance
|
||||||
|
; wallet/tokens
|
||||||
|
; http/ post, put, delete
|
||||||
|
|
||||||
(re-frame/reg-fx
|
(re-frame/reg-fx
|
||||||
::alert
|
::alert
|
||||||
|
@ -56,25 +61,39 @@
|
||||||
(fn [{:keys [db]} [_ {:keys [key]}]]
|
(fn [{:keys [db]} [_ {:keys [key]}]]
|
||||||
{:db (update-in db [:extensions-store :collectible] dissoc key)}))
|
{:db (update-in db [:extensions-store :collectible] dissoc key)}))
|
||||||
|
|
||||||
|
(defn- json? [res]
|
||||||
|
(string/starts-with? (get-in res [:headers "content-type"]) "application/json"))
|
||||||
|
|
||||||
(re-frame/reg-event-fx
|
(re-frame/reg-event-fx
|
||||||
:http/get
|
:http/get
|
||||||
(fn [_ [_ {:keys [url on-success on-failure timeout]}]]
|
(fn [_ [_ {:keys [url on-success on-failure timeout]}]]
|
||||||
{:http-get (merge {:url url
|
{:http-raw-get (merge {:url url
|
||||||
:success-event-creator (fn [o] (into on-success (vector o)))}
|
:success-event-creator
|
||||||
(when on-failure
|
(fn [o]
|
||||||
{:failure-event-creator (fn [o] (into on-failure (vector o)))})
|
(let [res (if (json? o) (update o :body #(js->clj (js/JSON.parse %) :keywordize-keys true) o))]
|
||||||
(when timeout
|
(on-success res)))}
|
||||||
{:timeout-ms timeout}))}))
|
(when on-failure
|
||||||
|
{:failure-event-creator on-failure})
|
||||||
|
(when timeout
|
||||||
|
{:timeout-ms timeout}))}))
|
||||||
|
|
||||||
(defn button [{:keys [on-click]} label]
|
(defn button [{:keys [on-click]} label]
|
||||||
[button/secondary-button {:on-press #(re-frame/dispatch on-click)} label])
|
[button/secondary-button {:on-press #(re-frame/dispatch (on-click {}))} label])
|
||||||
|
|
||||||
(defn input [{:keys [on-change placeholder]}]
|
(defn input [{:keys [on-change placeholder]}]
|
||||||
[react/text-input {:on-change-text #(re-frame/dispatch on-change) :placeholder placeholder}])
|
[react/text-input {:on-change-text #(re-frame/dispatch (on-change {})) :placeholder placeholder}])
|
||||||
|
|
||||||
|
(defn touchable-opacity [{:keys [on-press]}]
|
||||||
|
[react/touchable-opacity {:on-press #(re-frame/dispatch (on-press {}))}])
|
||||||
|
|
||||||
|
(defn image [{:keys [uri]}]
|
||||||
|
[react/image {:source {:uri uri}}])
|
||||||
|
|
||||||
(def capacities
|
(def capacities
|
||||||
{:components {'view {:value react/view}
|
{:components {'view {:value react/view}
|
||||||
'text {:value react/text}
|
'text {:value react/text}
|
||||||
|
'touchable-opacity {:value touchable-opacity :properties {:on-press :event}}
|
||||||
|
'image {:value image :properties {:uri :string}}
|
||||||
'input {:value input :properties {:on-change :event :placeholder :string}}
|
'input {:value input :properties {:on-change :event :placeholder :string}}
|
||||||
'button {:value button :properties {:on-click :event}}
|
'button {:value button :properties {:on-click :event}}
|
||||||
'nft-token-viewer {:value transactions/nft-token :properties {:token :string}}
|
'nft-token-viewer {:value transactions/nft-token :properties {:token :string}}
|
||||||
|
@ -82,7 +101,7 @@
|
||||||
'asset-selector {:value transactions/choose-nft-asset-suggestion}
|
'asset-selector {:value transactions/choose-nft-asset-suggestion}
|
||||||
'token-selector {:value transactions/choose-nft-token-suggestion}}
|
'token-selector {:value transactions/choose-nft-token-suggestion}}
|
||||||
:queries {'store/get {:value :store/get :arguments {:key :string}}
|
:queries {'store/get {:value :store/get :arguments {:key :string}}
|
||||||
'get-collectible-token {:value :get-collectible-token :arguments {:token :string :symbol :string}}}
|
'wallet/collectibles {:value :get-collectible-token :arguments {:token :string :symbol :string}}}
|
||||||
:events {'alert
|
:events {'alert
|
||||||
{:permissions [:read]
|
{:permissions [:read]
|
||||||
:value :alert
|
:value :alert
|
||||||
|
|
|
@ -48,6 +48,16 @@
|
||||||
:http-get
|
:http-get
|
||||||
http-get)
|
http-get)
|
||||||
|
|
||||||
|
(defn- http-raw-get [{:keys [url success-event-creator failure-event-creator timeout-ms]}]
|
||||||
|
(let [on-success #(re-frame/dispatch (success-event-creator %))
|
||||||
|
on-error (when failure-event-creator #(re-frame/dispatch (failure-event-creator %)))
|
||||||
|
opts {:timeout-ms timeout-ms}]
|
||||||
|
(http/raw-get url on-success on-error opts)))
|
||||||
|
|
||||||
|
(re-frame/reg-fx
|
||||||
|
:http-raw-get
|
||||||
|
http-raw-get)
|
||||||
|
|
||||||
(re-frame/reg-fx
|
(re-frame/reg-fx
|
||||||
:http-get-n
|
:http-get-n
|
||||||
(fn [calls]
|
(fn [calls]
|
||||||
|
|
|
@ -45,6 +45,32 @@
|
||||||
(fn [error]
|
(fn [error]
|
||||||
(utils/show-popup "Error" (str error))))))))
|
(utils/show-popup "Error" (str error))))))))
|
||||||
|
|
||||||
|
(defn- headers [response]
|
||||||
|
(let [entries (es6-iterator-seq (.entries (.-headers response)))]
|
||||||
|
(reduce #(assoc %1 (string/trim (string/lower-case (first %2))) (string/trim (second %2))) {} entries)))
|
||||||
|
|
||||||
|
(defn raw-get
|
||||||
|
"Performs an HTTP GET request and returns raw results :status :headers :body."
|
||||||
|
([url] (raw-get url nil))
|
||||||
|
([url on-success] (raw-get url on-success nil))
|
||||||
|
([url on-success on-error]
|
||||||
|
(raw-get url on-success on-error nil))
|
||||||
|
([url on-success on-error {:keys [timeout-ms]}]
|
||||||
|
(-> (rn-dependencies/fetch url
|
||||||
|
(clj->js {:method "GET"
|
||||||
|
:headers {"Cache-Control" "no-cache"}
|
||||||
|
:timeout (or timeout-ms http-request-default-timeout-ms)}))
|
||||||
|
(.then (fn [response]
|
||||||
|
(->
|
||||||
|
(.text response)
|
||||||
|
(.then (fn [body]
|
||||||
|
(on-success {:status (.-status response)
|
||||||
|
:headers (headers response)
|
||||||
|
:body body}))))))
|
||||||
|
(.catch (or on-error
|
||||||
|
(fn [error]
|
||||||
|
(utils/show-popup "Error" (str error))))))))
|
||||||
|
|
||||||
(defn get
|
(defn get
|
||||||
"Performs an HTTP GET request"
|
"Performs an HTTP GET request"
|
||||||
([url] (get url nil))
|
([url] (get url nil))
|
||||||
|
|
Loading…
Reference in New Issue