"""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 http import HTTPStatus from typing import Any from owsrequest import request from video import config from video.constants import header, service from video.exceptions import ProjectNotFound, ProjectOwnershipError def get_project_by_id(project_id: int) -> dict[str, Any]: """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: dict[str, Any]: the project data. Raises: ProjectNotFound: if the project does not exist. HTTPError: if the upstream service returns an error. """ project_response = request.process( application=config.SERVICE_NAME, environment=config.ENVIRONMENT, method="GET", service_name=service.OWS_PROJECT, uwsgi_cache_enabled=True, path=f"/project/{project_id}", ) if project_response.status_code == HTTPStatus.NOT_FOUND: raise ProjectNotFound() project_response.raise_for_status() return project_response.json() def check_project_ownership( project_id: int, account_type: int | str | None = None, account_id: int | str | None = None, ) -> 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 (int | str | None): Grass account type. account_id (int | str | None): Grass account id. Raises: ProjectOwnershipError: if the upstream service denies access. """ if not account_type and not account_id: return 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 = request.process( application=config.SERVICE_NAME, environment=config.ENVIRONMENT, method="HEAD", service_name=service.OWS_PROJECT, uwsgi_cache_enabled=True, path=request_url, ) if not ownership_response.ok: raise ProjectOwnershipError() def create_project( data: dict[str, Any], account_type: str, account_id: str ) -> dict[str, Any]: """Create a project.""" headers = { header.GRASS_ACCOUNT_TYPE: account_type, header.GRASS_ACCOUNT_ID: account_id, } result = request.process( application=config.SERVICE_NAME, environment=config.ENVIRONMENT, method="POST", service_name=service.OWS_PROJECT, uwsgi_cache_enabled=True, path="/project", json=data, headers=headers, ) result.raise_for_status() return result.json()