2016-05-19 18:31:56 +02:00
|
|
|
(ns status-im.utils.utils
|
2016-03-27 17:59:03 +03:00
|
|
|
(:require-macros
|
|
|
|
[natal-shell.async-storage :refer [get-item set-item]]
|
|
|
|
[natal-shell.alert :refer [alert]]
|
2016-03-28 15:14:57 +03:00
|
|
|
[natal-shell.toast-android :as toast])
|
2016-05-19 18:31:56 +02:00
|
|
|
(:require [status-im.constants :as const]))
|
2016-03-27 17:59:03 +03:00
|
|
|
|
|
|
|
(defn log [obj]
|
|
|
|
(.log js/console obj))
|
|
|
|
|
|
|
|
(defn toast [s]
|
|
|
|
(toast/show s (toast/long)))
|
|
|
|
|
|
|
|
(defn on-error [error]
|
|
|
|
(toast (str "error: " error)))
|
|
|
|
|
|
|
|
(defn http-post
|
|
|
|
([action data on-success]
|
|
|
|
(http-post action data on-success nil))
|
|
|
|
([action data on-success on-error]
|
|
|
|
(-> (.fetch js/window
|
2016-03-28 15:14:57 +03:00
|
|
|
(str const/server-address action)
|
2016-03-27 17:59:03 +03:00
|
|
|
(clj->js {:method "POST"
|
|
|
|
:headers {:accept "application/json"
|
|
|
|
:content-type "application/json"}
|
|
|
|
:body (.stringify js/JSON (clj->js data))}))
|
|
|
|
(.then (fn [response]
|
|
|
|
(log response)
|
|
|
|
(.text response)))
|
|
|
|
(.then (fn [text]
|
|
|
|
(let [json (.parse js/JSON text)
|
|
|
|
obj (js->clj json :keywordize-keys true)]
|
|
|
|
(on-success obj))))
|
|
|
|
(.catch (or on-error
|
|
|
|
(fn [error]
|
|
|
|
(toast (str error))))))))
|
2016-04-14 12:49:50 +03:00
|
|
|
|
|
|
|
(defn http-get
|
|
|
|
([action on-success on-error]
|
|
|
|
(-> (.fetch js/window
|
|
|
|
(str const/server-address action)
|
|
|
|
(clj->js {:method "GET"}))
|
|
|
|
(.then (fn [response]
|
|
|
|
(log response)
|
|
|
|
(.text response)))
|
2016-04-26 12:42:08 +03:00
|
|
|
(.then on-success)
|
2016-04-14 12:49:50 +03:00
|
|
|
(.catch (or on-error
|
|
|
|
(fn [error]
|
|
|
|
(toast (str error))))))))
|