Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method to save audiobooks #478

Merged
merged 1 commit into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/spotifyaio/spotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,17 @@ async def get_saved_audiobooks(self) -> list[SimplifiedAudiobook]:
response = await self._get("v1/me/audiobooks", params=params)
return SavedAudiobookResponse.from_json(response).items

# Save an audiobook
async def save_audiobooks(self, audiobook_ids: list[str]) -> None:
"""Save audiobooks."""
if not audiobook_ids:
return
if len(audiobook_ids) > 50:
msg = "Maximum of 50 audiobooks can be saved at once"
raise ValueError(msg)
params: dict[str, Any] = {
"ids": ",".join([get_identifier(i) for i in audiobook_ids])
}
await self._put("v1/me/audiobooks", params=params)

# Remove an audiobook

Expand Down
41 changes: 41 additions & 0 deletions tests/test_spotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -1458,6 +1458,47 @@ async def test_get_saved_audiobooks(
)


async def test_save_audiobooks(
responses: aioresponses,
authenticated_client: SpotifyClient,
) -> None:
"""Test saving an audiobook."""
responses.put(
f"{SPOTIFY_URL}/v1/me/audiobooks?ids=0TnOYISbd1XYRBk9myaseg",
status=200,
body="",
)
await authenticated_client.save_audiobooks(["0TnOYISbd1XYRBk9myaseg"])
responses.assert_called_once_with(
f"{SPOTIFY_URL}/v1/me/audiobooks",
METH_PUT,
headers=HEADERS,
params={"ids": "0TnOYISbd1XYRBk9myaseg"},
json=None,
)


async def test_save_no_audiobooks(
responses: aioresponses,
authenticated_client: SpotifyClient,
) -> None:
"""Test saving no audiobooks."""
await authenticated_client.save_audiobooks([])
responses.assert_not_called() # type: ignore[no-untyped-call]


async def test_save_too_many_audiobooks(
responses: aioresponses,
authenticated_client: SpotifyClient,
) -> None:
"""Test saving too many audiobooks."""
with pytest.raises(
ValueError, match="Maximum of 50 audiobooks can be saved at once"
):
await authenticated_client.save_audiobooks(["abc"] * 51)
responses.assert_not_called() # type: ignore[no-untyped-call]


async def test_get_show_episodes(
responses: aioresponses,
snapshot: SnapshotAssertion,
Expand Down
Loading