From b96fd5ca8de03209adb041d6a424982ac26c8517 Mon Sep 17 00:00:00 2001 From: Paul Lamere Date: Tue, 28 Apr 2015 06:25:06 -0400 Subject: [PATCH] Fixed problem with retry support --- CHANGES.txt | 1 + setup.py | 2 +- spotipy/client.py | 14 ++++++++++---- tests/tests.py | 3 ++- 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index db37f3f..d1f9dd2 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -12,3 +12,4 @@ v2.310, August 20, 2014 -- Added playlist replace and remove methods. Added auth tests. Improved API docs v2.310, January 05, 2015 -- Added session support v2.3.1, March 28, 2015 -- auto retry support +v2.3.3, April 28, 2015 -- fixed bug in auto retry support diff --git a/setup.py b/setup.py index 0a3f195..e96b0f4 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ from setuptools import setup setup( name='spotipy', - version='2.3.3', + version='2.3.4', description='simple client for the Spotify Web API', author="@plamere", author_email="paul@echonest.com", diff --git a/spotipy/client.py b/spotipy/client.py index 34bb6ab..d4c12ea 100644 --- a/spotipy/client.py +++ b/spotipy/client.py @@ -40,7 +40,7 @@ class Spotify(object): ''' trace = False # Enable tracing? - max_get_retries = 5 + max_get_retries = 10 def __init__(self, auth=None, requests_session=True, client_credentials_manager=None): @@ -115,16 +115,22 @@ class Spotify(object): if args: kwargs.update(args) retries = self.max_get_retries + delay = 1 while retries > 0: try: return self._internal_call('GET', url, payload, kwargs) except SpotifyException as e: - if e.http_status >= 500 and e.http_status < 600: - retries -= 1 + retries -= 1 + status = e.http_status + # 429 means we hit a rate limit, backoff + if status == 429 or status >= 500 and status < 600: if retries < 0: raise else: - time.sleep(1) + if self.trace: + print ('retrying ...' + str(delay) + 'secs') + time.sleep(delay) + delay += 1 else: raise diff --git a/tests/tests.py b/tests/tests.py index 0157ae5..3f244fe 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -118,7 +118,8 @@ class TestSpotipy(unittest.TestCase): def test_unauthenticated_post_fails(self): with self.assertRaises(SpotifyException) as cm: self.spotify.user_playlist_create("spotify", "Best hits of the 90s") - self.assertEqual(cm.exception.http_status, 401) + self.assertTrue(cm.exception.http_status == 401 or + cm.exception.http_status == 403) def test_custom_requests_session(self): from requests import Session