Inizializzare un client

Le librerie client di Cloud per Rust utilizzano le strutture client come interfaccia principale per i serviziGoogle Cloud , con metodi corrispondenti a ogni RPC offerta dal servizio. Prima di chiamare un servizio, inizializza il relativo client.

Questo documento mostra come inizializzare un client Secret Manager con la configurazione predefinita e chiamare un RPC. Lo stesso schema si applica a tutti gli altri servizi Google Cloud .

Prima di iniziare

Prima di eseguire il codice in questo documento, completa i seguenti prerequisiti:

Dipendenze

Aggiungi i crate richiesti al file Cargo.toml:

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

Inizializza un client ed esegui una RPC

Per inizializzare un client, chiama builder sulla struttura del client per ottenere un ClientBuilder e poi chiama build. Completa i seguenti passaggi per inizializzare un client con le impostazioni predefinite e chiamare un RPC:

  1. Importa il tipo di client SecretManagerService:

    use google_cloud_secretmanager_v1::client::SecretManagerService;

  2. Crea il client con la configurazione predefinita:

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

  3. Utilizza il client inizializzato per chiamare list_locations, che elenca le località supportate da Secret Manager per il tuo progetto:

    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);
        }
    }

L'output è simile al seguente:

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

In questo output, PROJECT_NUMBER è il numero di progetto numerico per il tuo progetto Google Cloud .

Completamento del codice

L'esempio seguente combina l'inizializzazione del client e la chiamata list_locations in un programma eseguibile:

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
}