"""Verify a product is owned by an account through ows-product microservice.""" from oto import response from owsrequest import request from sales_goals import config from sales_goals.constants import ows_services def get_product_ownership_by_account(account_type, account_id, product_id): """Verify a product is owned by vendor/subaccount. Args: account_type (str): account type, allowed values: "vendor" or "subaccount". account_id (str): id of vendor/subaccount. product_id (str): id of product that should be Returns: Response: empty with status==200 if product belongs to account or with error and status==403 if it doesn't. """ endpoint = '/{account_type}/{account_id}/product/{product_id}'.format( account_type=account_type, account_id=account_id, product_id=product_id) ows_product_response = request.process( ows_services.APPLICATION_NAME, config.ENVIRONMENT, ows_services.SERVICE_CALL_METHOD_HEAD, ows_services.OWS_PRODUCT_SERVICE_NAME, endpoint, None, 'https') if ows_product_response.status_code == 200: return response.Response() elif ows_product_response.status_code == 403: return response.create_error_response( code=ows_services.CODE_FORBIDDEN_PRODUCT, message=ows_services.MESSAGE_FORBIDDEN_PRODUCT.format( product=product_id, account=account_id), status=403) elif ows_product_response.status_code == 404: return response.create_error_response( code=ows_services.CODE_NOT_FOUND_PRODUCT, message=ows_services.MESSAGE_NOT_FOUND_PRODUCT.format( product=product_id), status=404) else: return response.create_error_response( code=ows_services.OWNERSHIP_VALIDATION_ERROR, message=ows_product_response.text, status=ows_product_response.status_code)