Servicers
Servicers are server-side (backend) classes where you implement the
methods you declared for your durable state data types in your
.proto
files.
To implement the methods for a state type called, e.g., Account
, you'll need to
subclass the generated Account.Servicer
class and provide an
implementation for the abstract methods declared in that class. For example:
- Python
- TypeScript
from bank.v1.bank_rbt import Account
class AccountServicer(Account.Servicer):
# ...
import { Account } from "../../api/bank/v1/account_rbt.js";
class AccountServicer extends Account.Servicer {
// ...
}
Each method will get passed:
context
, a subclass ofContext
specific to the method kind (e.g.,ReaderContext
).request
of the type declared in the.proto
.
reader
, writer
, and transaction
methods are also passed:
state
, e.g.,Account.State
for theAccount
servicer.
See workflow
for more details about how they
read and write state.
Here's an example of the reader
method Balance
on Account
:
- Python
- TypeScript
async def Balance(
self,
context: ReaderContext,
state: Account.State,
request: BalanceRequest,
) -> BalanceResponse:
return BalanceResponse(balance=state.balance)
async balance(
_context: ReaderContext,
state: Account.State,
request: BalanceRequest
): Promise<PartialMessage<BalanceResponse>> {
return { balance: state.balance };
}
If you've used gRPC this will look very familiar, with the
difference being the type of context
and the inclusion of the
state
argument for some of the methods.
For more details on the individual methods see the docs for: