-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.cljc
179 lines (153 loc) · 5.97 KB
/
helper.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
;;;; Helper functions for Clogram
;;;;
;;;; src/meinside/clogram/helper.cljc
;;;;
;;;; created on 2019.12.09.
(ns meinside.clogram.helper
#?(:cljs
(:require-macros [cljs.core.async.macros :refer [go]]))
(:require #?@(:clj [[clj-http.client :as http]
[clojure.data.json :as json]]
:cljs [[cljs-http.client :as http]
[cljs.core.async :refer [<!]]
[clojure.walk :refer [postwalk]]])
clojure.string))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; constants
(def api-baseurl "https://api.telegram.org/bot")
(def file-baseurl "https://api.telegram.org/file/bot")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; helper functions
;; print verbose log messages
(defn verbose
[bot & args]
(when (:verbose? bot)
(#?(:clj println
:cljs js/console.log)
(str #?(:clj (java.time.LocalDateTime/now)
:cljs (.getTime (js/Date.))))
"| VERBOSE |"
(clojure.string/join "" (map #(if (coll? %) (pr-str %) (str %)) args)))))
;; print log messages
(defn log
[& args]
(#?(:clj println
:cljs js/console.log)
(str #?(:clj (java.time.LocalDateTime/now)
:cljs (.getTime (js/Date.))))
"| LOG |"
(clojure.string/join "" (map #(if (coll? %) (pr-str %) (str %)) args))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; http request helper functions
(defn- is-file?
"Check if given object is a file object."
[obj]
#?(:clj (= (class obj) java.io.File)
:cljs (or
; when it is HTML input
(.-files obj)
; when it is js/Blob
(.-size obj))))
(defn- has-file?
"Check if given params include any file object."
[params]
(not-empty (filter is-file? (vals params))))
(defn- purge-nil-params
"Remove params with nil value"
[params]
(filter (fn [[_ v]] (some? v)) params))
(defn- convert-param
"Convert given param for proper http request."
[param]
(cond
(is-file? param) param
(string? param) param
(keyword? param) (name param)
:else #?(:clj (json/write-str param)
:cljs (.stringify js/JSON (clj->js param)))))
(defn- params-for-multipart
"Convert given params for multipart data."
[params]
(map (fn [[k v]] {:name (str k)
:content (convert-param v)}) params))
(defn- params-for-urlencoded
"Convert given params for urlencoded data."
[params]
(reduce (fn [kv [k v]]
(assoc kv k (convert-param v))) {} params))
(defn- request-multipart
"Send multipart form data."
[bot url params]
(let [timeout-msecs (* 1000 (:timeout-seconds bot))]
(verbose bot "sending multipart form data to: " url ", params: " params)
#?(:clj (http/post url {:multipart (params-for-multipart params)
:socket-timeout timeout-msecs
:connection-timeout timeout-msecs
:accept :json
:throw-exceptions false})
:cljs (http/post url {:multipart-params (params-for-multipart params)
:timeout timeout-msecs}))))
(defn- request-urlencoded
"Send urlencoded form data."
[bot url params]
(let [timeout-msecs (* 1000 (:timeout-seconds bot))]
(verbose bot "sending urlencoded data to: " url ", params: " params)
#?(:clj (http/post url {:form-params (params-for-urlencoded params)
:socket-timeout timeout-msecs
:connection-timeout timeout-msecs
:accept :json
:throw-exceptions false})
:cljs (http/post url {:form-params (params-for-urlencoded params)
:with-credentials? false ; XXX - due to CORS
:timeout timeout-msecs}))))
#?(:clj (defn- key->keyword
"Convert json key to clojure keyword."
[key]
(keyword (clojure.string/replace key "_" "-")))
:cljs (defn keyword->kebabed
"Convert given keyword to kebab-case."
[k]
;; https://stackoverflow.com/questions/36723449/change-all-keywords-in-collection-removing-dots-from-the-namespace-and-replacin
(when (keyword? k)
(->> k
str
(map (some-fn {\_ \-} identity))
rest
clojure.string/join
keyword))))
(defn request
"Send HTTP request with given method name and params.
Return a response synchronously on Clojure,
and return a response channel on ClojureScript.
Keywords in returned responses are in kebab-case."
[bot method params]
(let [f (fn [b m ps]
(let [token (:token b)
url (str api-baseurl token "/" m)
params (purge-nil-params ps)
result (if (has-file? params)
(request-multipart bot url params)
(request-urlencoded bot url params))]
#?(:clj (cond
(= (:status result) 200) (json/read-str (:body result) :key-fn key->keyword)
:else (do
(verbose bot "request error: " result)
(assoc result :ok false)))
:cljs (go (let [res (<! result)]
(cond
(= (:status res) 200) (postwalk (some-fn keyword->kebabed identity) (:body res))
:else (do
(verbose bot "request error: " res)
(assoc res :ok false))))))))]
#?(:clj (try (f bot method params)
(catch Exception e {:ok false
:reason-phrase (.getMessage e)}))
:cljs (f bot method params))))
(defn url-for-filepath
"Generate a URL from a fetched file info.
(https://core.telegram.org/bots/api#getfile)"
[bot filepath]
(str file-baseurl (:token bot) "/" filepath))