Skip to content

Commit 66ad716

Browse files
authored
Print warnings when a rate/request limit is reached (#1134)
* create custom urllib3.Retry class for printing warnings on rate/request limits * move import urllib3 from client.py to util.py * Using Retry.increment instead of Retry.is_retry. Shows the Retry-After value in the warning as well * Making sure that max column <= 99 * add types.TracebackType * Change warning in request/rate limit warning * adding all parameters, just to make sure * fixing length of line * making sure that response is not None
1 parent 5e09c78 commit 66ad716

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Add your changes below.
1515
- Updated TUTORIAL.md instructions to match current layout of Spotify Developer Dashboard
1616
- Added test_artist_id, test_artist_url, and test_artists_mixed_ids to non_user_endpoints test.py
1717
- Added rate/request limit to FAQ
18+
- Added custom `urllib3.Retry` class for printing a warning when a rate/request limit is reached.
1819

1920
### Fixed
2021
- Audiobook integration tests

spotipy/client.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
import warnings
99

1010
import requests
11-
import urllib3
1211

1312
from spotipy.exceptions import SpotifyException
13+
from spotipy.util import Retry
1414

1515
from collections import defaultdict
1616

@@ -220,7 +220,7 @@ def __del__(self):
220220

221221
def _build_session(self):
222222
self._session = requests.Session()
223-
retry = urllib3.Retry(
223+
retry = Retry(
224224
total=self.retries,
225225
connect=None,
226226
read=False,

spotipy/util.py

+31
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1+
from __future__ import annotations
2+
13
""" Shows a user's playlists. This needs to be authenticated via OAuth. """
24

35
__all__ = ["CLIENT_CREDS_ENV_VARS", "prompt_for_user_token"]
46

57
import logging
68
import os
79
import warnings
10+
from types import TracebackType
811

912
import spotipy
1013

14+
import urllib3
15+
1116
LOGGER = logging.getLogger(__name__)
1217

1318
CLIENT_CREDS_ENV_VARS = {
@@ -142,3 +147,29 @@ def normalize_scope(scope):
142147
return " ".join(sorted(scopes))
143148
else:
144149
return None
150+
151+
152+
class Retry(urllib3.Retry):
153+
"""
154+
Custom class for printing a warning when a rate/request limit is reached.
155+
"""
156+
def increment(
157+
self,
158+
method: str | None = None,
159+
url: str | None = None,
160+
response: urllib3.BaseHTTPResponse | None = None,
161+
error: Exception | None = None,
162+
_pool: urllib3.connectionpool.ConnectionPool | None = None,
163+
_stacktrace: TracebackType | None = None,
164+
) -> urllib3.Retry:
165+
if response:
166+
retry_header = response.headers.get("Retry-After")
167+
if self.is_retry(method, response.status, bool(retry_header)):
168+
logging.warning("Your application has reached a rate/request limit. "
169+
f"Retry will occur after: {retry_header}")
170+
return super().increment(method,
171+
url,
172+
response=response,
173+
error=error,
174+
_pool=_pool,
175+
_stacktrace=_stacktrace)

0 commit comments

Comments
 (0)