"""Interface to the ows-project-manager microservice. The model is responsible to make a call to a ows-project-manager microservice. It makes a request to ows-project-manager endpoint ('/project/' ,methods=['GET']) and returns a result of the request. """ from oto import response from owsrequest import request as requests from ows_product_physical.constant import service_name from ows_product_physical.constant import success from ows_product_physical.utils.response_handler_util import safe_decode_json ERROR_FETCH_PROJECT_BY_ID = dict( code='fetch_project_id_error', message='Impossible to fetch the project by its id.') def get_project_by_id(project_id): """Get project by its id. Fetch project information by performing a GET call to ows-project-manager (see: GET /project/ endpoint). Args: project_id (int): uid of a project. Returns: response.Response: the result of getting project by id. """ project_response = requests.get( service_name.OWS_PROJECT, '/project/{ID}'.format(ID=project_id)) message = safe_decode_json(project_response, service_name.OWS_PROJECT) if project_response.status_code == 200: return response.Response(message=message) return response.Response( errors=message, status=project_response.status_code) def check_project_ownership(project_id, account_type=None, account_id=None): """Check project ownership by its id. Fetch project information by performing a HEAD call to ows-project-manager Args: project_id (int): uid of a project. account_type (str): Grass account type. account_id (str): Grass account id. Returns: response.Response: the result of the HEAD call with a project id. """ if not account_type and not account_id: return response.Response(status=success.SUCCESS_CODE) request_url = ( '/ownership/' '{ACCOUNT_TYPE}/' '{ACCOUNT_ID}/' 'project/' '{PROJECT_ID}' ).format( ACCOUNT_TYPE=account_type, ACCOUNT_ID=account_id, PROJECT_ID=project_id) ownership_response = requests.head(service_name.OWS_PROJECT, request_url) return response.Response(status=ownership_response.status_code)