Skip to content

Commit c09dd4b

Browse files
DoergeRoach
authored andcommitted
Added timeout to api calls. (#161)
1 parent c990314 commit c09dd4b

File tree

3 files changed

+13
-10
lines changed

3 files changed

+13
-10
lines changed

slackclient/_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def rtm_connect(self):
4444
except:
4545
return False
4646

47-
def api_call(self, method, **kwargs):
47+
def api_call(self, method, timeout=None, **kwargs):
4848
'''
4949
Call the Slack Web API as documented here: https://api.slack.com/web
5050
@@ -74,7 +74,7 @@ def api_call(self, method, **kwargs):
7474
7575
See here for more information on responses: https://api.slack.com/web
7676
'''
77-
result = json.loads(self.server.api_call(method, **kwargs))
77+
result = json.loads(self.server.api_call(method, timeout=timeout, **kwargs))
7878
if self.server:
7979
if method == 'im.open':
8080
if "ok" in result and result["ok"]:

slackclient/_server.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ def __str__(self):
6161
def __repr__(self):
6262
return self.__str__()
6363

64-
def rtm_connect(self, reconnect=False):
65-
reply = self.api_requester.do(self.token, "rtm.start")
64+
def rtm_connect(self, reconnect=False, timeout=None):
65+
reply = self.api_requester.do(self.token, "rtm.start", timeout=timeout)
6666
if reply.status_code != 200:
6767
raise SlackConnectionError
6868
else:
@@ -157,24 +157,26 @@ def attach_channel(self, name, channel_id, members=None):
157157
if self.channels.find(channel_id) is None:
158158
self.channels.append(Channel(self, name, channel_id, members))
159159

160-
def join_channel(self, name):
160+
def join_channel(self, name, timeout=None):
161161
'''
162162
Join a channel by name.
163163
164164
Note: this action is not allowed by bots, they must be invited to channels.
165165
'''
166166
return self.api_requester.do(
167167
self.token,
168-
"channels.join?name={}".format(name)
168+
"channels.join?name={}".format(name),
169+
timeout=timeout
169170
).text
170171

171-
def api_call(self, method, **kwargs):
172+
def api_call(self, method, timeout=None, **kwargs):
172173
'''
173174
Call the Slack Web API as documented here: https://api.slack.com/web
174175
175176
:Args:
176177
method (str): The API Method to call. See here for a list: https://api.slack.com/methods
177178
:Kwargs:
179+
(optional) timeout: stop waiting for a response after a given number of seconds
178180
(optional) kwargs: any arguments passed here will be bundled and sent to the api
179181
requester as post_data
180182
and will be passed along to the API.
@@ -198,7 +200,7 @@ def api_call(self, method, **kwargs):
198200
199201
See here for more information on responses: https://api.slack.com/web
200202
'''
201-
return self.api_requester.do(self.token, method, kwargs).text
203+
return self.api_requester.do(self.token, method, kwargs, timeout=timeout).text
202204

203205

204206
class SlackConnectionError(Exception):

slackclient/_slackrequest.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
class SlackRequest(object):
88

99
@staticmethod
10-
def do(token, request="?", post_data=None, domain="slack.com"):
10+
def do(token, request="?", post_data=None, domain="slack.com", timeout=None):
1111
'''
1212
Perform a POST request to the Slack Web API
1313
1414
Args:
1515
token (str): your authentication token
1616
request (str): the method to call from the Slack API. For example: 'channels.list'
17+
timeout (float): stop waiting for a response after a given number of seconds
1718
post_data (dict): key/value arguments to pass for the request. For example:
1819
{'channel': 'CABC12345'}
1920
domain (str): if for some reason you want to send your request to something other
@@ -36,4 +37,4 @@ def do(token, request="?", post_data=None, domain="slack.com"):
3637
url = 'https://{0}/api/{1}'.format(domain, request)
3738
post_data['token'] = token
3839

39-
return requests.post(url, data=post_data, files=files)
40+
return requests.post(url, data=post_data, files=files, timeout=timeout)

0 commit comments

Comments
 (0)