"""Interface for the graphql-router service.""" from typing import Any from gql import Client, gql as gql_query from gql.transport.requests import RequestsHTTPTransport from notifications.config import DEV_ENVIRONMENT, ENVIRONMENT, QA_ENVIRONMENT, SERVICE_NAME from notifications.constants import context def get_participant_by_chartmetric_id(chartmetric_id: int) -> tuple[str | None, str | None]: """Translate chartmetric ID to participant metdata. Args: chartmetric_id (int): unique identifier from chartmetric Returns: tuple: - (str) uuid of global participant - (str) participant name """ params = {'term': chartmetric_id} query_string = """ query globalParticipantByChartmetricId($term: Int!) { globalParticipantByChartmetricId(chartmetricId: $term) { id, name } } """ response = _make_request(_strip_query(query_string), params, context.NOTIFICATION_USER_HEADERS) data = response['globalParticipantByChartmetricId'] participant_id = None participant_name = None if data: participant_id = data[0]['id'] participant_name = data[0]['name'] return (participant_id, participant_name) def get_sound_recording_details(isrc: str) -> tuple[str | None, str | None, list[str]]: """Get additional details about sound recording. Args: isrc (str): sound recording identifier Returns: tuple: - (str) uuid of sound recording - (list) uuids of global participants """ params = {'term': isrc} query_string = """ query globalSoundRecordingByISRC($term: String!) { globalSoundRecordingByISRC(isrc: $term) { id, name, globalParticipants {id} } } """ response = _make_request(_strip_query(query_string), params, context.NOTIFICATION_USER_HEADERS) data = response['globalSoundRecordingByISRC'] sound_recording_id = None sound_recording_name = None participant_ids = [] if data: sound_recording_id = data['id'] sound_recording_name = data['name'] participant_ids = [x['id'] for x in data['globalParticipants']] return (sound_recording_id, sound_recording_name, participant_ids) def _strip_query(query_string: str) -> str: return ' '.join(query_string.split()).replace('\n', '') def _make_request( query_string: str, params: dict[str, Any], headers: dict[str, Any] | None = None ) -> dict[str, Any]: """Format and send request to graphql-gateway. 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-gateway """ headers = headers or {} env = ENVIRONMENT if ENVIRONMENT != DEV_ENVIRONMENT else QA_ENVIRONMENT url = f'https://{env}-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) def nr_delivery_order_by_id(order_id: str) -> dict[str, Any]: """Get Nr (performance) delivery order details. Args: order_id (str): Nr performance Delivery Order Id. Returns: dict: response directly from graphql-gateway """ params = {'id': order_id} query_string = """ query nrDeliveryOrderById($id: ID!) { nrDeliveryOrderById(id: $id) { id status outputFile numberOfJobs cmo { id name } createdBy { id name email } } } """ response = _make_request( _strip_query(query_string), params, context.DISTRIBUTION_SUITE_BACKEND_USER_HEADERS ) if response and response['nrDeliveryOrderById']: return response['nrDeliveryOrderById'] raise Exception(f'No NR Delivery Order found for id: {order_id}') def nr_ownership_delivery_order_by_id(order_id: str) -> dict[str, Any]: """Get Nr ownership delivery order details. Args: order_id (str): Nr Ownership Delivery Order Id. Returns: dict: response directly from graphql-gateway """ params = {'id': order_id} query_string = """ query ownershipNrDeliveryOrderById($id: ID!) { ownershipNrDeliveryOrderById(id: $id) { id status createdBy { id name email } summary { outputFileCount } } } """ response = _make_request( _strip_query(query_string), params, context.DISTRIBUTION_SUITE_BACKEND_USER_HEADERS ) if response and response['ownershipNrDeliveryOrderById']: return response['ownershipNrDeliveryOrderById'] raise Exception(f'No NR Ownership Delivery Order found for id: {order_id}') def phys_delivery_order_by_id(order_id: str) -> dict[str, Any]: """Get Physical delivery order details. Args: order_id (str): Physical Delivery Order Id. Returns: dict: response directly from graphql-gateway """ params = {'id': order_id} query_string = """ query physDeliveryOrderById($id: ID!) { physicalDeliveryOrder(orderId: $id) { id createdBy { id name email } } } """ response = _make_request( _strip_query(query_string), params, context.DISTRIBUTION_SUITE_BACKEND_USER_HEADERS ) if response and response['physicalDeliveryOrder']: return response['physicalDeliveryOrder'] raise Exception(f'No Physical Delivery Order found for id: {order_id}')