aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcodl <[email protected]>2018-04-08 23:06:09 +0200
committercodl <[email protected]>2018-04-08 23:07:08 +0200
commit06a7a875fe2705044b98f3ef285944b5513be7de (patch)
tree29d59351b48e046588523c539d0eccf398441862 /mastodon/Mastodon.py
parentd98a3546a318bca88469a1accde99ecdf46574c1 (diff)
downloadmastodon.py-06a7a875fe2705044b98f3ef285944b5513be7de.tar.gz
add timeouts to streams (GH-127)
Diffstat (limited to 'mastodon/Mastodon.py')
-rw-r--r--mastodon/Mastodon.py22
1 files changed, 12 insertions, 10 deletions
diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py
index 8b7064f..ea54649 100644
--- a/mastodon/Mastodon.py
+++ b/mastodon/Mastodon.py
@@ -89,6 +89,7 @@ class Mastodon:
89 """ 89 """
90 __DEFAULT_BASE_URL = 'https://mastodon.social' 90 __DEFAULT_BASE_URL = 'https://mastodon.social'
91 __DEFAULT_TIMEOUT = 300 91 __DEFAULT_TIMEOUT = 300
92 __DEFAULT_STREAM_TIMEOUT = 300
92 __SUPPORTED_MASTODON_VERSION = "2.2.0" 93 __SUPPORTED_MASTODON_VERSION = "2.2.0"
93 94
94 ### 95 ###
@@ -1395,37 +1396,37 @@ class Mastodon:
1395 return self.__stream('/api/v1/streaming/user', listener, async=async) 1396 return self.__stream('/api/v1/streaming/user', listener, async=async)
1396 1397
1397 @api_version("1.1.0", "1.4.2") 1398 @api_version("1.1.0", "1.4.2")
1398 def stream_public(self, listener, async=False): 1399 def stream_public(self, listener, async=False, timeout=__DEFAULT_STREAM_TIMEOUT):
1399 """ 1400 """
1400 Streams public events. 1401 Streams public events.
1401 """ 1402 """
1402 return self.__stream('/api/v1/streaming/public', listener, async=async) 1403 return self.__stream('/api/v1/streaming/public', listener, async=async, timeout=timeout)
1403 1404
1404 @api_version("1.1.0", "1.4.2") 1405 @api_version("1.1.0", "1.4.2")
1405 def stream_local(self, listener, async=False): 1406 def stream_local(self, listener, async=False, timeout=__DEFAULT_STREAM_TIMEOUT):
1406 """ 1407 """
1407 Streams local public events. 1408 Streams local public events.
1408 """ 1409 """
1409 return self.__stream('/api/v1/streaming/public/local', listener, async=async) 1410 return self.__stream('/api/v1/streaming/public/local', listener, async=async, timeout=timeout)
1410 1411
1411 @api_version("1.1.0", "1.4.2") 1412 @api_version("1.1.0", "1.4.2")
1412 def stream_hashtag(self, tag, listener, async=False): 1413 def stream_hashtag(self, tag, listener, async=False, timeout=__DEFAULT_STREAM_TIMEOUT):
1413 """ 1414 """
1414 Stream for all public statuses for the hashtag 'tag' seen by the connected 1415 Stream for all public statuses for the hashtag 'tag' seen by the connected
1415 instance. 1416 instance.
1416 """ 1417 """
1417 if tag.startswith("#"): 1418 if tag.startswith("#"):
1418 raise MastodonIllegalArgumentError("Tag parameter should omit leading #") 1419 raise MastodonIllegalArgumentError("Tag parameter should omit leading #")
1419 return self.__stream("/api/v1/streaming/hashtag?tag={}".format(tag), listener, async=async) 1420 return self.__stream("/api/v1/streaming/hashtag?tag={}".format(tag), listener, async=async, timeout=timeout)
1420 1421
1421 @api_version("2.1.0", "2.1.0") 1422 @api_version("2.1.0", "2.1.0")
1422 def stream_list(self, id, listener, async=False): 1423 def stream_list(self, id, listener, async=False, timeout=__DEFAULT_STREAM_TIMEOUT):
1423 """ 1424 """
1424 Stream events for the current user, restricted to accounts on the given 1425 Stream events for the current user, restricted to accounts on the given
1425 list. 1426 list.
1426 """ 1427 """
1427 id = self.__unpack_id(id) 1428 id = self.__unpack_id(id)
1428 return self.__stream("/api/v1/streaming/list?list={}".format(id), listener, async=async) 1429 return self.__stream("/api/v1/streaming/list?list={}".format(id), listener, async=async, timeout=timeout)
1429 1430
1430 ### 1431 ###
1431 # Internal helpers, dragons probably 1432 # Internal helpers, dragons probably
@@ -1667,7 +1668,7 @@ class Mastodon:
1667 1668
1668 return response 1669 return response
1669 1670
1670 def __stream(self, endpoint, listener, params={}, async=False): 1671 def __stream(self, endpoint, listener, params={}, async=False, timeout=__DEFAULT_STREAM_TIMEOUT):
1671 """ 1672 """
1672 Internal streaming API helper. 1673 Internal streaming API helper.
1673 1674
@@ -1697,7 +1698,8 @@ class Mastodon:
1697 url = url[:-1] 1698 url = url[:-1]
1698 1699
1699 headers = {"Authorization": "Bearer " + self.access_token} 1700 headers = {"Authorization": "Bearer " + self.access_token}
1700 connection = requests.get(url + endpoint, headers = headers, data = params, stream = True) 1701 connection = requests.get(url + endpoint, headers = headers, data = params, stream = True,
1702 timeout=(self.request_timeout, timeout))
1701 1703
1702 if connection.status_code != 200: 1704 if connection.status_code != 200:
1703 raise MastodonNetworkError("Could not connect to streaming server: %s" % connection.reason) 1705 raise MastodonNetworkError("Could not connect to streaming server: %s" % connection.reason)
Powered by cgit v1.2.3 (git 2.41.0)