Skip to content

Commit 01393c8

Browse files
authored
Add support for fetching an artist (#314)
1 parent 650bae8 commit 01393c8

File tree

4 files changed

+74
-1
lines changed

4 files changed

+74
-1
lines changed

src/spotifyaio/spotify.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from spotifyaio.exceptions import SpotifyConnectionError, SpotifyError
1515
from spotifyaio.models import (
1616
Album,
17+
Artist,
1718
ArtistResponse,
1819
BasePlaylist,
1920
BaseUserProfile,
@@ -45,7 +46,7 @@
4546
)
4647

4748
if TYPE_CHECKING:
48-
from spotifyaio import Artist, SimplifiedAlbum, Track
49+
from spotifyaio import SimplifiedAlbum, Track
4950

5051

5152
@dataclass
@@ -148,6 +149,12 @@ async def get_album(self, album_id: str) -> Album:
148149
response = await self._get(f"v1/albums/{album_id}")
149150
return Album.from_json(response)
150151

152+
async def get_artist(self, artist_id: str) -> Artist:
153+
"""Get artist."""
154+
identifier = artist_id.split(":")[-1]
155+
response = await self._get(f"v1/artists/{identifier}")
156+
return Artist.from_json(response)
157+
151158
async def get_playback(self) -> PlaybackState | None:
152159
"""Get playback state."""
153160
response = await self._get(

tests/__snapshots__/test_spotify.ambr

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,13 @@
366366
'uri': 'spotify:album:3IqzqH6ShrRtie9Yd2ODyG',
367367
})
368368
# ---
369+
# name: test_get_artist
370+
dict({
371+
'artist_id': '0TnOYISbd1XYRBk9myaseg',
372+
'name': 'Pitbull',
373+
'uri': 'spotify:artist:0TnOYISbd1XYRBk9myaseg',
374+
})
375+
# ---
369376
# name: test_get_category
370377
dict({
371378
'category_id': '0JQ5DAqbMKFRY5ok2pxXJ0',

tests/fixtures/artist.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"external_urls": {
3+
"spotify": "https://open.spotify.com/artist/0TnOYISbd1XYRBk9myaseg"
4+
},
5+
"followers": {
6+
"href": null,
7+
"total": 10817055
8+
},
9+
"genres": [
10+
"dance pop",
11+
"miami hip hop",
12+
"pop"
13+
],
14+
"href": "https://api.spotify.com/v1/artists/0TnOYISbd1XYRBk9myaseg?locale=en-US%2Cen%3Bq%3D0.5",
15+
"id": "0TnOYISbd1XYRBk9myaseg",
16+
"images": [
17+
{
18+
"url": "https://i.scdn.co/image/ab6761610000e5ebee07b5820dd91d15d397e29c",
19+
"height": 640,
20+
"width": 640
21+
},
22+
{
23+
"url": "https://i.scdn.co/image/ab67616100005174ee07b5820dd91d15d397e29c",
24+
"height": 320,
25+
"width": 320
26+
},
27+
{
28+
"url": "https://i.scdn.co/image/ab6761610000f178ee07b5820dd91d15d397e29c",
29+
"height": 160,
30+
"width": 160
31+
}
32+
],
33+
"name": "Pitbull",
34+
"popularity": 85,
35+
"type": "artist",
36+
"uri": "spotify:artist:0TnOYISbd1XYRBk9myaseg"
37+
}

tests/test_spotify.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,3 +1026,25 @@ async def test_get_category(
10261026
params=None,
10271027
data=None,
10281028
)
1029+
1030+
1031+
async def test_get_artist(
1032+
responses: aioresponses,
1033+
snapshot: SnapshotAssertion,
1034+
authenticated_client: SpotifyClient,
1035+
) -> None:
1036+
"""Test retrieving an artist."""
1037+
responses.get(
1038+
f"{SPOTIFY_URL}/v1/artists/0TnOYISbd1XYRBk9myaseg",
1039+
status=200,
1040+
body=load_fixture("artist.json"),
1041+
)
1042+
response = await authenticated_client.get_artist("0TnOYISbd1XYRBk9myaseg")
1043+
assert response == snapshot
1044+
responses.assert_called_once_with(
1045+
f"{SPOTIFY_URL}/v1/artists/0TnOYISbd1XYRBk9myaseg",
1046+
METH_GET,
1047+
headers=HEADERS,
1048+
params=None,
1049+
data=None,
1050+
)

0 commit comments

Comments
 (0)