Menu

1 / 2

gRPC in Python, the FastAPI way: models in, models out

DEV Community: rust·William·14 days ago
#dkmoPWJW
#devtorust#devto#dev#orders#fastgrpc3
Reading 0:00
15s threshold

Here is what a gRPC handler usually looks like in Python:

# 1. write orders.proto # 2. python -m grpc_tools.protoc -I. --python_out=. --grpc_python_out=. orders.proto # 3. commit orders_pb2.py and orders_pb2_grpc.py, regenerate them at every change import grpc import orders_pb2 , orders_pb2_grpc

class OrdersServicer ( orders_pb2_grpc . OrdersServicer ): async def Get ( self , request , context ): if request . order_number not in DB : await context . abort ( grpc . StatusCode . NOT_FOUND , " no such order " ) return orders_pb2 . Order ( order_number = request . order_number , paid = True )

Enter fullscreen mode

Exit fullscreen mode

It works. But the messages are not your domain models, nothing validates them, the generated files drift from the schema, and every new service starts with the same servicer-class ceremony.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.