"""GraphqlRouter connections.""" from gql.transport.exceptions import TransportServerError from gql.transport.exceptions import TransportQueryError from ..common.exceptions import exceptions from ..common.connectors import graphql_gateway import config TRACKS_QUERY = """ query Track( $isrc: String!) { isrc(isrc: $isrc) { tracks { tracks { trackName labelSoundRecording { label { name } } product { subgenre { genre { name } } } participations { participant { name uuid } participated_as } } } } } """ def execute_query(isrc): """Execute gql tracks query. Args: isrc (str): ISRC for the track Returns: list: dicts of tracks """ headers = { 'apollographql-client-name': config.APPLICATION_NAME, 'apollographql-client-version': '1', 'Orchard-Identity-Id': config.ADMIN_IDENTITY_ID, 'Orchard-Profile-Id': config.ADMIN_PROFILE_ID, 'Orchard-Profile-Type': config.ADMIN_PROFILE_TYPE, 'Cache-Control': 'no-cache' } params = {'isrc': isrc} return graphql_gateway.make_request(headers, TRACKS_QUERY, params) def get_track(isrc): """Get metadata about the first track found by its ISRC. Args: isrc (str): ISRC for the track Returns: track: track data """ try: data = execute_query(isrc) except TransportServerError as e: if e.code in (504, 503, 502): message_error = f'Unexpected response code from GraphQL Gateway HTTP:{e.code}' # noqa:E501 raise exceptions.RetryableException(message_error) else: raise e except TransportQueryError as e: raise exceptions.RetryableException(str(e)) tracks = data.get('isrc', {}).get('tracks', {}).get('tracks', None) if tracks and len(tracks) > 0: return tracks[0] return None