The Cloud Client Libraries for Rust use client structs as the main interface to
Google Cloud services, with methods corresponding to each RPC offered by the
service. Before calling a service, you initialize its client.

This document shows you how to initialize a Secret Manager client with
its default configuration and call an RPC. The same pattern applies to all
other Google Cloud services.

## Before you begin

Before running the code in this document, complete the following prerequisites:

- Follow [Create and access a secret using
  Secret Manager](https://docs.cloud.google.com/secret-manager/docs/create-secret-quickstart) to set up a Google Cloud project and enable the Secret Manager API.
- Configure [Application Default Credentials](https://docs.cloud.google.com/docs/authentication/provide-credentials-adc.md.txt) for your local environment. For more information, see [Authenticate with client libraries](https://docs.cloud.google.com/docs/authentication/client-libraries.md.txt).

### Dependencies

Add the required crates to your `Cargo.toml` file:

```bash
cargo add google-cloud-secretmanager-v1 google-cloud-gax anyhow
cargo add tokio --features macros,rt-multi-thread
```

## Initialize a client and make an RPC

To initialize a client, call `builder` on the client struct to obtain a
[`ClientBuilder`](https://docs.rs/google-cloud-gax/latest/google_cloud_gax/client_builder/struct.ClientBuilder.html) and then call `build`. Complete the following
steps to initialize a client with default settings and call an RPC:

1. Import the `SecretManagerService` client type:


   ```rust
   use google_cloud_secretmanager_v1::client::SecretManagerService;
   ```

   <br />

2. Build the client with its default configuration:


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

   <br />

3. Use the initialized client to call `list_locations`, which lists the
   locations supported by Secret Manager for your project:


   ```rust
   use google_cloud_gax::paginator::Paginator as _;
   let mut items = client
       .list_locations()
       .set_name(format!("projects/{project_id}"))
       .by_page();
   while let Some(page) = items.next().await {
       let page = page?;
       for location in page.locations {
           println!("{}", location.name);
       }
   }
   ```

   <br />

The output resembles the following:

```text
projects/PROJECT_NUMBER/locations/europe-west8
projects/PROJECT_NUMBER/locations/europe-west9
projects/PROJECT_NUMBER/locations/us-east5
```

In this output, `PROJECT_NUMBER` is the numeric project
number for your Google Cloud project.

## Complete code

The following example combines client initialization and the `list_locations`
call into a runnable program:

```rust
pub async fn initialize_client(project_id: &str) -> anyhow::Result<()> {
    use google_cloud_secretmanager_v1::client::SecretManagerService;

    // Initialize a client with the default configuration. This is an
    // asynchronous operation that may fail, as it requires acquiring an an
    // access token.
    let client = SecretManagerService::builder().build().await?;

    // Once initialized, use the client to make requests.
    use google_cloud_gax::paginator::Paginator as _;
    let mut items = client
        .list_locations()
        .set_name(format!("projects/{project_id}"))
        .by_page();
    while let Some(page) = items.next().await {
        let page = page?;
        for location in page.locations {
            println!("{}", location.name);
        }
    }

    Ok(())
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let project_id = std::env::args().nth(1).unwrap();

    initialize_client(&project_id).await
}
```