from typing import List, Optional from external_api.playlists.schemas import PlaylistsQuery, PlaylistResults, PlaylistModel from playlists.schemas import PlaylistResponseSchema from external_api.playlists.playlists_fetcher import PlaylistsFetcher, default_playlists_fetcher from utils import list_utils class PlaylistsService: playlists_fetcher: PlaylistsFetcher def __init__(self, playlists_fetcher: PlaylistsFetcher = default_playlists_fetcher()): self.playlists_fetcher = playlists_fetcher async def get_playlists_by_name(self, name: Optional[str], limit: int) -> List[PlaylistResponseSchema]: playlists_by_provider = await self.playlists_fetcher.fetch(PlaylistsQuery(name, limit)) return list_utils.flattern([self.__map_playlists(playlists) for playlists in playlists_by_provider]) def __map_playlists(self, results: PlaylistResults) -> List[PlaylistResponseSchema]: return [self.__map_single_playlist(playlist, results.provider.value) for playlist in results.playlists] def __map_single_playlist(self, playlist: PlaylistModel, provider: str) -> PlaylistResponseSchema: return PlaylistResponseSchema(playlist.id, playlist.name, playlist.owner_name, provider, playlist.images)