Skip to content

Commit 564fabc

Browse files
authored
Add method to shows (#491)
1 parent 0de8cb7 commit 564fabc

File tree

4 files changed

+165
-3
lines changed

4 files changed

+165
-3
lines changed

src/spotifyaio/spotify.py

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -746,11 +746,42 @@ async def get_saved_shows(self) -> list[SavedShow]:
746746
response = await self._get("v1/me/shows", params=params)
747747
return SavedShowResponse.from_json(response).items
748748

749-
# Save a show
749+
async def save_shows(self, show_ids: list[str]) -> None:
750+
"""Save shows."""
751+
if not show_ids:
752+
return
753+
if len(show_ids) > 50:
754+
msg = "Maximum of 50 shows can be saved at once"
755+
raise ValueError(msg)
756+
params: dict[str, Any] = {
757+
"ids": ",".join([get_identifier(i) for i in show_ids])
758+
}
759+
await self._put("v1/me/shows", params=params)
750760

751-
# Remove a show
761+
async def remove_saved_shows(self, show_ids: list[str]) -> None:
762+
"""Remove saved shows."""
763+
if not show_ids:
764+
return
765+
if len(show_ids) > 50:
766+
msg = "Maximum of 50 shows can be removed at once"
767+
raise ValueError(msg)
768+
params: dict[str, Any] = {
769+
"ids": ",".join([get_identifier(i) for i in show_ids])
770+
}
771+
await self._delete("v1/me/shows", params=params)
752772

753-
# Check if one or more shows is already saved
773+
async def are_shows_saved(self, show_ids: list[str]) -> dict[str, bool]:
774+
"""Check if shows are saved."""
775+
if not show_ids:
776+
return {}
777+
if len(show_ids) > 50:
778+
msg = "Maximum of 50 shows can be checked at once"
779+
raise ValueError(msg)
780+
identifiers = [get_identifier(i) for i in show_ids]
781+
params: dict[str, Any] = {"ids": ",".join(identifiers)}
782+
response = await self._get("v1/me/shows/contains", params=params)
783+
body: list[bool] = orjson.loads(response) # pylint: disable=no-member
784+
return dict(zip(identifiers, body))
754785

755786
# Get a track
756787

tests/__snapshots__/test_spotify.ambr

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
'3o0RYoo5iOMKSmEbunsbvX': False,
1313
})
1414
# ---
15+
# name: test_check_saved_shows
16+
dict({
17+
'18yVqkdbdRvS24c0Ilj2ci': True,
18+
'1HGw3J3NxZO1TP1BTtVhpZ': False,
19+
})
20+
# ---
1521
# name: test_checking_saved_albums
1622
dict({
1723
'1A2GTWGtFfWp7KSQTwWOyo': False,

tests/fixtures/shows_saved.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[
2+
true,
3+
false
4+
]

tests/test_spotify.py

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2245,3 +2245,124 @@ async def test_get_audio_features(
22452245
params=None,
22462246
json=None,
22472247
)
2248+
2249+
2250+
async def test_save_shows(
2251+
responses: aioresponses,
2252+
authenticated_client: SpotifyClient,
2253+
) -> None:
2254+
"""Test saving shows."""
2255+
responses.put(
2256+
f"{SPOTIFY_URL}/v1/me/shows?ids=0TnOYISbd1XYRBk9myaseg",
2257+
status=200,
2258+
body="",
2259+
)
2260+
await authenticated_client.save_shows(["0TnOYISbd1XYRBk9myaseg"])
2261+
responses.assert_called_once_with(
2262+
f"{SPOTIFY_URL}/v1/me/shows",
2263+
METH_PUT,
2264+
headers=HEADERS,
2265+
params={"ids": "0TnOYISbd1XYRBk9myaseg"},
2266+
json=None,
2267+
)
2268+
2269+
2270+
async def test_save_no_shows(
2271+
responses: aioresponses,
2272+
authenticated_client: SpotifyClient,
2273+
) -> None:
2274+
"""Test saving no shows."""
2275+
await authenticated_client.save_shows([])
2276+
responses.assert_not_called() # type: ignore[no-untyped-call]
2277+
2278+
2279+
async def test_save_too_many_shows(
2280+
responses: aioresponses,
2281+
authenticated_client: SpotifyClient,
2282+
) -> None:
2283+
"""Test saving too many shows."""
2284+
with pytest.raises(ValueError, match="Maximum of 50 shows can be saved at once"):
2285+
await authenticated_client.save_shows(["abc"] * 51)
2286+
responses.assert_not_called() # type: ignore[no-untyped-call]
2287+
2288+
2289+
async def test_remove_shows(
2290+
responses: aioresponses,
2291+
authenticated_client: SpotifyClient,
2292+
) -> None:
2293+
"""Test removing shows."""
2294+
responses.delete(
2295+
f"{SPOTIFY_URL}/v1/me/shows?ids=0TnOYISbd1XYRBk9myaseg",
2296+
status=200,
2297+
body="",
2298+
)
2299+
await authenticated_client.remove_saved_shows(["0TnOYISbd1XYRBk9myaseg"])
2300+
responses.assert_called_once_with(
2301+
f"{SPOTIFY_URL}/v1/me/shows",
2302+
METH_DELETE,
2303+
headers=HEADERS,
2304+
params={"ids": "0TnOYISbd1XYRBk9myaseg"},
2305+
json=None,
2306+
)
2307+
2308+
2309+
async def test_remove_no_shows(
2310+
responses: aioresponses,
2311+
authenticated_client: SpotifyClient,
2312+
) -> None:
2313+
"""Test removing no shows."""
2314+
await authenticated_client.remove_saved_shows([])
2315+
responses.assert_not_called() # type: ignore[no-untyped-call]
2316+
2317+
2318+
async def test_remove_too_many_shows(
2319+
responses: aioresponses,
2320+
authenticated_client: SpotifyClient,
2321+
) -> None:
2322+
"""Test removing too many shows."""
2323+
with pytest.raises(ValueError, match="Maximum of 50 shows can be removed at once"):
2324+
await authenticated_client.remove_saved_shows(["abc"] * 51)
2325+
responses.assert_not_called() # type: ignore[no-untyped-call]
2326+
2327+
2328+
async def test_check_saved_shows(
2329+
responses: aioresponses,
2330+
snapshot: SnapshotAssertion,
2331+
authenticated_client: SpotifyClient,
2332+
) -> None:
2333+
"""Test checking saved shows."""
2334+
responses.get(
2335+
f"{SPOTIFY_URL}/v1/me/shows/contains?ids=18yVqkdbdRvS24c0Ilj2ci%2C1HGw3J3NxZO1TP1BTtVhpZ",
2336+
status=200,
2337+
body=load_fixture("shows_saved.json"),
2338+
)
2339+
response = await authenticated_client.are_shows_saved(
2340+
["18yVqkdbdRvS24c0Ilj2ci", "1HGw3J3NxZO1TP1BTtVhpZ"]
2341+
)
2342+
assert response == snapshot
2343+
responses.assert_called_once_with(
2344+
f"{SPOTIFY_URL}/v1/me/shows/contains",
2345+
METH_GET,
2346+
headers=HEADERS,
2347+
params={"ids": "18yVqkdbdRvS24c0Ilj2ci,1HGw3J3NxZO1TP1BTtVhpZ"},
2348+
json=None,
2349+
)
2350+
2351+
2352+
async def test_check_no_saved_shows(
2353+
responses: aioresponses,
2354+
authenticated_client: SpotifyClient,
2355+
) -> None:
2356+
"""Test checking no saved shows."""
2357+
assert await authenticated_client.are_shows_saved([]) == {}
2358+
responses.assert_not_called() # type: ignore[no-untyped-call]
2359+
2360+
2361+
async def test_check_too_many_saved_shows(
2362+
responses: aioresponses,
2363+
authenticated_client: SpotifyClient,
2364+
) -> None:
2365+
"""Test checking too many saved shows."""
2366+
with pytest.raises(ValueError, match="Maximum of 50 shows can be checked at once"):
2367+
await authenticated_client.are_shows_saved(["abc"] * 51)
2368+
responses.assert_not_called() # type: ignore[no-untyped-call]

0 commit comments

Comments
 (0)