How to initialize a client
The Google Cloud Client Libraries for Rust use clients as the main abstraction to interface with specific services. Clients are implemented as Rust structs, with methods corresponding to each RPC offered by the service. To use a Google Cloud service using the Rust client libraries, you need to first initialize a client.
Prerequisites
In this guide you'll initialize a client and then use the client to make a simple RPC. For this tutorial, you'll use the Secret Manager API. The same structure applies to any other service in Google Cloud.
We recommend that you follow one of the Secret Manager getting started guides, such as how to Create a secret, before attempting to use the client library. These guides cover service specific concepts in more detail, and provide detailed guidance on project prerequisites.
We also recommend that you follow the instructions in the Authenticate for using client libraries guide. This guide will show you how to log in to configure the Application Default Credentials used in this guide.
Dependencies
As usual with Rust, you must declare the dependency in your Cargo.toml
file:
cargo add google-cloud-secretmanager-v1
To initialize a client, you first call Client::builder()
to obtain an
appropriate ClientBuilder
and then call build()
on
that builder to create a client.
The following creates a client with the default configuration, which is designed to meet requirements for most use cases.
let client = SecretManagerService::builder().build().await?;
Once the client is successfully initialized, you can use it to make RPCs:
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);
}
}
This example shows a call to list_locations
, which returns information about
the supported locations for the service (in this case, Secret Manager). The
output of the example should look something like this:
projects/123456789012/locations/europe-west8
projects/123456789012/locations/europe-west9
projects/123456789012/locations/us-east5
...
Full program
Putting all this code together into a full program looks as follows:
pub type Result = std::result::Result<(), Box<dyn std::error::Error>>;
pub async fn initialize_client(project_id: &str) -> 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(())
}
What's next
This guide showed you how to initialize a client using the Google Cloud Client Libraries for Rust. For a more complex example of working with a service, check out Generate text using the Vertex AI Gemini API.