"""Connector for talking to graphql-product.""" from switchboard_consumer import config from switchboard_consumer.connectors.graphql import GraphQLConnector from switchboard_consumer.constants.graphql import ( create_artist, create_label_participant, create_product, create_project, create_release_correction, create_release_correction_detail, create_tracks, create_video_single_product, delete_release_correction, delete_tracks, filter_artists, get_artist, get_participation_role_categories, get_product, get_product_available_channels, get_project_available_channels, get_project_by_project_code, get_track_by_tuid, rearrange_tracks, set_label_sound_recording_participations, submit_product_with_subaccount_id, submit_product_with_vendor_id, unsubmit_product, update_product, update_product_non_release_correctable_fields, update_project, update_tracks, update_video_single_product ) from switchboard_consumer.formatters.label_participant import ( format_spotify_uri ) class GraphQLProductClient(GraphQLConnector): """Connector for Orchard GQL Product.""" def __init__(self, url, secret, secret_name, credential_fetcher=lambda: {}): """Init connector.""" super().__init__(url, secret, secret_name, credential_fetcher) def create_project(self, data, correlation_id): """Call the Orchard createProject mutation.""" response = self._execute(create_project.query, data, correlation_id) if response.get('errors'): return response return response['data']['createProject'] def update_project(self, data, correlation_id): """Call the Orchard updateProject mutation.""" response = self._execute(update_project.query, data, correlation_id) if response.get('errors'): return response return response['data']['updateProject'] def create_product(self, data, correlation_id): """Call the Orchard createProduct mutation.""" response = self._execute(create_product.query, data, correlation_id) if response.get('errors'): return response return response['data']['createProduct'] def update_product(self, data, correlation_id): """Call the Orchard updateProduct mutation.""" response = self._execute(update_product.query, data, correlation_id) if response.get('errors'): return response return response['data']['updateProduct'] def update_non_release_correctable_product_fields(self, data, correlation_id): """Query to update fields that aren't release correctable but are updatable. """ response = self._execute( update_product_non_release_correctable_fields.query, data, correlation_id) if response.get('errors'): return response return response['data']['updateProduct'] def get_artist(self, data, correlation_id): """Call the Orchard artist query.""" response = self._execute(get_artist.query, data, correlation_id) if response.get('errors'): return response return response['data']['artist'] def create_artist(self, data, correlation_id): """Call the Orchard saveArtists query.""" response = self._execute(create_artist.query, data, correlation_id) if response.get('errors'): return response return response['data']['saveArtists'][0] def create_tracks(self, tracks_data, product_id, correlation_id): """Call the Orchard saveTracks mutation.""" data = { 'productId': product_id, 'tracks': tracks_data } response = self._execute(create_tracks.query, data, correlation_id) if response.get('errors'): return response return response['data']['saveTracks'] def get_product_by_id(self, orchard_product_id, correlation_id): """Call the Orchard product query.""" data = {'productId': orchard_product_id} response = self._execute(get_product.query, data, correlation_id) if response.get('errors'): return response return response['data']['product'] def delete_tracks(self, tracks, product_id, correlation_id): """Call the Orchard saveTracks.delete mutation.""" data = { 'productId': product_id, 'tracks': tracks } response = self._execute(delete_tracks.query, data, correlation_id) if response.get('errors'): return response return response['data']['saveTracks'] def rearrange_tracks(self, arrangement, product_id, correlation_id): """Call the Orchard saveTracks.update mutation.""" data = { 'productId': product_id, 'tracks': arrangement } response = self._execute(rearrange_tracks.query, data, correlation_id) if response.get('errors'): return response return response['data']['saveTracks'] def update_tracks(self, payload, correlation_id): """Call the Orchard saveTracks.update mutation.""" response = self._execute(update_tracks.query, payload, correlation_id) if response.get('errors'): return response return response['data']['saveTracks'] def filter_artists(self, payload, correlation_id): """Call the Orchard filterArtists query.""" response = self._execute(filter_artists.query, payload, correlation_id) if response.get('errors'): return response return response['data']['filterArtists'] def unsubmit_product(self, product_id, correlation_id): """Call the Orchard unsubmitProduct mutation.""" data = { 'productId': product_id } response = self._execute(unsubmit_product.query, data, correlation_id) if response.get('errors'): return response return response['data']['unsubmitProduct'] def create_video_single_product(self, payload, correlation_id): """Call the Orchard saveVideoSingleProduct.create mutation.""" response = self._execute( create_video_single_product.query, {'body': payload}, correlation_id) if response.get('errors'): return response return response['data']['saveVideoSingleProduct'] def update_video_single_product(self, payload, correlation_id): """Call the Orchard saveVideoSingleProduct.update mutation.""" response = self._execute( update_video_single_product.query, {'body': payload}, correlation_id) if response.get('errors'): return response return response['data']['saveVideoSingleProduct'] def create_release_correction(self, product_id, correlation_id): """Call the Orchard createProductCorrection mutation.""" payload = { 'productId': product_id, } response = self._execute( create_release_correction.query, payload, correlation_id) if response.get('errors'): return response return response['data']['createProductCorrection'] def delete_release_correction(self, product_id, release_correction_id, correlation_id): """Call the Orchard deleteProductCorrection mutation.""" payload = { 'productId': product_id, 'releaseCorrectionId': release_correction_id } response = self._execute( delete_release_correction.query, payload, correlation_id) if response.get('errors'): return response return response['data']['deleteProductCorrection'] def create_release_correction_detail(self, product_id, release_correction_id, corrections, correlation_id): """Call the Orchard createProductCorrectionDetail mutation.""" payload = { 'productId': product_id, 'releaseCorrectionId': release_correction_id, 'corrections': corrections } response = self._execute( create_release_correction_detail.query, payload, correlation_id) if response.get('errors'): return response return response['data']['createProductCorrectionDetail'] def get_project_available_channels( self, project_id, account_id, subaccount_id, correlation_id): """Call the Orchard getProjectAvailableChannels query.""" payload = { 'projectId': int(project_id), 'accountId': account_id, 'subaccountId': subaccount_id } response = self._execute( get_project_available_channels.query, payload, correlation_id) if response.get('errors'): return response return response['data']['getAvailableVideoChannelsByProjectId'] def get_product_available_channels(self, product_id, correlation_id): """Call the Orchard getAvailableVideoChannelsByProductId query.""" payload = { 'productId': int(product_id) } response = self._execute( get_product_available_channels.query, payload, correlation_id) if response.get('errors'): return response return response['data']['getAvailableVideoChannelsByProductId'] def submit_product(self, product_id, correlation_id, vendor_id=None, subaccount_id=None): """Call the submitProduct mutation.""" payload = { 'productId': product_id } if subaccount_id: payload['subaccountId'] = subaccount_id response = self._execute( submit_product_with_subaccount_id.query, payload, correlation_id ) else: payload['vendorId'] = vendor_id response = self._execute( submit_product_with_vendor_id.query, payload, correlation_id) if response.get('errors'): return response return response['data']['submitProduct'] def get_project_by_project_code( self, project_code, account_id, subaccount_id, correlation_id): """Call the Orchard projectByProjectCode query.""" payload = { 'projectCode': project_code, 'accountId': account_id, 'subaccountId': subaccount_id } response = self._execute( get_project_by_project_code.query, payload, correlation_id) if response.get('errors'): return response return response['data']['projectByProjectCode'] def get_track_by_tuid(self, tuid, correlation_id): """Call the Orchard getTrack query.""" payload = { 'tuid': tuid, } response = self._execute( get_track_by_tuid.query, payload, correlation_id) if response.get('errors'): return response return response['data']['track'] def get_participation_role_categories(self, correlation_id): response = self._execute( get_participation_role_categories.query, {}, correlation_id) if response.get('errors'): return response return response['data']['participationRoleCategories'] def create_label_participant(self, participant, vendor_id, subaccount_id, correlation_id): payload = { 'name': participant['name'], 'vendorId': vendor_id, 'subaccountId': subaccount_id, 'spotifyId': format_spotify_uri(participant['spotifyUri']), 'appleId': participant['appleId'] } response = self._execute( create_label_participant.query, payload, correlation_id) if response.get('errors'): return response return response['data']['labelParticipantCreate'] def set_label_sound_recording_participations(self, isrc, vendor_id, subaccount_id, participations, correlation_id): payload = { 'isrc': isrc, 'vendorId': vendor_id, 'subaccountId': subaccount_id, 'participations': participations } response = self._execute( set_label_sound_recording_participations.query, payload, correlation_id) if response.get('errors'): return response return response['data']['setLabelSoundRecordingParticipations'] def get_gql_product_client(): """Get GQL Product client.""" return GraphQLProductClient( config.GRAPHQL_PRODUCT_URL, config.GRAPHQL_SECRET, config.GRAPHQL_SECRET_NAME )