A proto file defines the contract for a service: structured message types and operations such as GetOrCreateSession and AddMessages. It is declarative rather than executable code. section 4.5
The Protocol Buffer compiler turns the definition into Python artifacts. One module provides message classes; another provides a gRPC stub whose methods serialize requests, perform remote calls, and deserialize responses. section 4.5
Treat the proto file as the boundary between service design and client implementation:
- Define request and response messages with fields such as user_id and session_id. section 4.5
- Declare service operations that become methods on the generated stub. section 4.5
- Regenerate the Protocol Buffer stubs whenever the definition changes, preventing stale accessors and client mismatches. section 4.5
Listing 4.16 SessionClient setup
from proto import sessions_pb2, sessions_pb2_grpc
from .base import BaseClient
class SessionClient(BaseClient):
def __init__(self, platform):
super().__init__(platform,
➥service_name="sessions") #A
self._stub = sessions_pb2_grpc.
➥SessionServiceStub(self._channel) #B
Shows how a generated SessionServiceStub is attached to a client channel. section 4.5
