Skip to content

Commit 91e625b

Browse files
authored
Save albums (#351)
1 parent 23cb926 commit 91e625b

2 files changed

Lines changed: 57 additions & 1 deletion

File tree

src/spotifyaio/spotify.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,17 @@ async def get_saved_albums(self) -> list[SavedAlbum]:
176176
response = await self._get("v1/me/albums", params=params)
177177
return SavedAlbumResponse.from_json(response).items
178178

179-
# Save an album
179+
async def save_albums(self, album_ids: list[str]) -> None:
180+
"""Save albums."""
181+
if not album_ids:
182+
return
183+
if len(album_ids) > 50:
184+
msg = "Maximum of 50 albums can be saved at once"
185+
raise ValueError(msg)
186+
params: dict[str, Any] = {
187+
"ids": ",".join([get_identifier(i) for i in album_ids])
188+
}
189+
await self._put("v1/me/albums", params=params)
180190

181191
# Remove an album
182192

tests/test_spotify.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,52 @@ async def test_get_album_tracks(
168168
)
169169

170170

171+
async def test_save_albums(
172+
responses: aioresponses,
173+
authenticated_client: SpotifyClient,
174+
) -> None:
175+
"""Test saving albums."""
176+
responses.put(
177+
f"{SPOTIFY_URL}/v1/me/albums?ids=3IqzqH6ShrRtie9Yd2ODyG%252C1A2GTWGtFfWp7KSQTwWOyo%252C2noRn2Aes5aoNVsU6iWTh",
178+
status=200,
179+
)
180+
await authenticated_client.save_albums(
181+
[
182+
"spotify:album:3IqzqH6ShrRtie9Yd2ODyG",
183+
"1A2GTWGtFfWp7KSQTwWOyo",
184+
"2noRn2Aes5aoNVsU6iWTh",
185+
]
186+
)
187+
responses.assert_called_once_with(
188+
f"{SPOTIFY_URL}/v1/me/albums",
189+
METH_PUT,
190+
headers=HEADERS,
191+
params={
192+
"ids": "3IqzqH6ShrRtie9Yd2ODyG,1A2GTWGtFfWp7KSQTwWOyo,2noRn2Aes5aoNVsU6iWTh"
193+
},
194+
json=None,
195+
)
196+
197+
198+
async def test_save_no_albums(
199+
responses: aioresponses,
200+
authenticated_client: SpotifyClient,
201+
) -> None:
202+
"""Test saving no albums."""
203+
await authenticated_client.save_albums([])
204+
responses.assert_not_called() # type: ignore[no-untyped-call]
205+
206+
207+
async def test_save_too_many_albums(
208+
responses: aioresponses,
209+
authenticated_client: SpotifyClient,
210+
) -> None:
211+
"""Test saving too many albums."""
212+
with pytest.raises(ValueError, match="Maximum of 50 albums can be saved at once"):
213+
await authenticated_client.save_albums(["abc"] * 51)
214+
responses.assert_not_called() # type: ignore[no-untyped-call]
215+
216+
171217
@pytest.mark.parametrize(
172218
"playback_fixture",
173219
[

0 commit comments

Comments
 (0)