Some services return potentially large lists of items, such as rows or resource
descriptions. To keep CPU and memory usage under control, services return these
resources in pages: smaller subsets of the items with a continuation token to
request the next subset.
Iterating over items by page can be tedious. The client libraries provide adapters to convert the pages into asynchronous iterators. This document shows you how to work with these adapters.
Prerequisites
This document uses the Secret Manager service to demonstrate list operations. The concepts included in this document also apply to other services.
To follow this document, you'll need to enable the Secret Manager service, sign in, and ensure that your account has the necessary permissions. For guidance on how to fulfill these prerequisites, follow the service quickstart.
For complete setup instructions for the Rust libraries, see Setting up your development environment.
Dependencies
Add the Secret Manager library to your Cargo.toml file:
cargo add google-cloud-secretmanager-v1
Iterating list methods
To help iterate the items in a list method, the APIs return an implementation of
the ItemPaginator trait. Introduce the trait into scope using a use
declaration:
To iterate over the items, use the by_item method.
In rare cases, pages might contain extra information that you need access to, or you might need to checkpoint your progress across processes. If needed, you can iterate over full pages instead of individual items.
Introduce
Paginatorinto scope using ausedeclaration:Iterate over the pages using
by_page:
Working with futures::Stream
You might want to use these APIs in the larger Rust ecosystem of asynchronous
streams, such as tokio::Stream. To do this, follow these steps:
Enable the
unstable-streamfeature in thegoogle-cloud-gaxcrate. The name of this feature serves as notice that these APIs are unstable. You should only use them if you're prepared to deal with any breaks that result from incompatible changes to thefutures::Streamtrait.cargo add google-cloud-gax --features unstable-streamThe following examples also use the
futures::stream::StreamExttrait, which you enable by adding thefuturescrate.cargo add futuresAdd the required
usedeclarations:Use the
into_streamfunction to convertItemPaginatorinto afutures::Stream.
Similarly, you can use the into_stream function to convert Paginator into a
futures::Stream.
Resuming list methods by setting next page token
In some cases, such as an interrupted list operation, you can set the next page token to resume paginating from a specific page.
When to use the paginator helpers
The Google Cloud Client Libraries for Rust provide an adapter that converts
list RPCs defined by AIP-4233 into types implementing ItemPaginator and
Paginator. This adapter applies when the Google API List method follows the
AIP-158 pagination guideline. In brief, this guideline requires each List
call to return a page of resource items (such as secrets) and a continuation
token. You can pass that token to the List method to retrieve the next page.
Most Google Cloud services follow these guidelines. In the cases in which they don't, you must implement your own adapter to iterate over the results.