Skip to content

Commit 8ed6828

Browse files
U1F984glensc
andauthored
Add timeout to all requests (#50)
* Add timeout to all requests Co-authored-by: Elan Ruusamäe <[email protected]>
1 parent 11bf3c0 commit 8ed6828

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

trakt/core.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@
7272
#: The ID of the application to register with, when using PIN authentication
7373
APPLICATION_ID = None
7474

75+
#: Timeout in seconds for all requests
76+
TIMEOUT = 30
77+
7578
#: Global session to make requests with
7679
session = requests.Session()
7780

@@ -141,7 +144,7 @@ def pin_auth(pin=None, client_id=None, client_secret=None, store=False):
141144
'client_id': CLIENT_ID,
142145
'client_secret': CLIENT_SECRET}
143146

144-
response = session.post(''.join([BASE_URL, '/oauth/token']), data=args)
147+
response = session.post(''.join([BASE_URL, '/oauth/token']), data=args, timeout=TIMEOUT)
145148
OAUTH_TOKEN = response.json().get('access_token', None)
146149

147150
if store:
@@ -231,7 +234,7 @@ def get_device_code(client_id=None, client_secret=None):
231234
data = {"client_id": CLIENT_ID}
232235

233236
device_response = session.post(device_code_url,
234-
json=data, headers=headers).json()
237+
json=data, headers=headers, timeout=TIMEOUT).json()
235238
print('Your user code is: {user_code}, please navigate to '
236239
'{verification_url} to authenticate'.format(
237240
user_code=device_response.get('user_code'),
@@ -272,7 +275,7 @@ def get_device_token(device_code, client_id=None, client_secret=None,
272275
}
273276

274277
response = session.post(
275-
urljoin(BASE_URL, '/oauth/device/token'), json=data
278+
urljoin(BASE_URL, '/oauth/device/token'), json=data, timeout=TIMEOUT
276279
)
277280

278281
# We only get json on success.
@@ -415,7 +418,7 @@ def _refresh_token(s):
415418
'redirect_uri': REDIRECT_URI,
416419
'grant_type': 'refresh_token'
417420
}
418-
response = session.post(url, json=data, headers=HEADERS)
421+
response = session.post(url, json=data, headers=HEADERS, timeout=TIMEOUT)
419422
s.logger.debug('RESPONSE [post] (%s): %s - %s', url, str(response), response.content)
420423
if response.status_code == 200:
421424
data = response.json()
@@ -542,10 +545,10 @@ def _handle_request(self, method, url, data=None):
542545
self.logger.debug('method, url :: %s, %s', method, url)
543546
if method == 'get': # GETs need to pass data as params, not body
544547
response = session.request(method, url, headers=HEADERS,
545-
params=data)
548+
params=data, timeout=TIMEOUT)
546549
else:
547550
response = session.request(method, url, headers=HEADERS,
548-
data=json.dumps(data))
551+
data=json.dumps(data), timeout=TIMEOUT)
549552
self.logger.debug('RESPONSE [%s] (%s): %s', method, url, str(response))
550553
if response.status_code in self.error_map:
551554
raise self.error_map[response.status_code](response)

0 commit comments

Comments
 (0)