from aiohttp import web from aiohttp_apispec import docs, querystring_schema from server.client import services from server.constants import BASE_API_PREFIX, DSP from server.constants.distributors import DISTRIBUTORS from server.scenarios.distributors import get_distributors from server.schemas.albums.spotify import SpotifyAlbumWithTracks from server.utils.response import dump_response_schema router = web.RouteTableDef() @router.get(BASE_API_PREFIX + "/albums/spotify/{album_id}/") @docs( tags=["albums", "spotify"], summary="Get album with tracks and check is_sony", ) @querystring_schema(SpotifyAlbumWithTracks.Request) @dump_response_schema(SpotifyAlbumWithTracks.Response, apply=False) async def get(request: web.Request) -> web.Response: params = request["querystring"] album_id, market, distributors = request.match_info["album_id"], params.get("market"), params.get("distributors") result = await services.vendor.get_album(DSP.SPOTIFY, album_id=album_id, market=market, with_tracks=True) upc = result.get("external_ids", {}).get("upc") if upc and distributors: distributed_data, _ = await get_distributors(country_code=market, upc=[upc], distributors=distributors) distributed_by = distributed_data[0].get("distributed_by") if distributed_data else None result.update( { "distributed_by": distributed_by, "is_sony": distributed_by == DISTRIBUTORS.SME.value, } ) return result