From a78421a6e8c16467de51012488213ea028f57de7 Mon Sep 17 00:00:00 2001 From: Paul Lamere Date: Wed, 1 Apr 2015 10:16:02 -0400 Subject: [PATCH] Added automatic retry logic for gets --- CHANGES.txt | 7 ++++--- README.md | 1 + setup.py | 2 +- spotipy/client.py | 16 +++++++++++++++- 4 files changed, 21 insertions(+), 5 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 8d9acc0..db37f3f 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -6,8 +6,9 @@ v1.45, July 7, 2014 -- Support for related artists endpoint. Don't used cache auth codes when scope changes v1.50, August 14, 2014 -- Refactored util out of examples and into the main package -v1.301, August 19, 2014 -- Upgraded version number to take precedence over +v2.301, August 19, 2014 -- Upgraded version number to take precedence over previously botched release (sigh) -v1.310, August 20, 2014 -- Added playlist replace and remove methods. Added auth +v2.310, August 20, 2014 -- Added playlist replace and remove methods. Added auth tests. Improved API docs -v1.310, January 05, 2015 -- Added session support +v2.310, January 05, 2015 -- Added session support +v2.3.1, March 28, 2015 -- auto retry support diff --git a/README.md b/README.md index 0609816..697a69f 100644 --- a/README.md +++ b/README.md @@ -65,3 +65,4 @@ If you have suggestions, bugs or other issues specific to this library, file the - v2.1.0 - October 25, 2014 -- Added support for new_releases and featured_playlists - v2.2.0 - November 15, 2014 -- Added support for user_playlist_tracks - v2.3.0 - January 5, 2015 -- Added session support added by akx. +- v2.3.2 - March 31, 2015 -- Added auto retry logic diff --git a/setup.py b/setup.py index 897bf47..91c0ad4 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ from setuptools import setup setup( name='spotipy', - version='2.3.0', + version='2.3.2', 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 110d7cc..34bb6ab 100644 --- a/spotipy/client.py +++ b/spotipy/client.py @@ -6,6 +6,7 @@ import sys import base64 import requests import json +import time ''' A simple and thin Python library for the Spotify Web API ''' @@ -39,6 +40,7 @@ class Spotify(object): ''' trace = False # Enable tracing? + max_get_retries = 5 def __init__(self, auth=None, requests_session=True, client_credentials_manager=None): @@ -112,7 +114,19 @@ class Spotify(object): def _get(self, url, args=None, payload=None, **kwargs): if args: kwargs.update(args) - return self._internal_call('GET', url, payload, kwargs) + retries = self.max_get_retries + 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 + if retries < 0: + raise + else: + time.sleep(1) + else: + raise def _post(self, url, args=None, payload=None, **kwargs): if args: