def get_multiple_albums(spotify_ids): """Get Spotify catalog information for multiple albums identified by their Spotify IDs. Returns: List of album objects from Spotify. More info about this type of objects is available at https://developer.spotify.com/web-api/object-model/#album-object. """ namespace = "spotify_albums" albums = cache.get_many(spotify_ids, namespace) # Checking which albums weren't in cache for album_id, data in albums.items(): if data is not None and album_id in spotify_ids: spotify_ids.remove(album_id) if len(spotify_ids) > 0: resp = requests.get("%s/albums?ids=%s" % (BASE_URL, ','.join(spotify_ids))).json()['albums'] received_albums = {} for album in resp: if album is not None: received_albums[album['id']] = album cache.set_many(received_albums, namespace=namespace, time=DEFAULT_CACHE_EXPIRATION) albums.update(received_albums) return albums
def get_multiple_albums(spotify_ids: List[str]) -> List[dict]: """Get information about multiple albums. Args: spotify_ids: List of Spotify IDs of albums. Returns: List of album objects from Spotify. More info about this type of objects is available at https://developer.spotify.com/web-api/object-model/#album-object. """ if not spotify_ids: return [] spotify_ids = list(spotify_ids) cache_namespace = "spotify_albums" albums = cache.get_many(spotify_ids, cache_namespace) # Checking which albums weren't in cache for album_id, data in albums.items(): if data is not None and album_id in spotify_ids: spotify_ids.remove(album_id) if spotify_ids: resp = _get(f"albums?ids={','.join(spotify_ids)}") if "albums" not in resp: logging.error("Album data is missing from a Spotify response", extra={ "spotify_ids": spotify_ids, "response": resp, }) raise SpotifyUnexpectedResponseException("Album data is missing") received_albums = {} for album in resp["albums"]: if album is not None: received_albums[album['id']] = album cache.set_many(received_albums, namespace=cache_namespace, time=_DEFAULT_CACHE_EXPIRATION) albums.update(received_albums) return albums
def test_many(self): # With namespace mapping = { "test1": "Hello", "test2": "there", } self.assertTrue(cache.set_many(mapping, namespace="testing-1")) self.assertEqual(cache.get_many(list(mapping.keys()), namespace="testing-1"), mapping) # With another namespace test = cache.get_many(list(mapping.keys()), namespace="testing-2") for key, val in test.items(): self.assertIn(key, mapping) self.assertIsNone(val) # Without a namespace mapping = { "test1": "What's", "test2": "good", } self.assertTrue(cache.set_many(mapping)) self.assertEqual(cache.get_many(list(mapping.keys())), mapping)
def test_many(self): # With namespace mapping = { "test1": "Hello", "test2": "there", } self.assertTrue(cache.set_many(mapping, namespace="testing-1")) self.assertEqual(cache.get_many(list(mapping.keys()), namespace="testing-1"), mapping) # With another namespace test = cache.get_many(list(mapping.keys()), namespace="testing-2") for key, val in test.items(): self.assertIn(key, mapping) self.assertIsNone(val) # Without a namespace mapping = { "test1": "What's", "test2": "good", } self.assertTrue(cache.set_many(mapping)) self.assertEqual(cache.get_many(list(mapping.keys())), mapping)