""" Interface to ows-carveouts microservice """ from oto import response from owsrequest import request from masters_registry import utils from masters_registry.constant import api_const from masters_registry.constant import error from masters_registry.utils import OwsCarveoutError from masters_registry.utils import retry store_names_cache = dict() def get_dms_carveout_for_upc(upc, correlation_id=None): """Get dms (store) level carveouts for a single upc Args: upc (int): UPC correlation_id (str): correlation id for logs Returns: response.Response """ try: # todo: convert this to handle list of upcs endpoint = '/carveout/{}'.format(upc) return _get_from_ows_carveouts_service(endpoint, correlation_id) except OwsCarveoutError as err: return response.create_error_response( code=error.OWS_CARVEOUTS_ERROR, message=err.message, status=err.status_code) @retry() def _get_from_ows_carveouts_service(endpoint, correlation_id): """Helper function to call ows-carveouts and return a Response Args: endpoint (str): endpoint to call Returns: response.Response """ carveout_response = request.get( api_const.OWS_CARVEOUTS, endpoint, correlation_id=correlation_id) message = utils.get_response_json(carveout_response) if carveout_response.status_code == 200: return response.Response(message=message) raise OwsCarveoutError( status_code=carveout_response.status_code, message=message ) def get_territory_carveout_for_upc(upc, correlation_id=None): """Get territory level carveouts for a single upc Retrieves the effective set of carveouts for the provided release, combining release, vendor, and subaccount level carveouts Args: upc (str): UPC correlation_id (str): correlation id for logs Returns: response.Response """ try: endpoint = '/carveout/{}/territory'.format(upc) return _get_from_ows_carveouts_service(endpoint, correlation_id) except OwsCarveoutError as err: return response.create_error_response( code=error.OWS_CARVEOUTS_ERROR, message=err.message, status=err.status_code) def get_store_name(store_id, correlation_id=None): """Get store name by store id. Args: store_id (str): store's id correlation_id (str): correlation id for logs Returns: response.Response """ global store_names_cache if not store_names_cache: endpoint = '/stores' try: carveouts_response = _get_from_ows_carveouts_service( endpoint, correlation_id) # check for non-critical negative response (as 404) if carveouts_response: store_names_cache = carveouts_response.message else: return carveouts_response except OwsCarveoutError as err: return response.create_error_response( code=error.OWS_CARVEOUTS_ERROR, message=err.message, status=err.status_code) try: store_name = store_names_cache[store_id] return response.Response( message=store_name ) except KeyError: return response.Response( status=404, message=error.STORE_DOES_NOT_EXIST )