/
gi.cljc
154 lines (140 loc) · 4.81 KB
/
gi.cljc
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
(ns nsfw.gi
(:require
[nsfw.util :as nu]
#? (:cljs
[cljs-http.client :as http]
:clj
[clj-http.client :as http])
#? (:clj
[clojure.core.async :as async
:refer [go <! >! chan close! sliding-buffer put! take! alts! timeout pipe mult tap]]
:cljs
[cljs.core.async :as async
:refer [<! >! chan close! sliding-buffer put! take! alts! timeout pipe mult tap]]))
#? (:cljs (:require-macros [cljs.core.async.macros :refer [go go-loop]])))
;; single endpoint with auth
(defn tunnel [{:keys [uri
include-response?
timeout
transit-opts]
:or {timeout 20000}
:as req}
payload]
(when-not uri
#? (:clj
(throw (Exception. "tunnel: missing {:uri ...}"))
:cljs
(throw (js/Error "tunnel: missing {:uri ...}"))))
(go
(let [res #?(:clj
(http/post
uri
{:content-type :transit+json
:form-params payload
:as :transit+json})
:cljs
(<! (http/post
uri
{:transit-params payload
:headers {"Content-Type" "application/transit+json"}
:timeout timeout
:transit-opts transit-opts})))
{:keys [success
body
headers
error-code
error-text
status] :as resp}
res]
(cond
(= :timeout error-code)
[nil
{:timeout? true
:error error-text}
req]
(not= 200 status)
[nil
{:error "Tunnel response status not 200"
:response res}
req]
(= -1
(.indexOf
(or (get headers "Content-Type")
(get headers "content-type"))
"application/transit+json"))
[nil
{:error (str "tunnel response content-type not application/transit+json: " (pr-str resp))}
req]
#? (:cljs (not success))
#? (:cljs [nil
{:error "Couldn't contact server"}
req])
:else [body nil req]))))
(defn <send-commands [tun-opts commands]
(go
(<! (tunnel tun-opts commands))))
(defn <send-command [tun-opts command]
(go
(let [response (<! (tunnel tun-opts [command]))]
(if (second response)
response
(ffirst response)))))
(defn handle-commands [spec commands]
(->> commands
(map (fn [command]
(let [handler (get spec (first command))]
(if handler
(try
(apply handler (rest command))
#? (:clj
(catch clojure.lang.ArityException e
(println "GI2 Exception Caught")
(prn e)
[nil {:error (str
"Wrong number of arguments for "
(first command))}]))
#? (:clj
(catch Exception e
(println "GI2 Exception Caught")
(prn e)
[nil {:error (str
"Exception when handling "
(first command))
:exception (pr-str e)}])
:cljs
(catch js/Error e
[nil {:error (str
"Exception when handling "
(first command))
:exception (pr-str e)}])))
[nil {:error (str "No handler found for " (first command))}]))))))
(defn handle-request [spec {:keys [body] :as request}]
(let [results (handle-commands spec body)
body (->> results
(map (fn [res]
(if (map? res)
(:body res)
res)))
vec)
session (->> results
(map (fn [res]
(when (map? res)
(:session res))))
(remove nil?)
(reduce merge))]
(merge
{:body body}
(when-not (empty? session)
{:session
(merge
(:session request)
session)}))))
#_(go
(nu/pp (<! (<send-commands
{:uri "http://localhost:5000/api/fash/gi2"}
[[:test "foo"]]))))
#_(go
(nu/spy "GI2 command"
(<! (<send-command
{:uri "http://localhost:5000/api/fash/gi2"}
[:test2 "foo" "bar"]))))