Skip to content

POST requests

The class method handle_post() handles all POST requests for the service. Add this method to accept POST requests and add logic to such a request:

custom/MyService.py
class MyService(BaseService):
...
@classmethod
def handle_post(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 (body parameters) - you should declare and define the body parameters as method parameters

Declare body parameters in handle_post() and define None as their default value, so that you know when this parameter is missing:

custom/MyService.py
@classmethod
def handle_post(
cls,
path: str,
template_id: str | None = None,
name: str | None = None,
content: str | None = None,
email_template_type: str = "EXTERNAL",
**kwargs
) -> dict[str, Any]:

The following method creates a new database entry for objects of the class EmailTemplate or updates an existing one.

The method uses path to find out whether a database object with the specified object ID should be updated:

  • /service/MyService/new: Creates a new EmailTemplate
  • /service/MyService/afg8928iuhadsdh755637a8sd: Updates an existing EmailTemplate
custom/MyService.py
from typing import Any
from perseus.datamanager import DatabaseManager, EmailTemplate
from perseus.datamanager.EmailTemplate import EmailTemplateType
from .BaseService import BaseService
class MyService(BaseService):
...
@classmethod
def handle_post(
cls,
path: str,
template_id: str | None = None,
name: str | None = None,
content: str | None = None,
email_template_type: str = "EXTERNAL",
**kwargs
) -> dict[str, Any]:
db_manager = DatabaseManager()
mail_template = db_manager.get_item(EmailTemplate, oid=path)
if mail_template is not None:
if template_id is not None:
mail_template.template_id = template_id
if name is not None:
mail_template.name = name
if content is not None:
mail_template.content = content
if email_template_type is not None:
mail_template.email_template_type = EmailTemplateType(email_template_type)
oid = db_manager.add_item(mail_template)
return {"result": True, "_id": str(oid)}
elif path == "new":
if name is None or content is None or template_id is None:
return {"result": False}
try:
mail_template = EmailTemplate(
template_id=template_id,
name=name,
content=content,
email_template_type=EmailTemplateType(email_template_type),
)
oid = db_manager.add_item(mail_template)
except ValueError:
return {"result": False}
return {"result": True, "content": {"_id": str(oid)}}
return {"result": False}