Skip to content

Commit cd1dd7a

Browse files
authored
Raise exception when rate limit exceeded (#425)
1 parent 31a1f64 commit cd1dd7a

File tree

5 files changed

+39
-1
lines changed

5 files changed

+39
-1
lines changed

src/spotifyaio/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
SpotifyConnectionError,
66
SpotifyError,
77
SpotifyNotFoundError,
8+
SpotifyRateLimitError,
89
)
910
from .models import (
1011
Album,
@@ -68,6 +69,7 @@
6869
"SpotifyConnectionError",
6970
"SpotifyError",
7071
"SpotifyNotFoundError",
72+
"SpotifyRateLimitError",
7173
"Track",
7274
"UserProfile",
7375
]

src/spotifyaio/exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,7 @@ class SpotifyAuthenticationFailedError(SpotifyError):
1515

1616
class SpotifyNotFoundError(SpotifyError):
1717
"""Spotify not found exception."""
18+
19+
20+
class SpotifyRateLimitError(SpotifyError):
21+
"""Spotify rate limit exception."""

src/spotifyaio/spotify.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
import orjson
1313
from yarl import URL
1414

15-
from spotifyaio.exceptions import SpotifyConnectionError, SpotifyNotFoundError
15+
from spotifyaio.exceptions import (
16+
SpotifyConnectionError,
17+
SpotifyNotFoundError,
18+
SpotifyRateLimitError,
19+
)
1620
from spotifyaio.models import (
1721
Album,
1822
AlbumsResponse,
@@ -132,6 +136,10 @@ async def _request(
132136
msg = f"Resource not found: {uri}"
133137
raise SpotifyNotFoundError(msg)
134138

139+
if '"status": 429' in text:
140+
msg = "Ratelimit exceeded"
141+
raise SpotifyRateLimitError(msg)
142+
135143
return text
136144

137145
async def _get(self, uri: str, params: dict[str, Any] | None = None) -> str:

tests/fixtures/rate_limit.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"error": {
3+
"status": 429,
4+
"message": "API rate limit exceeded"
5+
}
6+
}

tests/test_spotify.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
SpotifyClient,
1717
SpotifyConnectionError,
1818
SpotifyNotFoundError,
19+
SpotifyRateLimitError,
1920
)
2021

2122
from . import load_fixture
@@ -884,6 +885,23 @@ async def test_get_not_found_playlist(
884885
await authenticated_client.get_playlist("37i9dQZF1DXcBWIGoYBM5M")
885886

886887

888+
async def test_rate_limit(
889+
responses: aioresponses,
890+
authenticated_client: SpotifyClient,
891+
) -> None:
892+
"""Test raising rate limit exception."""
893+
responses.get(
894+
f"{SPOTIFY_URL}/v1/playlists/37i9dQZF1DXcBWIGoYBM5M?additional_types=track,episode",
895+
status=200,
896+
body=load_fixture("rate_limit.json"),
897+
)
898+
with pytest.raises(
899+
SpotifyRateLimitError,
900+
match="Ratelimit exceeded",
901+
):
902+
await authenticated_client.get_playlist("37i9dQZF1DXcBWIGoYBM5M")
903+
904+
887905
@pytest.mark.parametrize(
888906
"fixture",
889907
[

0 commit comments

Comments
 (0)