mirror of
https://github.com/c0de-archive/spotipy.git
synced 2024-11-05 07:27:47 +00:00
Use 'Retry-After' header if present.
This commit is contained in:
parent
39d4ee4193
commit
aecd392c4a
@ -12,10 +12,15 @@ import time
|
|||||||
'''
|
'''
|
||||||
|
|
||||||
class SpotifyException(Exception):
|
class SpotifyException(Exception):
|
||||||
def __init__(self, http_status, code, msg):
|
def __init__(self, http_status, code, msg, headers=None):
|
||||||
self.http_status = http_status
|
self.http_status = http_status
|
||||||
self.code = code
|
self.code = code
|
||||||
self.msg = msg
|
self.msg = msg
|
||||||
|
# `headers` is used to support `Retry-After` in the event of a
|
||||||
|
# 429 status code.
|
||||||
|
if headers is None:
|
||||||
|
headers = {}
|
||||||
|
self.headers = headers
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return 'http status: {0}, code:{1} - {2}'.format(
|
return 'http status: {0}, code:{1} - {2}'.format(
|
||||||
@ -108,10 +113,11 @@ class Spotify(object):
|
|||||||
except:
|
except:
|
||||||
if r.text and len(r.text) > 0 and r.text != 'null':
|
if r.text and len(r.text) > 0 and r.text != 'null':
|
||||||
raise SpotifyException(r.status_code,
|
raise SpotifyException(r.status_code,
|
||||||
-1, '%s:\n %s' % (r.url, r.json()['error']['message']))
|
-1, '%s:\n %s' % (r.url, r.json()['error']['message']),
|
||||||
|
headers=r.headers)
|
||||||
else:
|
else:
|
||||||
raise SpotifyException(r.status_code,
|
raise SpotifyException(r.status_code,
|
||||||
-1, '%s:\n %s' % (r.url, 'error'))
|
-1, '%s:\n %s' % (r.url, 'error'), headers=r.headers)
|
||||||
finally:
|
finally:
|
||||||
r.connection.close()
|
r.connection.close()
|
||||||
if r.text and len(r.text) > 0 and r.text != 'null':
|
if r.text and len(r.text) > 0 and r.text != 'null':
|
||||||
@ -139,8 +145,9 @@ class Spotify(object):
|
|||||||
if retries < 0:
|
if retries < 0:
|
||||||
raise
|
raise
|
||||||
else:
|
else:
|
||||||
print ('retrying ...' + str(delay) + 'secs')
|
sleep_seconds = int(e.headers.get('Retry-After', delay))
|
||||||
time.sleep(delay)
|
print ('retrying ...' + str(sleep_seconds) + 'secs')
|
||||||
|
time.sleep(sleep_seconds)
|
||||||
delay += 1
|
delay += 1
|
||||||
else:
|
else:
|
||||||
raise
|
raise
|
||||||
@ -151,8 +158,9 @@ class Spotify(object):
|
|||||||
# been know to throw a BadStatusLine exception
|
# been know to throw a BadStatusLine exception
|
||||||
retries -= 1
|
retries -= 1
|
||||||
if retries >= 0:
|
if retries >= 0:
|
||||||
|
sleep_seconds = int(e.headers.get('Retry-After', delay))
|
||||||
print ('retrying ...' + str(delay) + 'secs')
|
print ('retrying ...' + str(delay) + 'secs')
|
||||||
time.sleep(delay)
|
time.sleep(sleep_seconds)
|
||||||
delay += 1
|
delay += 1
|
||||||
else:
|
else:
|
||||||
raise
|
raise
|
||||||
|
Loading…
Reference in New Issue
Block a user