"""Verify a product is owned by an account through ows-product microservice.""" from oto import response from owsrequest import request from salessheets import config from salessheets.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) message = ows_product_response.content if ows_product_response.status_code == 200: return response.Response() if 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) return response.Response( message=message, status=ows_product_response.status_code)