Sub topic 4
Generating client stubs from pubsub_api.proto
This chapter assumes you can read Python, know what a gRPC streaming call is from the previous chapter, and have a working Python 3 environment with pip. By the end, you will have generated the Python modules that the later chapters import.
What the .proto file does
Salesforce publishes the Pub/Sub API contract as a Protocol Buffers file named pubsub_api.proto. It defines three things:
- The RPC methods of the
PubSubservice:GetTopic,GetSchema,Subscribe,Publish, and others. - The message types those methods send and receive, such as
TopicRequest,SchemaRequest,FetchRequest, andFetchResponse. - Enums such as
ReplayPreset.
A stub is generated code that lets you call a remote method as if it were a local Python function. The Protocol Buffers compiler, protoc, reads the .proto file and writes the stub code, so you never hand-write serialization.
Get the file and the tools
Download pubsub_api.proto from Salesforce's pub-sub-api repository on GitHub, and place it in your project directory. Then install the tools in a virtual environment:
python -m venv .venv
source .venv/bin/activate
pip install grpcio grpcio-tools fastavrogrpcio is the runtime that opens channels. grpcio-tools bundles protoc and the Python gRPC plugin. fastavro is used later to decode event payloads.
Generate the code
Run the compiler through Python so that the version of protoc matches your installed grpcio-tools:
python -m grpc_tools.protoc \
-I. \
--python_out=. \
--grpc_python_out=. \
pubsub_api.protoEach option does one job:
| Option | Meaning |
|---|---|
| `-I.` | Look for `.proto` files in the current directory. |
| `--python_out=.` | Write message classes to `pubsub_api_pb2.py`. |
| `--grpc_python_out=.` | Write client and server classes to `pubsub_api_pb2_grpc.py`. |
Check the generated modules
The earlier examples import both modules under short names:
import pubsub_api_pb2 as pb2
import pubsub_api_pb2_grpc as pb2_grpc
request = pb2.TopicRequest(topic_name="/data/AccountChangeEvent")
print(request)
print(hasattr(pb2_grpc, "PubSubStub"))Run this from the directory that contains the generated files. It should print the request fields and True. Constructing the request needs no network connection or credentials, so this is a safe first test.
Common errors
ModuleNotFoundError: No module named 'pubsub_api_pb2': Python cannot see the generated files. Run your script from the directory that holds them, or add that directory toPYTHONPATH.- Import error inside
pubsub_api_pb2_grpc.py: This happens when you generate into a subdirectory with a different-Ipath, so the grpc module cannot findpubsub_api_pb2. Generate both files into the same directory the-Ipath points to. - A protobuf runtime version error when importing: The generated code needs a compatible
protobufpackage. Regenerate the files with the currentgrpcio-toolsafter upgrading, rather than mixing versions. - Stale stubs after Salesforce updates the proto: Download the new file and regenerate. Do not edit the generated files by hand.
Exercise
You run your subscriber and get ModuleNotFoundError: No module named 'pubsub_api_pb2', though you generated the stubs yesterday. Name two likely causes and a fix for each.
Show answer
First, you are running the script from a different directory, or the files were written elsewhere. Fix it by running from the directory that contains pubsub_api_pb2.py, or by adding that directory to PYTHONPATH. Second, you are in a different virtual environment, or the generation step never wrote the files because it failed. Fix it by activating the right environment and rerunning python -m grpc_tools.protoc -I. --python_out=. --grpc_python_out=. pubsub_api.proto, then checking that both _pb2.py and _pb2_grpc.py exist.