"""Interface to the ows-track microservice.""" from flask import g from oto import response from owsrequest import request from owsrequest.constants import headers as header_constants from product_digital.constants import error from product_digital.constants import header from product_digital.constants import services from product_digital.exceptions import RequestError def validate_tracks(product_id, orchard_user_id=''): """Confirm the validation status of tracks belonging to a product. Args: product_id (int): the product relating the tracks to be confirmed orchard_user_id (string): user id of an orchard user Returns: response.Response: status of validation call and dictionary of results """ headers = {k: str(v) for k, v in { header.ORCHARD_USER_ID: orchard_user_id, header_constants.ORCHARD_PROFILE_TYPE: g.request_context.profile_type, header_constants.ORCHARD_PROFILE_ID: g.request_context.profile_id, header_constants.ORCHARD_IDENTITY_ID: g.request_context.identity_id, }.items() if v} validation_context = getattr(g, 'validation_context', 'pre_submission') ows_track_response = request.get( services.OWS_TRACK, '/product/{product_id}/tracks/validate'.format(product_id=product_id), params={ 'validation_context': validation_context, }, headers=headers ) validation_body = ows_track_response.json() if ows_track_response.status_code != 200: return response.create_error_response( status=ows_track_response.status_code, code=validation_body.get('code'), message=validation_body.get('message')) return response.Response(message=validation_body) def validate_publishing_obligation( product_id, account_type, account_id, orchard_user_id=''): """Confirm the validation status of publishing obligations of product. Args: product_id (int): the product relating the obligations to be confirmed account_type (str): the user account type in the header account_id (int): the user account id in the header orchard_user_id (string): user id of an orchard user. Returns: response.Response: status of validation call and dictionary of results """ query_params = { 'account_id': account_id, 'account_type': account_type} headers = {k: str(v) for k, v in { header.ORCHARD_USER_ID: orchard_user_id, header_constants.ORCHARD_PROFILE_TYPE: g.request_context.profile_type, header_constants.ORCHARD_PROFILE_ID: g.request_context.profile_id, }.items() if v} ows_track_response = request.get( services.OWS_TRACK, '/product/{product_id}/tracks/validate/publishing-obligation'.format( product_id=product_id), params=query_params, headers=headers, ) validation_body = ows_track_response.json() if ows_track_response.status_code != 200: return response.create_error_response( status=ows_track_response.status_code, code=validation_body.get('code'), message=validation_body.get('message')) return response.Response(message=validation_body) def get_track_by_track_id(track_id): """Get details about a track. Args: track_id (int): the track id to be searched Returns: response.Response: status of call and dictionary of results """ ows_track_response = request.get( services.OWS_TRACK, '/track/{}'.format(track_id), ) response_body = ows_track_response.json() if ows_track_response.status_code != 200: return response.create_error_response( status=ows_track_response.status_code, code=response_body.get('code'), message=response_body.get('message')) return response.Response(message=response_body) def get_instant_grats_by_product_id(product_id): """Get details about a product's Instant Grats. Args: product_id (int): the product id to be searched Returns: response.Response: status of call and dictionary of results """ ows_track_response = request.get( services.OWS_TRACK, '/product/{}/tracks/grats'.format(product_id)) response_body = ows_track_response.json() if ows_track_response.status_code != 200: return response.create_error_response( status=ows_track_response.status_code, code=response_body.get('code'), message=response_body.get('message')) return response.Response(message=response_body) def change_genre(product_id, request_data, orchard_user_id=None): """Clear hidden artist data upon genre change. Args: product_id (int): product id request_data (dict): dict containing genre_id, subgenre_id and correction_id orchard_user_id (str): orchard user id Returns: response.Response: status of post call """ headers = {k: str(v) for k, v in { header.ORCHARD_USER_ID: orchard_user_id or None, header_constants.ORCHARD_PROFILE_TYPE: getattr(g.request_context, 'profile_type', None), header_constants.ORCHARD_PROFILE_ID: getattr(g.request_context, 'profile_id', None), header_constants.ORCHARD_IDENTITY_ID: getattr(g.request_context, 'identity_id', None), }.items() if v} ows_track_response = request.post( services.OWS_TRACK, '/product/{}/tracks/change_genre'.format(product_id), json=request_data, headers=headers, ) if ows_track_response.status_code not in (200, 204, 404, 403): return response.create_error_response( code=error.INTERNAL_ERROR, message=error.ERROR_CODE_NOT_FOUND, status=500) return response.Response( status=ows_track_response.status_code) def delete_tracks_by_product_id(product_id, track_ids): """Delete track data and related metadata for the product. Args: product_id (int): product id track_ids(str): unique id of all the tracks """ ows_track_response = request.delete( services.OWS_TRACK, '/product/{}/tracks'.format(product_id), params={'tuids': track_ids}, ) response_body = ows_track_response.json() if ows_track_response.status_code != 200: return response.create_error_response( status=ows_track_response.status_code, code=response_body.get('code'), message=response_body.get('message')) return response.Response( status=ows_track_response.status_code) def get_tracks_by_product_id(product_id): """Get track-level artists. Args: track_id (int): the track id to be searched Returns: response.Response: status of call and dictionary of results """ ows_track_response = request.get( services.OWS_TRACK, '/product/{}/tracks'.format(product_id), ) if ows_track_response.status_code != 200: raise RequestError( 'Error retrieving tracks from ows-track', error_code=error.INTERNAL_ERROR, status=ows_track_response.status_code ) response_body = ows_track_response.json() return response.Response(message=response_body)