diff --git a/trakt/core.py b/trakt/core.py index b48d08b..e3712e0 100644 --- a/trakt/core.py +++ b/trakt/core.py @@ -72,6 +72,9 @@ #: The ID of the application to register with, when using PIN authentication APPLICATION_ID = None +#: Timeout in seconds for all requests +TIMEOUT = 30 + #: Global session to make requests with session = requests.Session() @@ -141,7 +144,7 @@ def pin_auth(pin=None, client_id=None, client_secret=None, store=False): 'client_id': CLIENT_ID, 'client_secret': CLIENT_SECRET} - response = session.post(''.join([BASE_URL, '/oauth/token']), data=args) + response = session.post(''.join([BASE_URL, '/oauth/token']), data=args, timeout=TIMEOUT) OAUTH_TOKEN = response.json().get('access_token', None) if store: @@ -231,7 +234,7 @@ def get_device_code(client_id=None, client_secret=None): data = {"client_id": CLIENT_ID} device_response = session.post(device_code_url, - json=data, headers=headers).json() + json=data, headers=headers, timeout=TIMEOUT).json() print('Your user code is: {user_code}, please navigate to ' '{verification_url} to authenticate'.format( user_code=device_response.get('user_code'), @@ -272,7 +275,7 @@ def get_device_token(device_code, client_id=None, client_secret=None, } response = session.post( - urljoin(BASE_URL, '/oauth/device/token'), json=data + urljoin(BASE_URL, '/oauth/device/token'), json=data, timeout=TIMEOUT ) # We only get json on success. @@ -415,7 +418,7 @@ def _refresh_token(s): 'redirect_uri': REDIRECT_URI, 'grant_type': 'refresh_token' } - response = session.post(url, json=data, headers=HEADERS) + response = session.post(url, json=data, headers=HEADERS, timeout=TIMEOUT) s.logger.debug('RESPONSE [post] (%s): %s - %s', url, str(response), response.content) if response.status_code == 200: data = response.json() @@ -542,10 +545,10 @@ def _handle_request(self, method, url, data=None): self.logger.debug('method, url :: %s, %s', method, url) if method == 'get': # GETs need to pass data as params, not body response = session.request(method, url, headers=HEADERS, - params=data) + params=data, timeout=TIMEOUT) else: response = session.request(method, url, headers=HEADERS, - data=json.dumps(data)) + data=json.dumps(data), timeout=TIMEOUT) self.logger.debug('RESPONSE [%s] (%s): %s', method, url, str(response)) if response.status_code in self.error_map: raise self.error_map[response.status_code](response)