Use the Lattice SDK for gRPC in Python
This page shows you how to set up the Lattice SDK for gRPC in Python and make a gRPC request.
Click here to download the SDK. By downloading and/or using, you agree to the terms of use. If you do not agree, do not use the SDK.
Before you begin
- Complete the setup steps to generate a valid API token and publish at least one entity to your environment.
Install the SDK
To install the Lattice SDK in Python:
-
Install the Lattice Python SDK package using pip:
pip install lattice-sdk-python
-
To set up a secure connection to the gRPC server, install
certifi
:pip install certifi
Connect to Lattice APIs
Establish a gRPC channel to connect to Lattice APIs:
-
Create a service stub of Entities API, passing it the credentialed channel:
get_entity.pyfrom anduril.entitymanager.v1 import EntityManagerApiStub
from grpclib.client import Channel
async def get_entity(entity_id):
# Open secure channel
channel = Channel(host="$YOUR_LATTICE_URL", port=443, ssl=True)
# Create service instance
entity_manager_stub = EntityManagerApiStub(channel) -
Define authorization headers to pass in with the request, replacing
$YOUR_BEARER_TOKEN
with the bearer token that you generated.get_entity.pyfrom anduril.entitymanager.v1 import EntityManagerApiStub
from grpclib.client import Channel
# Set authentication
metadata = {
'authorization': 'Bearer $YOUR_BEARER_TOKEN' # Replace $YOUR_BEARER_TOKEN with your token. You must keep the word, Bearer, in the metadata string.
}
async def get_entity(entity_id):
# Open secure channel
channel = Channel(host="$YOUR_LATTICE_URL", port=443, ssl=True)
# Create service instance
entity_manager_stub = EntityManagerApiStub(channel)
Allow self-signed certificates
Make sure you understand the implications of using self-signed certificates. This should only be used for testing.
To connect to a server that is using a self-signed certificate, replace:
channel = Channel(host="$YOUR_LATTICE_URL", port=443, ssl=True)
with the following code:
import ssl
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
channel = Channel(host="$YOUR_LATTICE_URL", port=443, ssl=ctx)
Example request
The following example makes a GetEntity
request to the Entities API to
fetch an entity from the common operational picture (COP):
from anduril.entitymanager.v1 import EntityManagerApiStub, GetEntityRequest
from anduril.ontology.v1 import Disposition
from grpclib.client import Channel
import asyncio
# Set authentication
metadata = {
'authorization': 'Bearer $YOUR_BEARER_TOKEN' # Replace $YOUR_BEARER_TOKEN with your token. You must keep the word, Bearer, in the metadata string.
}
# Get an entity's information
async def get_entity(entity_id):
# open secure channel
channel = Channel(host="$YOUR_LATTICE_URL", port=443, ssl=True)
# create service instance
entity_manager_stub = EntityManagerApiStub(channel)
# get the entity
response = await entity_manager_stub.get_entity(GetEntityRequest(entity_id=entity_id), metadata=metadata)
# close the channel
channel.close()
return response
if __name__ == "__main__":
# Replace $ENTITY_ID with the globally unique identifier for the entity you previously published.
# If you receive an ENTITY_NOT_FOUND error, this means your conenction was successful but an entity
# with the provided ID does not exist. Check to make sure you have set the correct ID.
print(asyncio.run(get_entity("$ENTITY_ID")))
If the request is successful and you replaced the entity ID then you should see the entity object you created when setting up your development environment. If you did not change the entity ID to a valid, existing entity, then you will get the following error:
ConnectError: [not_found] entity not found ENTITY_ID
This means you successfully authenticated and completed the call to your environment, but the entity does not exist. This is an expected error. If you get any other error then there is a problem with your code. Check that your Lattice environment URL and our bearer token are correct.
What's next
- Start building and publishing entities into Lattice.
- See the Lattice sample applications.