"""Utility Functions for Services.""" from oto import response from owsrequest import request from conflict_manager.constants import services from conflict_manager.exceptions import ServiceResponseError def get_from_ows_service(service_name, endpoint, **kwargs): """Call Orchard microservice and return a Response. Args: service_name (str): service name to query endpoint (str): endpoint to GET the response from kwargs: Additional argument to pass for request Returns: oto.response.Response: response object with information or error Raises: ServiceResponseError """ get_response = request.get(service_name, endpoint, **kwargs) return process_response(get_response, service_name) def process_response(res, service_name): """Process the service response. Args: res (request.Response): The requests library response service_name (str): The name of the service Raises: ServiceResponseError """ if res.status_code == 200: return response.Response(message=res.json()) try: response_errors = res.json() except: # noqa: E722 response_errors = res.content.decode() error_code = services.SERVICE_ERROR_CODES[service_name] raise ServiceResponseError( message=response_errors, error_code=error_code, status_code=res.status_code)