diff options
author | Lorenz Diener <[email protected]> | 2016-12-13 17:17:33 +0100 |
---|---|---|
committer | Lorenz Diener <[email protected]> | 2016-12-13 17:17:33 +0100 |
commit | 19a1b014d4bfa721c5f091c47b3d6d12bfc32664 (patch) | |
tree | 9a842cc1513e050e8205653fc9492631eaa71aa7 /mastodon | |
parent | f1c445dc1036beb4abac6305fdd5e8e64e7da6d2 (diff) | |
download | mastodon.py-19a1b014d4bfa721c5f091c47b3d6d12bfc32664.tar.gz |
Added timeout
Diffstat (limited to 'mastodon')
-rw-r--r-- | mastodon/Mastodon.py | 33 |
1 files changed, 21 insertions, 12 deletions
diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index 349bc03..7cbdcc4 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py | |||
@@ -24,6 +24,7 @@ class Mastodon: | |||
24 | patch in Real Proper OAuth if desired. | 24 | patch in Real Proper OAuth if desired. |
25 | """ | 25 | """ |
26 | __DEFAULT_BASE_URL = 'https://mastodon.social' | 26 | __DEFAULT_BASE_URL = 'https://mastodon.social' |
27 | __DEFAULT_TIMEOUT = 300 | ||
27 | 28 | ||
28 | 29 | ||
29 | ### | 30 | ### |
@@ -48,13 +49,16 @@ class Mastodon: | |||
48 | 'scopes': " ".join(scopes) | 49 | 'scopes': " ".join(scopes) |
49 | } | 50 | } |
50 | 51 | ||
51 | if redirect_uris != None: | 52 | try: |
52 | request_data['redirect_uris'] = redirect_uris; | 53 | if redirect_uris != None: |
53 | else: | 54 | request_data['redirect_uris'] = redirect_uris; |
54 | request_data['redirect_uris'] = 'urn:ietf:wg:oauth:2.0:oob'; | 55 | else: |
55 | 56 | request_data['redirect_uris'] = 'urn:ietf:wg:oauth:2.0:oob'; | |
56 | response = requests.post(api_base_url + '/api/v1/apps', data = request_data).json() | 57 | |
57 | 58 | response = requests.post(api_base_url + '/api/v1/apps', data = request_data, timeout = self.request_timeout).json() | |
59 | except: | ||
60 | raise MastodonNetworkError("Could not complete request.") | ||
61 | |||
58 | if to_file != None: | 62 | if to_file != None: |
59 | with open(to_file, 'w') as secret_file: | 63 | with open(to_file, 'w') as secret_file: |
60 | secret_file.write(response['client_id'] + '\n') | 64 | secret_file.write(response['client_id'] + '\n') |
@@ -65,7 +69,7 @@ class Mastodon: | |||
65 | ### | 69 | ### |
66 | # Authentication, including constructor | 70 | # Authentication, including constructor |
67 | ### | 71 | ### |
68 | def __init__(self, client_id, client_secret = None, access_token = None, api_base_url = __DEFAULT_BASE_URL, debug_requests = False, ratelimit_method = "wait", ratelimit_pacefactor = 1.1): | 72 | def __init__(self, client_id, client_secret = None, access_token = None, api_base_url = __DEFAULT_BASE_URL, debug_requests = False, ratelimit_method = "wait", ratelimit_pacefactor = 1.1, request_timeout = __DEFAULT_TIMEOUT): |
69 | """ | 73 | """ |
70 | Create a new API wrapper instance based on the given client_secret and client_id. If you | 74 | Create a new API wrapper instance based on the given client_secret and client_id. If you |
71 | give a client_id and it is not a file, you must also give a secret. | 75 | give a client_id and it is not a file, you must also give a secret. |
@@ -82,7 +86,10 @@ class Mastodon: | |||
82 | note that "pace" and "wait" are NOT thread safe. | 86 | note that "pace" and "wait" are NOT thread safe. |
83 | 87 | ||
84 | Specify api_base_url if you wish to talk to an instance other than the flagship one. | 88 | Specify api_base_url if you wish to talk to an instance other than the flagship one. |
85 | If a file is given as client_id, read client ID and secret from that file | 89 | If a file is given as client_id, read client ID and secret from that file. |
90 | |||
91 | By defautl, a timeout of 300 seconds is used for all requests. If you wish to change this, | ||
92 | pass the desired timeout (in seconds) as request_timeout. | ||
86 | """ | 93 | """ |
87 | self.api_base_url = api_base_url | 94 | self.api_base_url = api_base_url |
88 | self.client_id = client_id | 95 | self.client_id = client_id |
@@ -97,6 +104,8 @@ class Mastodon: | |||
97 | self.ratelimit_lastcall = time.time() | 104 | self.ratelimit_lastcall = time.time() |
98 | self.ratelimit_pacefactor = ratelimit_pacefactor | 105 | self.ratelimit_pacefactor = ratelimit_pacefactor |
99 | 106 | ||
107 | self.request_timeout = request_timeout | ||
108 | |||
100 | if not ratelimit_method in ["throw", "wait", "pace"]: | 109 | if not ratelimit_method in ["throw", "wait", "pace"]: |
101 | raise MastodonIllegalArgumentError("Invalid ratelimit method.") | 110 | raise MastodonIllegalArgumentError("Invalid ratelimit method.") |
102 | 111 | ||
@@ -510,13 +519,13 @@ class Mastodon: | |||
510 | response_object = None | 519 | response_object = None |
511 | try: | 520 | try: |
512 | if method == 'GET': | 521 | if method == 'GET': |
513 | response_object = requests.get(self.api_base_url + endpoint, data = params, headers = headers, files = files) | 522 | response_object = requests.get(self.api_base_url + endpoint, data = params, headers = headers, files = files, timeout = self.request_timeout) |
514 | 523 | ||
515 | if method == 'POST': | 524 | if method == 'POST': |
516 | response_object = requests.post(self.api_base_url + endpoint, data = params, headers = headers, files = files) | 525 | response_object = requests.post(self.api_base_url + endpoint, data = params, headers = headers, files = files, timeout = self.request_timeout) |
517 | 526 | ||
518 | if method == 'DELETE': | 527 | if method == 'DELETE': |
519 | response_object = requests.delete(self.api_base_url + endpoint, data = params, headers = headers, files = files) | 528 | response_object = requests.delete(self.api_base_url + endpoint, data = params, headers = headers, files = files, timeout = self.request_timeout) |
520 | except: | 529 | except: |
521 | raise MastodonNetworkError("Could not complete request.") | 530 | raise MastodonNetworkError("Could not complete request.") |
522 | 531 | ||