"""Interface for the graphql-router service.""" from gql import Client from gql import gql as gql_query from gql.transport.requests import RequestsHTTPTransport from config import ENVIRONMENT from config import APPLICATION_NAME as SERVICE_NAME def profile_can_access_track(profile_id, profile_type, identity_id, isrc, track_id): """Determine if profile can access track on sound recording. Args: profile_id (int): profile identifier profile_type (str): profile scope identity_id (str): identity UUID isrc (str): sound recording identifier track_id (int): internal track identifier Returns: bool: profile can access track from sound recording """ return str(track_id) in get_available_tracks(profile_id, profile_type, identity_id, isrc) # noqa:E501 def profile_can_access_sound_recording(profile_id, profile_type, identity_id, isrc): """Determine if profile can access sound recording. Args: profile_id (int): profile identifier profile_type (str): profile scope identity_id (str): identity UUID isrc (str): sound recording identifier Returns: bool: profile can access sound recording """ return len(get_available_sound_recordings(profile_id, profile_type, identity_id, isrc)) > 0 # noqa:E501 def get_available_sound_recordings(profile_id, profile_type, identity_id, isrc): """Get sound recordings profile has access to. Args: profile_id (int): profile identifier profile_type (str): profile scope identity_id (str): identity UUID isrc (str): sound recording identifier Returns: list: sound recording IDs """ headers = { 'Orchard-Profile-Id': str(profile_id), 'Orchard-Profile-Type': profile_type, 'Orchard-Identity-Id': identity_id, } params = {'term': isrc} query_string = """ query globalSoundRecordingByIsrc($term: String!) { globalSoundRecordingByIsrc(isrc: $term) { labelSoundRecordings { id } } } """ response = _make_request(_strip_query(query_string), params, headers) results = [] result = response['globalSoundRecordingByIsrc'] results += [x['id'] for x in result.get('labelSoundRecordings', [])] return results def get_available_tracks(profile_id, profile_type, identity_id, isrc): """Get tracks on sound recording profile has access to. Args: profile_id (int): profile identifier profile_type (str): profile scope identity_id (str): identity UUID isrc (str): sound recording identifier Returns: dict: resulting list of track IDs """ headers = { 'Orchard-Profile-Id': str(profile_id), 'Orchard-Profile-Type': profile_type, 'Orchard-Identity-Id': identity_id, } params = {'term': isrc} query_string = """ query globalSoundRecordingByIsrc($term: String!) { globalSoundRecordingByIsrc(isrc: $term) { labelSoundRecordings { tracks { tuid } } } } """ response = _make_request(_strip_query(query_string), params, headers) results = [] result = response['globalSoundRecordingByIsrc'] for label_recording in result.get('labelSoundRecordings', []): for track in label_recording.get('tracks', []): results.append(track['tuid']) return results def _strip_query(query_string): return ' '.join(query_string.split()).replace('\n', '') def _make_request(query_string, params, headers={}): """Format and send request to graphql-router. Args: query_string (str): graphql formatted request body params (dict): parameters to inject into body headers (dict): HTTP headers for request Returns: dict: response directly from graphql-router """ url = f'https://{ENVIRONMENT}-graphql-router.theorchard.io/graphql' headers['apollographql-client-name'] = SERVICE_NAME headers['apollographql-client-version'] = '1' transport = RequestsHTTPTransport( url=url, use_json=True, headers=headers, verify=True, retries=3 ) client = Client( transport=transport, fetch_schema_from_transport=False, ) query = gql_query(query_string) return client.execute(query, variable_values=params)