Generate Lattice SDK for HTTP
This guide shows how to generate client SDKs for the Lattice SDK in HTTP with your preferred language.
Click to go to the HTTP SDK repository in GitHub. By downloading and/or using, you agree to the Terms & Conditions. If you do not agree, do not use the SDK.
Before you begin
Before you generate and use the HTTP SDK for a language of your choice, do the following:
- Complete the setup steps to generate a valid API token and publish at least one entity to your environment.
- Install the
OpenAPI Generator.
This package allows you to generate Lattice SDKs for HTTP for your preferred
language. For more information about supported languages in OpenAPI, see the
Overview
on the
OpenAPITools
GitHub page.
Anduril is not affiliated with OpenAPI Tools.
Generate SDKs
To generate HTTP SDKs:
-
In your terminal, clone the Lattice OpenAPI specification repo:
git clone https://github.com/anduril/lattice-sdk-openapi.git lattice_sdk
cd lattice_sdk -
From the root of your local repo, generate HTTP SDKs for your client by running the following commands:
- Python
- TypeScript/JavaScript
Requires: Python 3.8+
openapi-generator generate \
-i openapi/entities_openapi.pub.yaml \
-g python \
-o lattice_sdk_py/entities_api \
--package-name=entities_api
openapi-generator generate \
-i openapi/tasks_openapi.pub.yaml \
-g python \
-o lattice_sdk_py/tasks_api \
--package-name=tasks_apiThe output is stored in
lattice_sdk/lattice_sdk_py
. If you generate SDKs successfully, you should see the following files in your folder:lattice_sdk_py/entities_api/entities_api/models/entity.py
lattice_sdk_py/tasks_api/tasks_api/models/task.py
Install both packages to your Python environment:
pip install lattice_sdk_py/entities_api
pip install lattice_sdk_py/tasks_apiConfirm packages were installed in a python shell:
python
import entities_api # successful if no returned error
import tasks_api # successful if no returned erroropenapi-generator generate \
-i openapi/entities_openapi.pub.yaml \
-g typescript \
-o lattice_sdk_ts/entities_api \
--package-name=entities_api
openapi-generator generate \
-i openapi/tasks_openapi.pub.yaml \
-g typescript \
-o lattice_sdk_ts/tasks_api \
--package-name=tasks_apiThe output is stored in
lattice_sdk/lattice_sdk_ts
. If you generate SDKs successfully, you should see the following files in your folder:lattice_sdk_ts/entities_api/models/Entity.ts
lattice_sdk_ts/tasks_api/models/Task.tsnoteYou might see
o.o.codegen.DefaultGenerator - X not generated
errors. This won’t affect the majority of use cases.
Example request
Run the following example
GetEntity
request to verify
your setup with the HTTP SDKs is complete:
- Python
- TypeScript/JavaScript
Run the example in Python:
-
Create a new file
get_entity.py
in the project directory, and add the following code:from entities_api import Configuration, ApiClient
from entities_api.models import Entity
from entities_api.api import EntityApi
def get_entity(lattice_hostname: str, bearer_token: str, entity_id: str) -> Entity:
config = Configuration(host=f"https://{lattice_hostname}/api/v1/")
api_client = ApiClient(configuration=config, header_name="Authorization", header_value=f"Bearer {bearer_token}")
entity_api = EntityApi(api_client=api_client)
response = entity_api.get_entity_by_id(entity_id=entity_id)
return response
try:
# Replace $YOUR_LATTICE_HOSTNAME and $YOUR_BEARER_TOKEN with your information.
entity = get_entity("$YOUR_LATTICE_HOSTNAME", "$YOUR_BEARER_TOKEN", "example-entity-id")
print(entity)
except Exception as error:
print(f"Lattice HTTP SDK GetEntity error {error}")Replace $YOUR_LATTICE_HOSTNAME and $YOUR_BEARER_TOKEN with your information. If you have the
entity_id
from the getting started example, replace example-entity-id. -
Run the example on the command line:
python get_entity.py
Run the example in TypeScript/JavaScript:
-
Assuming the TypeScript/JavaScript type SDKs were generated successfully, ensure that you have
npm
installed. -
Create a new directory for your example project and initialize it with npm:
mkdir my-example-project
cd my-example-project
npm init -y -
Navigate to each directory
lattice_sdk_ts/entities_api
andlattice_sdk_ts/tasks_api
and run the following command:npm install
npm run build
// create global symbolic link to each package
npm link -
Navigate back to your project directory and symbolic link it to the
entities_api
andtasks_api
module:npm link entities_api
npm link tasks_api -
Create a sample
get_entity.js
file and add the following code:const {
configureAuthMethods,
createConfiguration,
ServerConfiguration,
EntityApi,
} = require("entities_api");
async function getEntityById(hostName, token, entityId) {
const bearerTokenProvider = { getToken: () => token };
const authMethods = configureAuthMethods({
bearerHttpAuthentication: {
tokenProvider: bearerTokenProvider,
},
});
const apiConfig = createConfiguration({
baseServer: new ServerConfiguration(
"https://" + hostName + "/api/v1/",
{},
),
authMethods: authMethods,
});
const api = new EntityApi(apiConfig);
const response = await api.getEntityById(entityId);
return response;
}
async function main() {
try {
// Replace $YOUR_LATTICE_HOSTNAME and $YOUR_BEARER_TOKEN with your information.
entity = await getEntityById(
"$YOUR_LATTICE_HOSTNAME",
"$YOUR_BEARER_TOKEN",
"example-entity-id",
);
console.log(entity);
} catch (error) {
console.error(`Error: ${error.message}`);
}
}
main();Replace $YOUR_LATTICE_HOSTNAME and $YOUR_BEARER_TOKEN with your information. If you have the
entity_id
from the getting started example, replace example-entity-id. -
Run the example on the command line:
bash node get_entity.js
If the request is successful and you specified a valid entity-id
you should
see the entity data. Alternatively, if you use example-entity-id
, you get an
expected 404 NOT_FOUND_ERROR
. This means you successfully authenticated and
completed the call to your environment, but the entity does not exist.
What's next
- Start building and publishing entities into Lattice.
- See the Lattice sample applications.