This repository has been archived by the owner on Oct 24, 2020. It is now read-only.
/
__init__.py
493 lines (375 loc) · 10.5 KB
/
__init__.py
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
import asyncio
from urllib.parse import urlencode
import aiohttp
import async_timeout
import requests
import slacker
from .compat import PY_350, ensure_future
__version__ = '0.0.11'
Error = slacker.Error
Response = slacker.Response
class BaseAPI(slacker.BaseAPI):
def __init__(
self,
token=None,
timeout=slacker.DEFAULT_TIMEOUT,
*,
loop=None
):
if loop is None:
loop = asyncio.get_event_loop()
self.loop = loop
self.session = aiohttp.ClientSession(
connector=aiohttp.TCPConnector(
use_dns_cache=False,
loop=self.loop,
),
)
self.methods = {
requests.get: 'GET',
requests.post: 'POST',
}
self.futs = set()
super().__init__(token=token, timeout=timeout)
@asyncio.coroutine
def __request(self, method, api, **kwargs):
method = self.methods[method]
kwargs.setdefault('params', {})
kwargs.setdefault('timeout', None)
if self.token:
kwargs['params']['token'] = self.token
kwargs['params'] = urlencode(kwargs['params'], doseq=True)
if method == 'POST':
files = kwargs.pop('files', None)
if files is not None:
data = kwargs.pop('data', {})
_data = aiohttp.FormData()
for k, v in files.items():
_data.add_field(k, open(v.name, 'rb'))
for k, v in data.items():
if v is not None:
_data.add_field(k, str(v))
kwargs['data'] = _data
_url = slacker.API_BASE_URL.format(api=api)
_request = self.session.request(method, _url, **kwargs)
_response = None
try:
with async_timeout.timeout(self.timeout, loop=self.loop):
_response = yield from _request
_response.raise_for_status()
text = yield from _response.text()
finally:
if _response is not None:
yield from _response.release()
response = Response(text)
if not response.successful:
raise Error(response.error)
return response
def _request(self, method, api, **kwargs):
coro = self.__request(method, api, **kwargs)
fut = ensure_future(coro, loop=self.loop)
self.futs.add(fut)
fut.add_done_callback(self.futs.remove)
return fut
@asyncio.coroutine
def close(self):
if self.futs:
yield from asyncio.gather(
*self.futs,
loop=self.loop,
return_exceptions=True
)
yield from self.session.close()
class IM(BaseAPI, slacker.IM):
pass
class API(BaseAPI, slacker.API):
pass
class DND(BaseAPI, slacker.DND):
pass
class RTM(BaseAPI, slacker.RTM):
pass
class Auth(BaseAPI, slacker.Auth):
pass
class Bots(BaseAPI, slacker.Bots):
pass
class Chat(BaseAPI, slacker.Chat):
pass
class TeamProfile(BaseAPI, slacker.TeamProfile):
pass
class Team(BaseAPI, slacker.Team):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._profile = TeamProfile(*args, **kwargs)
def close(self):
coros = [
super().close(),
self._profile.close(),
]
return asyncio.gather(
*coros,
loop=self.loop,
return_exceptions=True
)
class Pins(BaseAPI, slacker.Pins):
pass
class MPIM(BaseAPI, slacker.MPIM):
pass
class UsersProfile(BaseAPI, slacker.UsersProfile):
pass
class Users(BaseAPI, slacker.Users):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._profile = UsersProfile(*args, **kwargs)
def close(self):
coros = [
super().close(),
self._profile.close(),
]
return asyncio.gather(
*coros,
loop=self.loop,
return_exceptions=True
)
class FilesComments(BaseAPI, slacker.FilesComments):
pass
class Files(BaseAPI, slacker.Files):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._comments = FilesComments(*args, **kwargs)
def close(self):
coros = [
super().close(),
self._comments.close(),
]
return asyncio.gather(
*coros,
loop=self.loop,
return_exceptions=True
)
class Stars(BaseAPI, slacker.Stars):
pass
class Emoji(BaseAPI, slacker.Emoji):
pass
class Search(BaseAPI, slacker.Search):
pass
class Groups(BaseAPI, slacker.Groups):
pass
class Channels(BaseAPI, slacker.Channels):
pass
class Presence(BaseAPI, slacker.Presence):
pass
class Reminders(BaseAPI, slacker.Reminders):
pass
class Reactions(BaseAPI, slacker.Reactions):
pass
class IDPGroups(BaseAPI, slacker.IDPGroups):
pass
class UserGroupsUsers(BaseAPI, slacker.UserGroupsUsers):
pass
class UserGroups(BaseAPI, slacker.UserGroups):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._users = UserGroupsUsers(*args, **kwargs)
def close(self):
coros = [
super().close(),
self._users.close(),
]
return asyncio.gather(
*coros,
loop=self.loop,
return_exceptions=True
)
class IncomingWebhook(BaseAPI, slacker.IncomingWebhook):
def __init__(
self,
url=None,
timeout=slacker.DEFAULT_TIMEOUT,
*, loop=None
):
self.url = url
super().__init__(token=None, timeout=timeout, loop=loop)
@asyncio.coroutine
def post(self, data):
if not self.url:
raise slacker.Error('URL for incoming webhook is undefined')
_request = self.session.request(
'POST',
self.url,
data=data,
timeout=None,
)
_response = None
try:
with async_timeout.timeout(self.timeout, loop=self.loop):
_response = yield from _request
_response.raise_for_status()
_json = yield from _response.json()
finally:
if _response is not None:
yield from _response.release()
return _json
class Slacker(slacker.Slacker):
def __init__(
self,
token,
incoming_webhook_url=None,
timeout=slacker.DEFAULT_TIMEOUT,
*, loop=None
):
if loop is None:
loop = asyncio.get_event_loop()
self.loop = loop
self.im = IM(
token=token,
timeout=timeout,
loop=self.loop,
)
self.api = API(
token=token,
timeout=timeout,
loop=self.loop,
)
self.dnd = DND(
token=token,
timeout=timeout,
loop=self.loop,
)
self.rtm = RTM(
token=token,
timeout=timeout,
loop=self.loop,
)
self.auth = Auth(
token=token,
timeout=timeout,
loop=self.loop,
)
self.bots = Bots(
token=token,
timeout=timeout,
loop=self.loop,
)
self.chat = Chat(
token=token,
timeout=timeout,
loop=self.loop,
)
self.team = Team(
token=token,
timeout=timeout,
loop=self.loop,
)
self.pins = Pins(
token=token,
timeout=timeout,
loop=self.loop,
)
self.mpim = MPIM(
token=token,
timeout=timeout,
loop=self.loop,
)
self.users = Users(
token=token,
timeout=timeout,
loop=self.loop,
)
self.files = Files(
token=token,
timeout=timeout,
loop=self.loop,
)
self.stars = Stars(
token=token,
timeout=timeout,
loop=self.loop,
)
self.emoji = Emoji(
token=token,
timeout=timeout,
loop=self.loop,
)
self.search = Search(
token=token,
timeout=timeout,
loop=self.loop,
)
self.groups = Groups(
token=token,
timeout=timeout,
loop=self.loop,
)
self.channels = Channels(
token=token,
timeout=timeout,
loop=self.loop,
)
self.presence = Presence(
token=token,
timeout=timeout,
loop=self.loop,
)
self.reminders = Reminders(
token=token,
timeout=timeout,
loop=self.loop,
)
self.reactions = Reactions(
token=token,
timeout=timeout,
loop=self.loop,
)
self.idpgroups = IDPGroups(
token=token,
timeout=timeout,
loop=self.loop,
)
self.usergroups = UserGroups(
token=token,
timeout=timeout,
loop=self.loop,
)
self.incomingwebhook = IncomingWebhook(
url=incoming_webhook_url,
timeout=timeout,
loop=self.loop,
)
def close(self):
coros = [
self.im.close(),
self.api.close(),
self.dnd.close(),
self.rtm.close(),
self.auth.close(),
self.bots.close(),
self.chat.close(),
self.team.close(),
self.pins.close(),
self.mpim.close(),
self.users.close(),
self.files.close(),
self.stars.close(),
self.emoji.close(),
self.search.close(),
self.groups.close(),
self.channels.close(),
self.presence.close(),
self.reminders.close(),
self.reactions.close(),
self.idpgroups.close(),
self.usergroups.close(),
self.incomingwebhook.close(),
]
return asyncio.gather(
*coros,
loop=self.loop,
return_exceptions=True
)
if PY_350:
@asyncio.coroutine
def __aenter__(self): # noqa
return self
@asyncio.coroutine
def __aexit__(self, *exc_info): # noqa
yield from self.close()