Service initialization
Please read our concept of services first before developing one.
A service can be used to create:
- API endpoints
- Internal functionality to use in other services or states
- Cronjobs
Directory structure
Section titled “Directory structure”Directoryperseus
Directoryservices
Directorycustom/ Directory for custom services
- __init__.py
- Template.py Tip: Use the template to speed up the process
- …
- __init__.py
- BaseService.py Base service (parent class) for a custom service
- …
Initial service structure
Section titled “Initial service structure”-
Add a new file
Start by adding a new file to your custom service directory and name it like you want, for example
MyService.py.custom/MyService.py from typing import Anyfrom ..BaseService import BaseServiceclass Template(BaseService):@classmethoddef initialize(cls):...@classmethoddef handle_get(cls, path: str, **kwargs) -> dict[str, Any]:...@classmethoddef handle_post(cls, path: str, **kwargs) -> dict[str, Any]:...@classmethoddef handle_delete(cls, path: str, **kwargs) -> dict[str, Any]:...All methods will be explained in the step-by-step guide.
- Handle GET requests
- Handle POST requests
- Handle DELETE requests
-
Register custom service
To allow PERSEUS to use the service you will have to import it to the
__init__.pyfile in the custom service directory. To do this, add the following line to that file:custom/__init__.py from .MyService import MyService
Initialize new service
Section titled “Initialize new service”The class method initialize() will be executed when PERSEUS is starting up.
-
Add permissions
First, you need to set the permissions for the new service by using its
add_permission()method. Add all roles that should have access to this service. You can view a list of all roles here.custom/MyService.py from ..BaseService import BaseServicefrom perseus.security import CoreRoleNameclass MyService(BaseService):@classmethoddef initialize(cls):MyService.add_permission(CoreRoleName.OWNER)MyService.add_permission(CoreRoleName.SUPPORT_L1) -
Add a frontend menu item
Next, let’s create some API endpoints.