Skip to content

Commit 2a829e9

Browse files
authored
Add method to remove save audiobooks (#479)
1 parent 0a4490a commit 2a829e9

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

src/spotifyaio/spotify.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,17 @@ async def save_audiobooks(self, audiobook_ids: list[str]) -> None:
318318
}
319319
await self._put("v1/me/audiobooks", params=params)
320320

321-
# Remove an audiobook
321+
async def remove_saved_audiobooks(self, audiobook_ids: list[str]) -> None:
322+
"""Remove saved audiobooks."""
323+
if not audiobook_ids:
324+
return
325+
if len(audiobook_ids) > 50:
326+
msg = "Maximum of 50 audiobooks can be removed at once"
327+
raise ValueError(msg)
328+
params: dict[str, Any] = {
329+
"ids": ",".join([get_identifier(i) for i in audiobook_ids])
330+
}
331+
await self._delete("v1/me/audiobooks", params=params)
322332

323333
# Check if one or more audiobooks is already saved
324334

tests/test_spotify.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,6 +1499,47 @@ async def test_save_too_many_audiobooks(
14991499
responses.assert_not_called() # type: ignore[no-untyped-call]
15001500

15011501

1502+
async def test_remove_audiobooks(
1503+
responses: aioresponses,
1504+
authenticated_client: SpotifyClient,
1505+
) -> None:
1506+
"""Test removing an audiobook."""
1507+
responses.delete(
1508+
f"{SPOTIFY_URL}/v1/me/audiobooks?ids=0TnOYISbd1XYRBk9myaseg",
1509+
status=200,
1510+
body="",
1511+
)
1512+
await authenticated_client.remove_saved_audiobooks(["0TnOYISbd1XYRBk9myaseg"])
1513+
responses.assert_called_once_with(
1514+
f"{SPOTIFY_URL}/v1/me/audiobooks",
1515+
METH_DELETE,
1516+
headers=HEADERS,
1517+
params={"ids": "0TnOYISbd1XYRBk9myaseg"},
1518+
json=None,
1519+
)
1520+
1521+
1522+
async def test_remove_no_audiobooks(
1523+
responses: aioresponses,
1524+
authenticated_client: SpotifyClient,
1525+
) -> None:
1526+
"""Test removing no audiobooks."""
1527+
await authenticated_client.remove_saved_audiobooks([])
1528+
responses.assert_not_called() # type: ignore[no-untyped-call]
1529+
1530+
1531+
async def test_remove_too_many_audiobooks(
1532+
responses: aioresponses,
1533+
authenticated_client: SpotifyClient,
1534+
) -> None:
1535+
"""Test removing too many audiobooks."""
1536+
with pytest.raises(
1537+
ValueError, match="Maximum of 50 audiobooks can be removed at once"
1538+
):
1539+
await authenticated_client.remove_saved_audiobooks(["abc"] * 51)
1540+
responses.assert_not_called() # type: ignore[no-untyped-call]
1541+
1542+
15021543
async def test_get_show_episodes(
15031544
responses: aioresponses,
15041545
snapshot: SnapshotAssertion,

0 commit comments

Comments
 (0)