Skip to content

Commit

Permalink
Retry for POST, PUT and DELETE, fixes #577 (#596)
Browse files Browse the repository at this point in the history
* Retry for POST, PUT and DELETE, fixes #577

* Lint
  • Loading branch information
stephanebruckert committed Oct 24, 2020
1 parent 275cd7e commit dd948c4
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 12 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Unreleased

### Fixed

- playlist_tracks example code no longer prints extra characters on final loop iteration
- SpotifyException now thrown when a request fails & has no response ( fixes #571, #581 )
- Added scope, 'playlist-read-private', to examples that access user playlists using the spotipy api: current_user_playlists() (fixes #591)
- Enable retries for POST, DELETE, PUT

### Changed

- both inline and starting import lists are sorted using `isort` module
- changed exception Max Retries exception code from 599 to 429

## [2.16.0] - 2020-09-16

Expand Down
3 changes: 2 additions & 1 deletion spotipy/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ def _build_session(self):
total=self.retries,
connect=None,
read=False,
method_whitelist=frozenset(['GET', 'POST', 'PUT', 'DELETE']),
status=self.status_retries,
backoff_factor=self.backoff_factor,
status_forcelist=self.status_forcelist)
Expand Down Expand Up @@ -272,7 +273,7 @@ def _internal_call(self, method, url, payload, params):
except (IndexError, AttributeError):
reason = None
raise SpotifyException(
599,
429,
-1,
"%s:\n %s" % (request.path_url, "Max Retries"),
reason=reason
Expand Down
4 changes: 0 additions & 4 deletions tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,5 @@ def get_spotify_playlist(spotify_object, playlist_name, username):
playlists = spotify_object.next(playlists)


def create_spotify_playlist(spotify_object, playlist_name, username):
return spotify_object.user_playlist_create(username, playlist_name)


def get_as_base64(url):
return base64.b64encode(requests.get(url).content).decode("utf-8")
7 changes: 4 additions & 3 deletions tests/integration/test_non_user_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,16 +240,17 @@ def test_search_timeout(self):
self.assertRaises((requests.exceptions.Timeout, requests.exceptions.ConnectionError),
lambda: sp.search(q='my*', type='track'))

def test_max_retries_reached(self):
def test_max_retries_reached_get(self):
spotify_no_retry = Spotify(
client_credentials_manager=SpotifyClientCredentials(),
retries=0)
i = 0
while i < 100:
try:
spotify_no_retry.search(q='foo')
except spotipy.exceptions.SpotifyException as e:
self.assertIsInstance(e, spotipy.exceptions.SpotifyException)
except SpotifyException as e:
self.assertIsInstance(e, SpotifyException)
self.assertEqual(e.http_status, 429)
return
i += 1
self.fail()
Expand Down
20 changes: 16 additions & 4 deletions tests/integration/test_user_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,11 @@ def setUpClass(cls):
token = prompt_for_user_token(cls.username, scope=scope)

cls.spotify = Spotify(auth=token)

cls.spotify_no_retry = Spotify(auth=token, retries=0)
cls.new_playlist_name = 'spotipy-playlist-test'
cls.new_playlist = helpers.get_spotify_playlist(
cls.spotify, cls.new_playlist_name, cls.username) or \
helpers.create_spotify_playlist(
cls.spotify, cls.new_playlist_name, cls.username)
cls.spotify.user_playlist_create(cls.username, cls.new_playlist_name)
cls.new_playlist_uri = cls.new_playlist['uri']

def test_user_playlists(self):
Expand Down Expand Up @@ -120,6 +119,19 @@ def test_get_playlist_by_id(self):
pl = self.spotify.playlist(self.new_playlist['id'])
self.assertEqual(pl["tracks"]["total"], 0)

def test_max_retries_reached_post(self):
i = 0
while i < 100:
try:
self.spotify_no_retry.playlist_change_details(
self.new_playlist['id'], description="test")
except SpotifyException as e:
self.assertIsInstance(e, SpotifyException)
self.assertEqual(e.http_status, 429)
return
i += 1
self.fail()

def test_playlist_add_items(self):
# add tracks to playlist
self.spotify.playlist_add_items(
Expand Down Expand Up @@ -223,7 +235,7 @@ def test_current_user_save_and_unsave_tracks(self):
new_total = tracks['total']
self.assertEqual(new_total - total, len(self.four_tracks))

tracks = self.spotify.current_user_saved_tracks_delete(
self.spotify.current_user_saved_tracks_delete(
self.four_tracks)
tracks = self.spotify.current_user_saved_tracks()
new_total = tracks['total']
Expand Down

0 comments on commit dd948c4

Please sign in to comment.