Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Override the default endpoint

The Google Cloud client libraries for Rust automatically configure the endpoint for each service. Some applications may need to override the default endpoint either because their network has specific requirements, or because they wish to use regional versions of the service. This guide shows you how to override the default.

Prerequisites

This guide uses the Secret Manager API. The same concepts apply to the client libraries for other services. You may want to follow the service quickstart, which shows you how to enable the service.

For complete setup instructions for the Rust libraries, see Setting up your development environment.

Dependencies

You must declare the dependencies in your Cargo.toml file:

cargo add google-cloud-secretmanager-v1 google-cloud-gax

The default endpoint

First, review how to use the client libraries with the default endpoint. Add some use declarations to simplify the rest of the example:

    pub use google_cloud_gax::paginator::ItemPaginator;
    pub use google_cloud_secretmanager_v1::client::SecretManagerService;

Initialize the client using the defaults:

    let client = SecretManagerService::builder().build().await?;

Use this client as usual:

    let mut items = client
        .list_secrets()
        .set_parent(format!("projects/{project_id}"))
        .by_item();
    println!("listing all secrets in project {project_id}");
    while let Some(secret) = items.next().await.transpose()? {
        println!("  {secret:?}");
    }
    println!("DONE");

The project ID is received as a parameter:

/// # Parameters
/// - `project_id`: the id of a Google Cloud project, or its numeric ID.
///   For example: `my-project`.
pub async fn sample(project_id: &str) -> anyhow::Result<()> {

Override the default endpoint

In this example we configure the client library to use secret manager's regional endpoints. The same override can be used to configure the endpoint with one of the private access options, or for locational endpoints in the services that support them.

As before, write an example that receives the project ID and region as parameters:

/// # Parameters
/// - `project_id`: the id of a Google Cloud project, or its numeric ID.
///   For example: `my-project`.
/// - `region`: the id of a Gooogle Cloud region. For example `us-central1`.
pub async fn sample(project_id: &str, region: &str) -> anyhow::Result<()> {

Add some use declarations to simplify the code:

    pub use google_cloud_gax::paginator::ItemPaginator;
    pub use google_cloud_secretmanager_v1::client::SecretManagerService;

Initialize the client using the target endpoint:

    let client = SecretManagerService::builder()
        .with_endpoint(format!("https://secretmanager.{region}.rep.googleapis.com"))
        .build()
        .await?;

Use this client as usual:

    let mut items = client
        .list_secrets()
        .set_parent(format!("projects/{project_id}/locations/{region}"))
        .by_item();
    println!("listing all secrets in project {project_id} and region {region}");
    while let Some(secret) = items.next().await.transpose()? {
        println!("  {secret:?}");
    }
    println!("DONE");