"""Interface to the ows-assets microservice.""" from oto import response from owsrequest import request from product_digital.connectors.sentry import send_to_sentry from product_digital.constants import error, header, services from product_digital.utils import error_handling def get_assets(product_id, orchard_user_id, account_type='', account_id=''): """Get the assets info belonging to a product. Args: product_id (int): the product relating the assets orchard_user_id (str): ALW or OA user id account_type (str): the user account type (vendor/subaccount) account_id (str): the user account id Returns: response.Response: status of assets call and dictionary of results """ request_headers = {} if orchard_user_id: request_headers[header.ORCHARD_USER_ID] = orchard_user_id if account_type: request_headers[header.GRASS_ACCOUNT_TYPE] = account_type if account_id: request_headers[header.GRASS_ACCOUNT_ID] = account_id ows_assets_response = request.get( services.OWS_ASSETS, '/asset/product/{product_id}'.format(product_id=product_id), headers=request_headers) info_body = ows_assets_response.json() if ows_assets_response.status_code != 200: return response.create_error_response( status=ows_assets_response.status_code, code=info_body.get('code'), message=info_body.get('message')) return response.Response(message=info_body) def get_correction_image(product_id, orchard_user_id): """Get the correction image for a product. Args: product_id (int): the product the assets belong to orchard_user_id (str): ALW or OA user id needed by ows-assets to evaluate feature flags. Returns: response.Response: Contains url to correction image """ ows_assets_response = request.get( services.OWS_ASSETS, '/image/product/large_cover_correction/{}/location'.format(product_id), ) if ows_assets_response.status_code != 200: info_body = ows_assets_response.json() return response.create_error_response( status=ows_assets_response.status_code, code=info_body.get('code'), message=info_body.get('message')) info_body = { 'image': ows_assets_response.text } return response.Response(message=info_body) def validate_artwork(product_id): """Get the artwork location info. Args: product_id (int): the product relating the assets Returns: response.Response: status of assets call and dictionary of results """ artwork_response = request.get( services.OWS_ASSETS, '/validators/product/{product_id}/artwork'.format( product_id=product_id)) if artwork_response.status_code != 200: try: response_body = artwork_response.json() return response.create_error_response( status=artwork_response.status_code, code=response_body.get('code'), message=response_body.get('message')) except Exception as err: # noqa send_to_sentry(err, {}, 500, error.ERROR_MESSAGE_ARTWORK_VALIDATION_FAILED) return response.create_error_response( code=error.INTERNAL_ERROR, message='assets error', status=500) return response.Response() def validate_artwork_v2(product_id, require_v1=False): """Check that v2 artwork is complete for the given product. Args: product_id (int): ID of the product to check. require_v1 (bool): whether or not to force v1 validation. Returns: response.Response: result of the completeness check. """ query_params = {} if require_v1: query_params['require_v1'] = '1' artwork_response = request.get( services.OWS_ASSETS, '/v2/validators/product/{}/artwork'.format(product_id), params=query_params ) if artwork_response.status_code in [400, 404]: return response.create_not_found_response('Artwork incomplete') if artwork_response.status_code != 200: error_handling.log_unexpected_response( services.OWS_ASSETS, artwork_response ) return response.create_fatal_response( 'Unexpected response from ows-assets' ) return response.Response() def get_image_text_extract(product_id): """Fetch the OCR text extracted from a product's artwork via ows-assets. Calls the ows-assets image-text-extract endpoint for the given product. Returns a not-found response when the artwork is missing or incomplete (HTTP 400/404), and a fatal response for any other unexpected status. Args: product_id (int): The product whose artwork text should be retrieved. Returns: response.Response: On success, the message contains the JSON body from ows-assets (including ``result.block_text``). On failure, returns an appropriate error or fatal response. """ ows_response = request.get( services.OWS_ASSETS, f'/v2/asset/product/{product_id}/image-text-extract' ) if ows_response.status_code in [400, 404]: return response.create_not_found_response('Artwork incomplete') if ows_response.status_code != 200: error_handling.log_unexpected_response( services.OWS_ASSETS, ows_response ) return response.create_fatal_response( 'Unexpected response from ows-assets' ) return response.Response(message=ows_response.json()) def delete_product_corrections_v2(product_id): """Delete correction assets v2 for a given product. Args: product_id (int): the product for which corrections will be deleted. Returns: response.Response: status of assets call. """ ows_assets_response = request.delete( services.OWS_ASSETS, '/v2/product/{}/corrections'.format(product_id), ) if ows_assets_response.status_code not in [200, 404]: return response.Response( message='Error deleting correction assets v2', status=ows_assets_response.status_code) return response.Response()