"""Interface to ows-territories microservice.""" from oto import response from owsrequest import request as requests from sales_goals.constants import ows_services _response_cache = {} def get_territories(): """Get territories information from ows-territories by standard name. Args: standard (str): Territory standard ('ISO_3166_1_2016' or 'Orch_1_2016') Returns: oto.Response: response object containing territories or errors """ endpoint = '/territory/Orch_1_2016' res = _response_cache.get(endpoint) if res: return res # Cache didn't have a valid response, so query service res = requests.get( ows_services.OWS_TERRITORIES_NAME, endpoint) # Map results before caching to simplify working with data if res: res = response.Response(_transform_territories(res.json()['items'])) _response_cache[endpoint] = res return res def _transform_territories(territories): """Add keys to the territories to standardize them.""" for territory in territories: territory['name'] = territory['territory_name'] territory['country_id'] = territory['orch_id'] territory['category'] = 'Country' return territories