GET requests
The class method handle_get() handles all GET requests for the service. Add this method to accept GET requests and add
logic to such a request:
class MyService(BaseService): ... @classmethod def handle_get(cls, path: str, **kwargs) -> dict[str, Any]:The method expects the following parameters:
path: Sub path of the request like my-sub-path (/service/MyService/my-sub-path)**kwargs: Dictionary of keyword arguments (URL parameters) - you should declare and define the URL parameters as method parameters
Declare URL parameters in handle_get() and define None as their default value, so that you know when this parameter is missing:
@classmethoddef handle_get(cls, path: str, search_term: str | None = None, **kwargs) -> dict[str, Any]:handle_get() has to return a dictionary with only strings as keys.
Example handle_get() method
Section titled “Example handle_get() method”The following service returns projects from the database using a URL parameter as a search term. The project search is using the project abbreviation.
The endpoint would be available via GET /service/MyService/search/?search_term=my-abbreviation.
from typing import Any
from perseus.datamanager import DatabaseManager, Project
class MyService(BaseService): ...
@classmethod def handle_get(cls, path: str, search_term: str | None = None, **kwargs) -> dict[str, Any]: if path == "search": if search_term is None: return {"result": False}
db_manager = DatabaseManager() projects = db_manager.get_items(Project, search_filter={"abbreviation": search_term})
if projects is None: return {"result": False}
return {"projects": projects} return {"result": False}