from aiohttp import web from aiohttp_apispec import docs, json_schema, querystring_schema from server.apple import deserializers from server.apple.constants import SongIdType @docs(tags=["apple"], summary="Fetch a song by using its identifier.") @querystring_schema(deserializers.TrackDetailedDeserializer) async def track_details_view(request: web.Request) -> web.Response: api = request.app["apple_api"] data = request["querystring"] track_id = request.match_info.get("track_id") market = request.match_info.get("storefront") or data["market"] result = await api.song( track_id, storefront=market, include=data.get("include"), localization=data.get("localization") ) return web.json_response(result) @docs(tags=["apple"], summary="Fetch one or more songs by using their IDs or ISRCs.") @querystring_schema(deserializers.TracksDeserializer) async def tracks_view(request: web.Request) -> web.Response: api = request.app["apple_api"] data = request["querystring"] market = request.match_info.get("storefront") or data["market"] ids, isrc_list = data.get("ids"), data.get("isrc_list") or data.get("isrc_filter") id_type = SongIdType.ID if ids else SongIdType.ISRC result = await api.songs( ids or isrc_list, id_type, market, include=data.get("include"), localization=data.get("localization") ) return web.json_response(result) @docs(tags=["apple"], summary="Fetch one or more song's images by using their IDs or ISRCs.") @json_schema(deserializers.TracksImagesDeserializer) async def tracks_images_view(request: web.Request) -> web.Response: api = request.app["apple_api"] data = request["json"] ids, isrc_list = data.get("ids"), data.get("isrc_list") or data.get("isrc_filter") id_type = SongIdType.ID if ids else SongIdType.ISRC result = await api.songs_images(ids or isrc_list, id_type, data["market"], image_size=data.get("image_size")) return web.json_response(result) @docs(tags=["apple"], summary="Fetch one or more songs by using their ISRC.") @querystring_schema(deserializers.TrackISRCDeserializer) async def tracks_by_isrc_view(request: web.Request) -> web.Response: api = request.app["apple_api"] data = request["querystring"] result = await api.songs_by_isrc(data["isrc_list"], storefront=data["market"]) return web.json_response(result) @docs(tags=["apple"], summary="Fetch a playlist by using its identifier.") @querystring_schema(deserializers.PlaylistDeserializer) async def playlist_details_view(request: web.Request) -> web.Response: api = request.app["apple_api"] data = request["querystring"] playlist_id = request.match_info.get("playlist_id") market = request.match_info.get("storefront") or data["market"] result = await api.playlist( playlist_id, market, include=data.get("include"), localization=data.get("localization"), include_songs=data.get("include_songs"), include_music_videos=data.get("include_music_videos"), include_library_playlists=data.get("include_library_playlists"), ) return web.json_response(result) @docs(tags=["apple"], summary="Fetch one or more playlists by using their identifiers.") @querystring_schema(deserializers.PlaylistsDeserializer) async def playlists_view(request: web.Request) -> web.Response: api = request.app["apple_api"] data = request["querystring"] market = request.match_info.get("storefront") or data["market"] result = await api.playlists( data["ids"], market, include=data.get("include"), localization=data.get("localization") ) return web.json_response(result) @docs(tags=["apple"], summary="Fetch one or more playlist's images by using their identifiers.") @json_schema(deserializers.PlaylistsImagesDeserializer) async def playlists_images_view(request: web.Request) -> web.Response: api = request.app["apple_api"] data = request["json"] result = await api.playlists_images(data["ids"], data["market"], image_size=data.get("image_size")) return web.json_response(result) @docs(tags=["apple"], summary="Fetch an album by using its identifier.") @querystring_schema(deserializers.AlbumDeserializer) async def album_details_view(request: web.Request) -> web.Response: album_id = request.match_info.get("album_id") api = request.app["apple_api"] data = request["querystring"] market = request.match_info.get("storefront") or data["market"] result = await api.album( album_id, storefront=market, include=data.get("include"), localization=data.get("localization") ) return web.json_response(result) @docs(tags=["apple"], summary="Fetch one or more albums by using their identifiers.") @querystring_schema(deserializers.AlbumsDeserializer) async def albums_view(request: web.Request) -> web.Response: api = request.app["apple_api"] data = request["querystring"] market = request.match_info.get("storefront") or data["market"] result = await api.albums( data["ids"], storefront=market, include=data.get("include"), localization=data.get("localization") ) return web.json_response(result) @docs(tags=["apple"], summary="Search the catalog by using a query.") @querystring_schema(deserializers.SearchDeserializer) async def search_view(request: web.Request) -> web.Response: api = request.app["apple_api"] data = request["querystring"] market = request.match_info.get("storefront") or data["market"] result = await api.search( data.get("term") or data["query"], market, data.get("types") or data["item_type"], data.get("localization"), data["offset"], data["limit"], ) return web.json_response(result) @docs(tags=["apple"], summary="Fetch a station by its identifier.") @querystring_schema(deserializers.StationDeserializer) async def station_details_view(request: web.Request) -> web.Response: station_id = request.match_info.get("station_id") api = request.app["apple_api"] data = request["querystring"] market = request.match_info.get("storefront") or data["market"] result = await api.station( station_id, storefront=market, include=data.get("include"), localization=data.get("localization") ) return web.json_response(result) @docs(tags=["apple"], summary="Fetch one or more stations by their identifiers.") @querystring_schema(deserializers.StationsDeserializer) async def stations_view(request: web.Request) -> web.Response: api = request.app["apple_api"] data = request["querystring"] market = request.match_info.get("storefront") or data["market"] result = await api.stations( data["ids"], storefront=market, include=data.get("include"), localization=data.get("localization") ) return web.json_response(result) @docs(tags=["apple"], summary="Fetch one or more stations by their identifiers.") @json_schema(deserializers.StationsDeserializer) async def stations_post_view(request: web.Request) -> web.Response: api = request.app["apple_api"] data = request["json"] market = request.match_info.get("storefront") or data["market"] result = await api.stations( data["ids"], storefront=market, include=data.get("include"), localization=data.get("localization") ) return web.json_response(result) @docs(tags=["apple"], summary="Fetch supported storefronts.") @querystring_schema(deserializers.StorefrontsDeserializer) async def storefronts_view(request: web.Request) -> web.Response: api = request.app["apple_api"] data = request["querystring"] result = await api.storefronts( extend=data.get("extend"), include=data.get("include"), localization=data.get("localization") ) return web.json_response({"data": list(result.values())})