"""Interface to the ows-product-digital microservice.""" import json from oto import response from oto import status as status_code from owsrequest import request from backend.constants import error from backend.constants import header from backend.constants import services from backend.utils import model_utils def get_product_by_product_id(product_id, orchard_user_id=None): """Get digital product metadata by product_id. Args: product_id (int): id of the product orchard_user_id (str): user id of an orchard user Returns: oto.response.Response """ url = '/product/audio/{0}'.format(product_id) headers = {header.CONTENT_TYPE: header.CONTENT_TYPE_JSON_VAL} if orchard_user_id: headers[header.ORCHARD_USER_ID] = orchard_user_id r = request.get(services.OWS_PRODUCT_DIGITAL, url, headers=headers) if r.status_code == status_code.OK: return response.Response(message=r.json()) return model_utils.process_error_response( r, code=error.OWS_PRODUCT_DIGITAL_ERROR_CODE) def post_track_correction(product_id, correction_id, data, correlation_id, orchard_user_id): """Post artist specific track correction data. Args: product_id(int): unique identifier of the releases table correction_id(int): unique identifier of the release_correction table data(list): list of valid artist objects correlation_id(string): correlation identifier orchard_user_id(string): orchard user id in the format user_type:user_id Returns: oto.response.Response """ url = '/product/{0}/correction/{1}/details'.format(product_id, correction_id) data = {'items': data} result = request.post( services.OWS_PRODUCT_DIGITAL, url, data=json.dumps(data), headers={header.CONTENT_TYPE: header.CONTENT_TYPE_JSON_VAL, 'Orchard-User-Id': orchard_user_id}, correlation_id=correlation_id) if result.status_code in [200, 201, 204]: return response.Response() return model_utils.process_error_response( result, code=error.OWS_PRODUCT_DIGITAL_ERROR_CODE) def get_product_spatial(product_id): """Get release_spatial data by product_id. Args: product_id (int): ID of the product. Returns: oto.response.Response. """ url = '/product/{0}/spatial'.format(product_id) headers = {header.CONTENT_TYPE: header.CONTENT_TYPE_JSON_VAL} result = request.get(services.OWS_PRODUCT_DIGITAL, url, headers=headers) if result.status_code == status_code.OK: return response.Response(message=result.json()) return model_utils.process_error_response( result, code=error.OWS_PRODUCT_DIGITAL_ERROR_CODE) def create_product_spatial(product_id, orchard_user_id=None): """Create a spatial UPC record for a product. Args: product_id (int): ID of the product. orchard_user_id (str): user id of an orchard user. Returns: oto.response.Response """ url = '/product/{0}/spatial'.format(product_id) headers = {header.CONTENT_TYPE: header.CONTENT_TYPE_JSON_VAL} if orchard_user_id: headers[header.ORCHARD_USER_ID] = orchard_user_id result = request.post( services.OWS_PRODUCT_DIGITAL, url, data=json.dumps({}), headers=headers) if result.status_code == 200: return response.Response() return model_utils.process_error_response( result, code=error.OWS_PRODUCT_DIGITAL_ERROR_CODE)