Override the default endpoint

The Rust client libraries 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 need to use regional versions of the service. This guide shows you how to override the default.

Prerequisites

This guide uses the Secret Manager API. To enable this API, follow the service quickstart.

For complete setup instructions for the Rust client libraries, see Getting started with Rust.

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. Start by writing some use declarations to simplify the rest of the example:

  1. Write the example in a function that receives the project ID 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<()> {

  2. Add some use declarations simplify the example code:

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

  3. Initialize the client using the defaults:

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

  4. And 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");

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.

  1. 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<()> {

  2. Add some use declarations to simplify the code:

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

  3. Initialize the client using the target endpoint:

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

  4. And 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");