Skip to content

anduril/lattice-sdk-go

Repository files navigation

Lattice SDK Go library

The Lattice SDK Go library provides convenient access to the Lattice API from Go.

Documentation

API reference documentation is available here.

Requirements

To use the SDK please ensure you have the following installed:

Installation

Install the Lattice SDK package with

go get github.com/anduril/lattice-sdk-go/v2

Support

For support with this library please reach out to your Anduril representative.

Usage

Instantiate and use the client with the following:

package example

import (
    client "github.com/anduril/lattice-sdk-go/v2/client"
    option "github.com/anduril/lattice-sdk-go/v2/option"
    context "context"
    Lattice "github.com/anduril/lattice-sdk-go/v2"
)

func do() {
    client := client.NewClient(
        option.WithToken(
            "<token>",
        ),
    )
    client.Entities.LongPollEntityEvents(
        context.TODO(),
        &Lattice.EntityEventRequest{
            SessionToken: "sessionToken",
        },
    )
}

Environments

You can choose between different environments by using the option.WithBaseURL option. You can configure any arbitrary base URL, which is particularly useful in test environments.

client := client.NewClient(
    option.WithBaseURL(Lattice.Environments.Default),
)

Errors

Structured error types are returned from API calls that return non-success status codes. These errors are compatible with the errors.Is and errors.As APIs, so you can access the error like so:

response, err := client.Entities.LongPollEntityEvents(...)
if err != nil {
    var apiError *core.APIError
    if errors.As(err, apiError) {
        // Do something with the API error ...
    }
    return err
}

Request Options

A variety of request options are included to adapt the behavior of the library, which includes configuring authorization tokens, or providing your own instrumented *http.Client.

These request options can either be specified on the client so that they're applied on every request, or for an individual request, like so:

Providing your own *http.Client is recommended. Otherwise, the http.DefaultClient will be used, and your client will wait indefinitely for a response (unless the per-request, context-based timeout is used).

// Specify default options applied on every request.
client := client.NewClient(
    option.WithToken("<YOUR_API_KEY>"),
    option.WithHTTPClient(
        &http.Client{
            Timeout: 5 * time.Second,
        },
    ),
)

// Specify options for an individual request.
response, err := client.Entities.LongPollEntityEvents(
    ...,
    option.WithToken("<YOUR_API_KEY>"),
)

Advanced

Response Headers

You can access the raw HTTP response data by using the WithRawResponse field on the client. This is useful when you need to examine the response headers received from the API call.

response, err := client.Entities.WithRawResponse.LongPollEntityEvents(...)
if err != nil {
    return err
}
fmt.Printf("Got response headers: %v", response.Header)

Retries

The SDK is instrumented with automatic retries with exponential backoff. A request will be retried as long as the request is deemed retryable and the number of retry attempts has not grown larger than the configured retry limit (default: 2).

A request is deemed retryable when any of the following HTTP status codes is returned:

  • 408 (Timeout)
  • 429 (Too Many Requests)
  • 5XX (Internal Server Errors)

Use the option.WithMaxAttempts option to configure this behavior for the entire client or an individual request:

client := client.NewClient(
    option.WithMaxAttempts(1),
)

response, err := client.Entities.LongPollEntityEvents(
    ...,
    option.WithMaxAttempts(1),
)

Timeouts

Setting a timeout for each individual request is as simple as using the standard context library. Setting a one second timeout for an individual API call looks like the following:

ctx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()

response, err := client.Entities.LongPollEntityEvents(ctx, ...)

About

Anduril's Lattice Go SDK

Topics

Resources

License

Security policy

Stars

Watchers

Forks

Contributors 8

Languages