-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.cljs
29 lines (26 loc) · 961 Bytes
/
client.cljs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
(ns fifql.client
(:require-macros
[cljs.core.async.macros :refer [go]])
(:require
[cljs-http.client :as http]
[clojure.tools.reader.edn :as edn]
[cljs.core.async :refer [<!]]))
(defn ^:export query
"Performs a POST request on the given `url` with the given fif
`sform`. Upon success will call the `callback` function with the
result of the fif stack-machine.
# Examples
(def x 10)
(query \"http://localhost:8080/fifql\"
(sform %= x 2 +)
(fn [data] (println data)))
;; {:input-string \"10 2 +\", :stack (12), :stdout [], :stderr []}
"
[url sform callback]
(go (let [options {:body sform
:headers {"Content-Type" "application/fif"
"Accept" "application/edn"}}
response (<! (http/post url options))]
;; cljs-http automatically converts into EDN
(when-let [response-body (:body response)]
(callback response-body)))))