|
| 1 | +from __future__ import annotations |
| 2 | + |
1 | 3 | """ Shows a user's playlists. This needs to be authenticated via OAuth. """
|
2 | 4 |
|
3 | 5 | __all__ = ["CLIENT_CREDS_ENV_VARS", "prompt_for_user_token"]
|
4 | 6 |
|
5 | 7 | import logging
|
6 | 8 | import os
|
7 | 9 | import warnings
|
| 10 | +from types import TracebackType |
8 | 11 |
|
9 | 12 | import spotipy
|
10 | 13 |
|
| 14 | +import urllib3 |
| 15 | + |
11 | 16 | LOGGER = logging.getLogger(__name__)
|
12 | 17 |
|
13 | 18 | CLIENT_CREDS_ENV_VARS = {
|
@@ -142,3 +147,29 @@ def normalize_scope(scope):
|
142 | 147 | return " ".join(sorted(scopes))
|
143 | 148 | else:
|
144 | 149 | 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