Example service
This example service handles only GET requests to return a specific project using project.abbrevation.
GetProject: Directory structure
Section titled “GetProject: Directory structure”Directoryperseus
Directoryservices
Directorycustom
- __init__.py
- GetProject.py Our example
- …
- __init__.py
- BaseService.py
GetProject service
Section titled “GetProject service”from typing import Any
from perseus.security import CoreRoleNamefrom perseus.datamanager import ( DatabaseManager, Project,)from perseus.documentation import ( EndpointDocumentation, EndpointDocumentationPropertyType, EndpointDocumentationParameter, EndpointDocumentationProperty,)from .. import BaseService
class GetProject(BaseService):
@classmethod def initialize(cls): GetProject.add_permission(CoreRoleName.OWNER) GetProject.add_permission(CoreRoleName.CLUSTER_ADMIN)
get_doc = EndpointDocumentation("", "Get project using the abbreviation") get_doc.add_parameter( EndpointDocumentationParameter( "abbreviation", "query", EndpointDocumentationPropertyType.STRING, "use the abbreviation", ) ) get_doc.add_response( 200, "Action was successful", [ Project.openapi_documentation_property(accept_null=True) ], )
GetProject.add_get_documentation(get_doc)
@classmethod def handle_get( cls, path: str, abbreviation: str | None = None, **kwargs ) -> dict[str, Any]: db_manager = DatabaseManager() if abbreviation is None: return {"project": None}
projects = db_manager.get_items( Project, search_filter={"abbreviation": project_compute_id}, ) if projects is None or len(projects) > 1: return {"project": None}
return {"project": projects[0]}