|
72 | 72 | #: The ID of the application to register with, when using PIN authentication |
73 | 73 | APPLICATION_ID = None |
74 | 74 |
|
| 75 | +#: Timeout in seconds for all requests |
| 76 | +TIMEOUT = 30 |
| 77 | + |
75 | 78 | #: Global session to make requests with |
76 | 79 | session = requests.Session() |
77 | 80 |
|
@@ -141,7 +144,7 @@ def pin_auth(pin=None, client_id=None, client_secret=None, store=False): |
141 | 144 | 'client_id': CLIENT_ID, |
142 | 145 | 'client_secret': CLIENT_SECRET} |
143 | 146 |
|
144 | | - response = session.post(''.join([BASE_URL, '/oauth/token']), data=args) |
| 147 | + response = session.post(''.join([BASE_URL, '/oauth/token']), data=args, timeout=TIMEOUT) |
145 | 148 | OAUTH_TOKEN = response.json().get('access_token', None) |
146 | 149 |
|
147 | 150 | if store: |
@@ -231,7 +234,7 @@ def get_device_code(client_id=None, client_secret=None): |
231 | 234 | data = {"client_id": CLIENT_ID} |
232 | 235 |
|
233 | 236 | device_response = session.post(device_code_url, |
234 | | - json=data, headers=headers).json() |
| 237 | + json=data, headers=headers, timeout=TIMEOUT).json() |
235 | 238 | print('Your user code is: {user_code}, please navigate to ' |
236 | 239 | '{verification_url} to authenticate'.format( |
237 | 240 | user_code=device_response.get('user_code'), |
@@ -272,7 +275,7 @@ def get_device_token(device_code, client_id=None, client_secret=None, |
272 | 275 | } |
273 | 276 |
|
274 | 277 | response = session.post( |
275 | | - urljoin(BASE_URL, '/oauth/device/token'), json=data |
| 278 | + urljoin(BASE_URL, '/oauth/device/token'), json=data, timeout=TIMEOUT |
276 | 279 | ) |
277 | 280 |
|
278 | 281 | # We only get json on success. |
@@ -415,7 +418,7 @@ def _refresh_token(s): |
415 | 418 | 'redirect_uri': REDIRECT_URI, |
416 | 419 | 'grant_type': 'refresh_token' |
417 | 420 | } |
418 | | - response = session.post(url, json=data, headers=HEADERS) |
| 421 | + response = session.post(url, json=data, headers=HEADERS, timeout=TIMEOUT) |
419 | 422 | s.logger.debug('RESPONSE [post] (%s): %s - %s', url, str(response), response.content) |
420 | 423 | if response.status_code == 200: |
421 | 424 | data = response.json() |
@@ -542,10 +545,10 @@ def _handle_request(self, method, url, data=None): |
542 | 545 | self.logger.debug('method, url :: %s, %s', method, url) |
543 | 546 | if method == 'get': # GETs need to pass data as params, not body |
544 | 547 | response = session.request(method, url, headers=HEADERS, |
545 | | - params=data) |
| 548 | + params=data, timeout=TIMEOUT) |
546 | 549 | else: |
547 | 550 | response = session.request(method, url, headers=HEADERS, |
548 | | - data=json.dumps(data)) |
| 551 | + data=json.dumps(data), timeout=TIMEOUT) |
549 | 552 | self.logger.debug('RESPONSE [%s] (%s): %s', method, url, str(response)) |
550 | 553 | if response.status_code in self.error_map: |
551 | 554 | raise self.error_map[response.status_code](response) |
|
0 commit comments