Step 2: Build the Python Client
Create a Python file in any editor. For example, create a file named
To import these modules, add these import statements at the top of the file.
Set a semaphore at the beginning of the program.
Because of Python gRPC’s design, the program shuts down immediately if no response comes back in the milliseconds between calling an RPC and the end of the program. When you set a semaphore, the client keeps running indefinitely. In this example, you define the semaphore globally. Because this sample starts one subscription via one Subscribe RPC call, the global semaphore works. However, the global semaphore isn’t appropriate for multiple subscriptions because the semaphore is shared. If you change this sample code to make multiple Subscribe RPC calls, make sure that each Subscribe call uses a separate semaphore and the semaphore isn’t shared among subscriptions.
Create a global variable to store the replay ID.
Set up the gRPC channel, and generate the stub. The gRPC channel points to the Pub/Sub API endpoint
api.pubsub.salesforce.com:7443. In this quick start, we use port
7443, but you can alternatively use port
443. If your company uses a firewall for network security, make sure that the Pub/Sub API endpoint is allowed in the firewall configuration.
Retrieve your session token. Replace the username, password, and API login URL with values for your Salesforce org. If you’re using a production instance, the API login URL is https://login.salesforce.com/services/Soap/u/55.0/. If it’s a sandbox, the URL is https://test.salesforce.com/services/Soap/u/55.0/. Send a POST request formatted like this to the login URL.
- When you copy the code snippet, ensure that it aligns with the indented block of code above it. In Python, code blocks are delimited by their indentation.
- If you haven’t set up a range of trusted IP addresses for your org, append a security token to your password. For more information, see Reset Your Security Token and Set Trusted IP Ranges for Your Organization.
- The code example uses username and password authentication for simplicity, but we recommend that you use OAuth in production apps. For more information, see Supported Authentication.
Run this client by entering this command on the command line.
When the request returns, you have XML-formatted data in the response content field
res.content. It contains a session ID wrapped within the
<sessionId>tags. It also contains the server URL wrapped within the
<serverUrl>tags and the org ID within the
<userInfo>. Note those values because you use them in the next step.
After getting the session information, you can delete or comment out the login code you added in the previous step. To comment out a line in Python, prepend it with
Store the authorization information, including session ID, instance URL, and org ID, in a tuple called
authmetadata. Each element in this tuple is also a tuple. You use this information when subscribing to a channel or publishing events.
sessionidplaceholder value with the session ID that you got from the previous step.
instanceurlplaceholder value with the first part of the server URL that you got from the previous step, without the path portion. For example,
tenantidplaceholder value with the org ID that you got from the previous step. Alternatively, you can get your organization ID value by following the steps in Find your Salesforce Organization ID in Salesforce Help. For more information about the headers, see Include Authorization Headers in RPC Method Calls.
Generate your stub object like this.
PubSubStubcomes from the
pubsub_api_pb2_grpc.pyfile, which you generated in the previous step.