"""Interface to ows-territories microservice.""" from oto import response from conflict_manager.constants import ows_territories from conflict_manager.constants import services from conflict_manager.utils import service_utils _response_cache = {} def get_territories_map(standard=ows_territories.ISO_3166_1_2016): """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/{}'.format(standard) res = _response_cache.get(endpoint) if res: return res # Cache didn't have a valid response, so query service res = service_utils.get_from_ows_service( services.OWS_TERRITORIES, endpoint) # Map results before caching to simplify working with data if res: res = response.Response(_make_territory_map(res.message['items'])) _response_cache[endpoint] = res return res def _make_territory_map(territories): """Make map of territories with `territory_code_a2`.""" territories_map = {} for territory in territories: territories_map[territory['territory_code_a2']] = territory return territories_map