""" Interface to ows-territories 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.constant import field_const def call_ows_territories(endpoint, correlation_id): """Call the ows-territories endpoint and get response Args: endpoint (str): endpoint of ows-territories with required params correlation_id (str): correlation id for logging Returns: response.Response """ territories_response = request.get( api_const.OWS_TERRITORIES, endpoint, correlation_id=correlation_id) message = utils.get_response_json(territories_response) if territories_response.status_code == 200: return response.Response(message=message) return response.create_error_response( code=error.OWS_TERRITORIES_ERROR, message=message, status=territories_response.status_code) def get_territories(standard, correlation_id=None): """Get territories from ows-territories with given standard Args: standard (str): country standard code correlation_id (str): correlation id for logging Returns: response.Response """ endpoint = '/territory/{}'.format(standard) return call_ows_territories(endpoint, correlation_id) def get_complement_territories( territories, standard=field_const.ISO_3166_1_2016, correlation_id=None): """Get complement territories for a given ISRC Args: territories(list): The list of territories from which to get the complement standard (str): country standard code correlation_id (str): correlation id for logging Returns: response.Response """ territories_csv = ','.join(territories) # comma seperated territories endpoint = '/complement/{}?territory_code_a2={}'.format( standard, territories_csv) return call_ows_territories(endpoint, correlation_id) def convert_territories( territories, input_standard=field_const.ORCH_1_2016, output_standard=field_const.ISO_3166_1_2016, correlation_id=None): """Convert territories from input standard to output standard Args: territories(list): The list of territories from which to get the complement input_standard (str): input country standard code output_standard (str): output country standard code correlation_id (str): correlation id for logging Returns: response.Response """ territories_csv = ','.join(territories) # comma seperated territories endpoint = '/conversion/{}/{}?territory_code_a2={}'.format( input_standard, output_standard, territories_csv) return call_ows_territories(endpoint, correlation_id)