-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgitter-api.red
319 lines (281 loc) · 6.37 KB
/
gitter-api.red
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
Red [
Title: "Gitter API"
Author: "Boleslav Březovský"
File: %gitter-api.red
Rights: "Copyright © 2016-2019 Boleslav Březovský. All rights reserved."
License: 'BSD
Date: "23-10-2016"
Note: {
This link returns non-UTF8 data: https://api.gitter.im/v1/rooms/572d874bc43b8c6019719620/chatMessages?beforeId=5aede3eeff26986d0835ca5e
}
]
#include %http-tools.red
gitter: context [
token: none
verbose?: false ; force verbose mode
any-map?: func [
"Return TRUE if VALUE is MAP! or OBJECT!"
value
] [
any [map? value object? value]
]
get-id: func [
"Return ID from user or room object/map or pass ID"
data
] [
if path? data [data: get-room-info data] ; TODO: cache room info
if any-map? data [data: data/id]
data
]
map: function [
"Make map with reduce/no-set emulation"
data
] [
value: none
parse data: copy data [
some [
change set value set-word! (reduce ['quote value])
| skip
]
]
make map! reduce data
]
; ----------------------------------------------------------------------------
; gitter api
; ----------------------------------------------------------------------------
response: none
remaining-requests: 100
next-reset: (to integer! now) - 1
; TODO: store verbose output somewhere
info: func [value][if verbose? [print value]]
send: func [
data
"Send GET request to gitter API"
/post "Send POST request"
post-data
/put
put-data
/delete
/verbose
/local
call method link header ts print print* nowi
] [
if verbose [verbose?: true]
method: case [
post ['POST]
put ['PUT]
delete ['DELETE]
true ['GET]
]
info ["Send/method:" method "data:" data "add.data:" any [post-data put-data]]
link: make-url compose [https://api.gitter.im/v1/ (data)]
append header: clear [] [
Accept: "application/json"
]
if any [post put] [
insert header [Content-Type: "application/json"]
post-data: map any [post-data put-data]
]
if all [
remaining-requests < 2
0 < till-reset: next-reset - to integer! now
][
info ["Rate limit reached. Waiting" till-reset "seconds..."]
wait till-reset
]
response: send-request/data/with/auth link method post-data header 'Bearer token
if response/headers/X-RateLimit-Remaining [
remaining-requests: to integer! response/headers/X-RateLimit-Remaining
]
if response/headers/X-RateLimit-Reset [
ts: copy response/headers/X-RateLimit-Reset
next-reset: to integer! (load ts) / 1000
]
switch/default response/code [
200 [response/data]
401 [
; NOTE: we can be here not only because of rate limiting, but also because "unauthorized" access.
if equal? response/data/error "Unauthorized" [
; TODO: This is not best error handling that can be done, but it's at least something
return none
]
; wait until next reset when applicable
nowi: to integer! now
either next-reset >= nowi [
info ["Error 401: Need to wait for" next-reset - nowi "seconds."]
wait next-reset - nowi + 1
info "Wait over."
call: [send]
unless equal? 'get method [
call/1: make path! call/1
append call/1 method
append call data
if equal? 'post method [append call post-data]
if equal? 'put method [append call put-data]
do reduce probe call
]
][
do make error! response/data/error
]
]
][
; TODO: what else can happen?
info ["Return code: " response/code]
response/data
]
response/data
]
; --- groups resource --------------------------------------------------------
get-groups: does [
send %groups
]
group-rooms: func [
group
] [
send [%groups group %rooms]
]
; --- rooms resource
user-rooms: function [
user
] [
user: get-id user
send [%user user %rooms]
]
get-room-info: function [
room "Room name"
] [
res: send/post %rooms [uri: room]
if res/error [cause-error 'user 'message [rejoin ["error getting room '" room "': ^"" copy/part res/error 80 "^""]]]
res
]
join-room: function [
user
room ""
/by-id "Room arg is id instead of name"
] [
user: get-id user
unless by-id [room: select get-room-info room 'id]
send/post [%user user %rooms] [id: room]
]
remove-user: function [
user
room
] [
send/delete [%rooms room %users user]
]
update-topic: function [
room
topic
] [
send/put [%rooms room] [topic: topic]
]
room-tags: function [
room
tags [string! issue! block!]
] [
unless block? tags [tags: reduce tags]
; tags: collect [foreach tag tags [keep rejoin [form tag ", "]]]
; remove/part back back tail tags 2
; send/put [%rooms room] rejoin [{^{"tags":"} tags {"^}}]
send/put [%rooms room] json-map [tags: tags]
]
; TODO: index room
remove-room: function [
room
] [
send/delete [%rooms room]
]
get-users: function [
room
] [
room: get-id room
send [%rooms room %users]
]
; --- messages resource
get-messages: function [
room
/with "skip, beforeId, afterId, aroundId, limit, q (search query)"
values
] [
room: get-id room
data: copy [%rooms room %chatMessages]
if with [append/only data values]
messages: send data
; if there's only one message in room, we get map! instead of block!,
; so we need to make sure that block! is returned in all cases
unless block? messages [messages: reduce [messages]]
; Do date conversion. TODO: avatarUrl conversion (would probably need some checks)
foreach message messages [
message/sent: load message/sent
]
messages
]
get-message: function [
room "Room object or ID"
id "Message ID"
/local message
] [
room: get-id room
message: send [%rooms room %chatMessages id]
message/sent: load message/sent
message
]
send-message: function [
room
text
] [
room: get-id room
send/post [%rooms room %chatMessages] [text: text]
]
update-message: function [
room
text
id
] [
room: get-id room
send/post [%rooms room %chatMessages id] [text: text]
]
; --- user resource
user-info: does [first send %user]
get-user: func [name] [send [%users name]]
; TODO: make part of get-messages?
get-unread: function [
user
room
] [
user: get-id user
room: get-id room
send [%user user %rooms room %unreadItems]
]
mark-as-read: function [
user
room
messages [string! issue! block!]
] [
user: get-id user
room: get-id room
unless block? messages [messages: reduce messages]
unless empty? messages [
send/post [%user user %rooms room %unreadItems] [chat: messages]
]
]
get-orgs: function [
user
] [
user: get-id user
send [%user user %orgs]
]
get-repos: function [
user
] [
user: get-id user
send [%user user %repos]
]
get-channels: function [
user
] [
user: get-id user
; TODO
]
; --- end of Gitter context
]