Skip to content
This repository was archived by the owner on Jul 3, 2020. It is now read-only.

Commit 8abb933

Browse files
committed
Merge pull request #39 from alexjurkiewicz/timeout
Support default timeout for API calls.
2 parents 364bbe8 + 44008dd commit 8abb933

File tree

1 file changed

+27
-22
lines changed

1 file changed

+27
-22
lines changed

slacker/__init__.py

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121

2222
API_BASE_URL = 'https://slack.com/api/{api}'
23+
DEFAULT_TIMEOUT = 10
2324

2425

2526
__all__ = ['Error', 'Response', 'BaseAPI', 'API', 'Auth', 'Users', 'Groups',
@@ -41,14 +42,16 @@ def __init__(self, body):
4142

4243

4344
class BaseAPI(object):
44-
def __init__(self, token=None):
45+
def __init__(self, token=None, timeout=DEFAULT_TIMEOUT):
4546
self.token = token
47+
self.timeout = timeout
4648

4749
def _request(self, method, api, **kwargs):
4850
if self.token:
4951
kwargs.setdefault('params', {})['token'] = self.token
5052

5153
response = method(API_BASE_URL.format(api=api),
54+
timeout=self.timeout,
5255
**kwargs)
5356

5457
response.raise_for_status()
@@ -461,8 +464,9 @@ def access(self, client_id, client_secret, code, redirect_uri=None):
461464

462465

463466
class IncomingWebhook(object):
464-
def __init__(self, url=None):
467+
def __init__(self, url=None, timeout=DEFAULT_TIMEOUT):
465468
self.url = url
469+
self.timeout = timeout
466470

467471
def post(self, data):
468472
"""
@@ -472,26 +476,27 @@ def post(self, data):
472476
if not self.url:
473477
raise Error('URL for incoming webhook is undefined')
474478

475-
return requests.post(self.url, data=json.dumps(data))
479+
return requests.post(self.url, data=json.dumps(data),
480+
timeout=self.timeout)
476481

477482

478483
class Slacker(object):
479-
oauth = OAuth()
480-
481-
def __init__(self, token, incoming_webhook_url=None):
482-
self.im = IM(token=token)
483-
self.api = API(token=token)
484-
self.rtm = RTM(token=token)
485-
self.auth = Auth(token=token)
486-
self.chat = Chat(token=token)
487-
self.team = Team(token=token)
488-
self.users = Users(token=token)
489-
self.files = Files(token=token)
490-
self.stars = Stars(token=token)
491-
self.emoji = Emoji(token=token)
492-
self.search = Search(token=token)
493-
self.groups = Groups(token=token)
494-
self.channels = Channels(token=token)
495-
self.presence = Presence(token=token)
496-
self.reactions = Reactions(token=token)
497-
self.incomingwebhook = IncomingWebhook(url=incoming_webhook_url)
484+
oauth = OAuth(timeout=DEFAULT_TIMEOUT)
485+
486+
def __init__(self, token, incoming_webhook_url=None, timeout=DEFAULT_TIMEOUT):
487+
self.im = IM(token=token, timeout=timeout)
488+
self.api = API(token=token, timeout=timeout)
489+
self.rtm = RTM(token=token, timeout=timeout)
490+
self.auth = Auth(token=token, timeout=timeout)
491+
self.chat = Chat(token=token, timeout=timeout)
492+
self.team = Team(token=token, timeout=timeout)
493+
self.users = Users(token=token, timeout=timeout)
494+
self.files = Files(token=token, timeout=timeout)
495+
self.stars = Stars(token=token, timeout=timeout)
496+
self.emoji = Emoji(token=token, timeout=timeout)
497+
self.search = Search(token=token, timeout=timeout)
498+
self.groups = Groups(token=token, timeout=timeout)
499+
self.channels = Channels(token=token, timeout=timeout)
500+
self.presence = Presence(token=token, timeout=timeout)
501+
self.reactions = Reactions(token=token, timeout=timeout)
502+
self.incomingwebhook = IncomingWebhook(url=incoming_webhook_url, timeout=timeout)

0 commit comments

Comments
 (0)