Pydantic
You can use Pydantic to define your API for Python backends.
To declare a data type called ChatRoom, put the following in a .py file:
from pydantic import BaseModel
from typing import Optional
from reboot.api import (
API,
Field,
Methods,
Reader,
Writer,
StateModel,
Type,
)
class ChatRoomState(StateModel):
messages: Optional[list[str]] = Field(tag=1)
class MessagesResponse(BaseModel):
messages: list[str] = Field(tag=1)
class SendRequest(BaseModel):
message: str = Field(tag=1)
ChatRoomMethods = Methods(
messages=Reader(
request=None,
response=MessagesResponse,
),
send=Writer(
request=SendRequest,
response=None,
),
)
api = API(
ChatRoom=Type(
state=ChatRoomState,
methods=ChatRoomMethods,
),
)
Reboot uses standard
Pydantic BaseModel,
with StateModel as the base class for your durable state types.
In addition to defining the data type, you'll also need to define operations
for that type. In the example above, ChatRoomMethods defines the
operations for the ChatRoom type.
Your state class should end in State, e.g., ChatRoomState. Your
Methods variable should end in Methods, e.g., ChatRoomMethods. Your
API types are composed of state and methods and should be
descriptive of the interface, e.g., ChatRoom.
Depending on its kind, a method
might be able to only read (e.g., Reader) or both read and write
(e.g., Writer) the state.
In the example above, ChatRoom has just one Reader and one Writer
method, but it can have any number of Reader, Writer,
Transaction, and Workflow methods.
All Pydantic fields must include a tag parameter using
Field(tag=N). This is required for safe backwards compatibility.
To learn more about how you implement each data type's methods see Implement your API.