Use the Lattice SDK for gRPC in C++
This page shows you how to set up theLattice SDK for gRPC in C++ and make a gRPC request.
Click to go to the gRPC SDK repository. 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
Before you install and use the Lattice C++ SDK, do the following:
- Complete the set up steps to generate a valid API token.
- Install latest CMake.
- Install the required versions of gRPC and Protobuf.
Get the example files
-
Run the following command to create, and navigate to, a new project folder
mkdir lattice-sdk-example && cd lattice-sdk-example
-
Copy and paste the following examples into your C++ project.
CMakeLists.txt
warningUse the latest version of GIT_TAG to avoid issues from gRPC or Protobuf dependency updates, as it includes the specific versions used for building.
cmake_minimum_required(VERSION 3.25.0)
project(lattice-sdk-example)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# Download the SDK from github and add it as part of the project
include(FetchContent)
FetchContent_Declare(
lattice-sdk-cpp
GIT_REPOSITORY https://github.com/anduril/lattice-sdk-cpp.git
GIT_TAG v1.0.0
)
FetchContent_MakeAvailable(lattice-sdk-cpp)
# Build the text-only CLI and link against the Lattice SDK.
add_executable(cli_client main.cpp)
target_link_libraries(cli_client lattice-sdk-cpp)main.cpp
#include <grpc/grpc.h>
#include <grpcpp/channel.h>
#include <grpcpp/create_channel.h>
#include <grpcpp/security/credentials.h>
#include <grpcpp/security/tls_credentials_options.h>
#include <anduril/entitymanager/v1/entity_manager_grpcapi.pub.grpc.pb.h>
int main(int argc, char *argv[]) {
GOOGLE_PROTOBUF_VERIFY_VERSION;
// Set your Lattice environment URL
auto url = "$YOUR_LATTICE_URL";
// Set authorization using your bearer token
grpc::ClientContext ctx;
ctx.AddMetadata("authorization", "Bearer $YOUR_BEARER_TOKEN");
// Open a secure channel
auto creds = grpc::SslCredentials(grpc::SslCredentialsOptions());
std::shared_ptr<grpc::Channel> channel = grpc::CreateChannel(url, creds);
// Create a new stub for the service instance
std::shared_ptr<anduril::entitymanager::v1::EntityManagerAPI::Stub> mStub =
anduril::entitymanager::v1::EntityManagerAPI::NewStub(channel);
// Define the request and response objects
anduril::entitymanager::v1::GetEntityRequest req;
// Replace ENTITY_ID with any active entity ID in your environment
req.set_entity_id("$ENTITY_ID");
anduril::entitymanager::v1::GetEntityResponse res;
// Get the entity
grpc::Status status = mStub->GetEntity(&ctx, req, &res);
if(!status.ok()) {
std::cerr << "Error code: " << status.error_code() << std::endl;
std::cerr << "Error message: " << status.error_message() << std::endl;
} else {
std::cout << res.entity().DebugString() << std::endl;
}
// Automatically closed the channel when out of scope
return 0;
}infoIn
main.cpp
, replace$YOUR_LATTICE_URL
and$YOUR_BEARER_TOKEN
with your information. Replace$ENTITY_ID
with the entity you published when you previously set up your development environment.
Install the SDK
Install the gRPC SDK in C++:
- Create and navigate into a new
build
folder in thelattice-sdk-example
project.mkdir build && cd build/
- Use
cmake
to install the Lattice C++ SDK, as configured inCMakeLists.txt
.If successful, this command installs the dependencies, including the Lattice SDK.cmake ..
Run the application
To build and run the application, do the following:
-
Inside
build/
, build the project.make
If successful, the
make
command compiles the project and generates an executable calledcli_client
. -
Run the executable to connect to your Lattice environment.
./cli_client
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.
How it works
In main.cpp
, you implement a program that using the gRPC framework to call the Lattice GetEntity
API.
To do this, first you declare a variable for your Lattice environment URL. You use gRPC's ClientContext
interface and set the unique bearer token you generated during
set up. You then create a secure gRPC channel and supply your credentials.
// Set your Lattice environment URL
auto url = "$YOUR_LATTICE_URL";
grpc::ClientContext ctx;
ctx.AddMetadata("authorization", "Bearer $YOUR_BEARER_TOKEN"); // Replace $YOUR_BEARER_TOKEN with your token. You must keep the word, Bearer, in the metadata string.
// Open a secure channel
auto creds = grpc::SslCredentials(grpc::SslCredentialsOptions());
std::shared_ptr<grpc::Channel> channel = grpc::CreateChannel(url, creds);
Then, you create a new service stub mStub
. mStub
is a client-side RPC stub that provides an interface
for your application to interact with the Lattice API.
// Create a new stub for the service instance
std::shared_ptr<anduril::entitymanager::v1::EntityManagerAPI::Stub> mStub =
anduril::entitymanager::v1::EntityManagerAPI::NewStub(channel);
You declare the request and response objects, set your entity ID, and finally, call the GetEntity
API.
// Replace $ENTITY_ID with any existing entity ID in your environment
req.set_entity_id("$ENTITY_ID");
anduril::entitymanager::v1::GetEntityResponse res;
// Get the entity
grpc::Status status = mStub->GetEntity(&ctx, req, &res);
Allow self-signed certificates
Make sure you understand the implications of using self-signed certificates. This should only be used for testing.
In order to connect to a server that is using a self-signed certificate, replace your gRPC channel declaration as shown in the following:
with the following code:
// Add the following include statement to the top of the file.
+ #include <grpcpp/security/tls_credentials_options.h>
.
.
.
// Remove the following statements.
- auto creds = grpc::SslCredentials(grpc::SslCredentialsOptions());
- std::shared_ptr<grpc::Channel> channel = grpc::CreateChannel(url, creds);
+
// Replace with the following
+ grpc::experimental::TlsChannelCredentialsOptions authOptions;
+ authOptions.set_verify_server_certs(false);
+
+ auto verifier = std::make_shared<grpc::experimental::NoOpCertificateVerifier>();
+ authOptions.set_certificate_verifier(verifier);
+ authOptions.set_check_call_host(false);
+
+ std::shared_ptr<grpc::Channel> channel = grpc::CreateChannel(url, grpc::experimental::TlsCredentials(authOptions));
What's next
- Start building and publishing entities into Lattice.
- See the Lattice sample applications.