Skip to content

Commit 9158913

Browse files
authored
Check saved album (#353)
1 parent fc5e056 commit 9158913

File tree

4 files changed

+74
-1
lines changed

4 files changed

+74
-1
lines changed

src/spotifyaio/spotify.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
from aiohttp import ClientSession
1111
from aiohttp.hdrs import METH_DELETE, METH_GET, METH_POST, METH_PUT
12+
import orjson
1213
from yarl import URL
1314

1415
from spotifyaio.exceptions import SpotifyConnectionError
@@ -209,7 +210,18 @@ async def remove_saved_albums(self, album_ids: list[str]) -> None:
209210
}
210211
await self._delete("v1/me/albums", params=params)
211212

212-
# Check if one or more albums is already saved
213+
async def are_albums_saved(self, album_ids: list[str]) -> dict[str, bool]:
214+
"""Check if albums are saved."""
215+
if not album_ids:
216+
return {}
217+
if len(album_ids) > 20:
218+
msg = "Maximum of 20 albums can be checked at once"
219+
raise ValueError(msg)
220+
identifiers = [get_identifier(i) for i in album_ids]
221+
params: dict[str, Any] = {"ids": ",".join(identifiers)}
222+
response = await self._get("v1/me/albums/contains", params=params)
223+
body: list[bool] = orjson.loads(response) # pylint: disable=no-member
224+
return dict(zip(identifiers, body))
213225

214226
async def get_new_releases(self) -> list[SimplifiedAlbum]:
215227
"""Get new releases."""

tests/__snapshots__/test_spotify.ambr

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
# serializer version: 1
2+
# name: test_checking_saved_albums
3+
dict({
4+
'1A2GTWGtFfWp7KSQTwWOyo': False,
5+
'2noRn2Aes5aoNVsU6iWTh': True,
6+
'3IqzqH6ShrRtie9Yd2ODyG': True,
7+
})
8+
# ---
29
# name: test_get_album
310
dict({
411
'album_id': '3IqzqH6ShrRtie9Yd2ODyG',

tests/fixtures/album_saved.json

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

tests/test_spotify.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,55 @@ async def test_removing_too_many_saved_albums(
260260
responses.assert_not_called() # type: ignore[no-untyped-call]
261261

262262

263+
async def test_checking_saved_albums(
264+
responses: aioresponses,
265+
snapshot: SnapshotAssertion,
266+
authenticated_client: SpotifyClient,
267+
) -> None:
268+
"""Test checking saved albums."""
269+
responses.get(
270+
f"{SPOTIFY_URL}/v1/me/albums/contains?ids=3IqzqH6ShrRtie9Yd2ODyG%252C1A2GTWGtFfWp7KSQTwWOyo%252C2noRn2Aes5aoNVsU6iWTh",
271+
status=200,
272+
body=load_fixture("album_saved.json"),
273+
)
274+
response = await authenticated_client.are_albums_saved(
275+
[
276+
"spotify:album:3IqzqH6ShrRtie9Yd2ODyG",
277+
"1A2GTWGtFfWp7KSQTwWOyo",
278+
"2noRn2Aes5aoNVsU6iWTh",
279+
]
280+
)
281+
assert response == snapshot
282+
responses.assert_called_once_with(
283+
f"{SPOTIFY_URL}/v1/me/albums/contains",
284+
METH_GET,
285+
headers=HEADERS,
286+
params={
287+
"ids": "3IqzqH6ShrRtie9Yd2ODyG,1A2GTWGtFfWp7KSQTwWOyo,2noRn2Aes5aoNVsU6iWTh"
288+
},
289+
json=None,
290+
)
291+
292+
293+
async def test_checking_no_saved_albums(
294+
responses: aioresponses,
295+
authenticated_client: SpotifyClient,
296+
) -> None:
297+
"""Test checking no saved albums."""
298+
await authenticated_client.are_albums_saved([])
299+
responses.assert_not_called() # type: ignore[no-untyped-call]
300+
301+
302+
async def test_checking_too_many_saved_albums(
303+
responses: aioresponses,
304+
authenticated_client: SpotifyClient,
305+
) -> None:
306+
"""Test checking too many saved albums."""
307+
with pytest.raises(ValueError, match="Maximum of 20 albums can be checked at once"):
308+
await authenticated_client.are_albums_saved(["abc"] * 21)
309+
responses.assert_not_called() # type: ignore[no-untyped-call]
310+
311+
263312
@pytest.mark.parametrize(
264313
"playback_fixture",
265314
[

0 commit comments

Comments
 (0)