Skip to content

Commit 1cab0cc

Browse files
authored
Add support for fetching a category (#312)
1 parent 66ab61a commit 1cab0cc

5 files changed

Lines changed: 64 additions & 0 deletions

File tree

src/spotifyaio/models.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,16 @@ class CategoryPlaylistResponse(DataClassORJSONMixin):
382382
playlists: PlaylistResponse
383383

384384

385+
@dataclass
386+
class Category(DataClassORJSONMixin):
387+
"""Category model."""
388+
389+
category_id: str = field(metadata=field_options(alias="id"))
390+
name: str
391+
href: str
392+
icons: list[Image]
393+
394+
385395
class ProductType(StrEnum):
386396
"""Product type."""
387397

src/spotifyaio/spotify.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
ArtistResponse,
1818
BasePlaylist,
1919
BaseUserProfile,
20+
Category,
2021
CategoryPlaylistResponse,
2122
CurrentPlaying,
2223
Device,
@@ -278,6 +279,11 @@ async def get_featured_playlists(self) -> list[BasePlaylist]:
278279
response = await self._get("v1/browse/featured-playlists", params=params)
279280
return FeaturedPlaylistResponse.from_json(response).playlists.items
280281

282+
async def get_category(self, category_id: str) -> Category:
283+
"""Get category."""
284+
response = await self._get(f"v1/browse/categories/{category_id}")
285+
return Category.from_json(response)
286+
281287
async def get_saved_albums(self) -> list[SavedAlbum]:
282288
"""Get saved albums."""
283289
params: dict[str, Any] = {"limit": 48}

tests/__snapshots__/test_spotify.ambr

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,20 @@
366366
'uri': 'spotify:album:3IqzqH6ShrRtie9Yd2ODyG',
367367
})
368368
# ---
369+
# name: test_get_category
370+
dict({
371+
'category_id': '0JQ5DAqbMKFRY5ok2pxXJ0',
372+
'href': 'https://api.spotify.com/v1/browse/categories/0JQ5DAqbMKFRY5ok2pxXJ0',
373+
'icons': list([
374+
dict({
375+
'height': 274,
376+
'url': 'https://t.scdn.co/media/original/dinner_1b6506abba0ba52c54e6d695c8571078_274x274.jpg',
377+
'width': 274,
378+
}),
379+
]),
380+
'name': 'Cooking & Dining',
381+
})
382+
# ---
369383
# name: test_get_category_playlists
370384
list([
371385
dict({

tests/fixtures/category.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"href": "https://api.spotify.com/v1/browse/categories/0JQ5DAqbMKFRY5ok2pxXJ0",
3+
"id": "0JQ5DAqbMKFRY5ok2pxXJ0",
4+
"icons": [
5+
{
6+
"height": 274,
7+
"url": "https://t.scdn.co/media/original/dinner_1b6506abba0ba52c54e6d695c8571078_274x274.jpg",
8+
"width": 274
9+
}
10+
],
11+
"name": "Cooking & Dining"
12+
}

tests/test_spotify.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -982,3 +982,25 @@ async def test_get_top_tracks(
982982
params={"limit": 48},
983983
data=None,
984984
)
985+
986+
987+
async def test_get_category(
988+
responses: aioresponses,
989+
snapshot: SnapshotAssertion,
990+
authenticated_client: SpotifyClient,
991+
) -> None:
992+
"""Test retrieving a category."""
993+
responses.get(
994+
f"{SPOTIFY_URL}/v1/browse/categories/dinner",
995+
status=200,
996+
body=load_fixture("category.json"),
997+
)
998+
response = await authenticated_client.get_category("dinner")
999+
assert response == snapshot
1000+
responses.assert_called_once_with(
1001+
f"{SPOTIFY_URL}/v1/browse/categories/dinner",
1002+
METH_GET,
1003+
headers=HEADERS,
1004+
params=None,
1005+
data=None,
1006+
)

0 commit comments

Comments
 (0)