Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Adding get_all() utility function on streaming paginator #655

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@

pub trait AsCursor: AsRef<str> {}

impl<'a> AsCursor for &'a str {}

Check failure on line 164 in src/params.rs

View workflow job for this annotation

GitHub Actions / clippy (tokio-hyper-rustls-webpki)

the following explicit lifetimes could be elided: 'a

Check failure on line 164 in src/params.rs

View workflow job for this annotation

GitHub Actions / clippy (tokio-hyper)

the following explicit lifetimes could be elided: 'a

Check failure on line 164 in src/params.rs

View workflow job for this annotation

GitHub Actions / clippy (tokio-hyper-rustls)

the following explicit lifetimes could be elided: 'a

Check failure on line 164 in src/params.rs

View workflow job for this annotation

GitHub Actions / clippy (async-std-surf)

the following explicit lifetimes could be elided: 'a

Check failure on line 164 in src/params.rs

View workflow job for this annotation

GitHub Actions / clippy (blocking)

the following explicit lifetimes could be elided: 'a

Check failure on line 164 in src/params.rs

View workflow job for this annotation

GitHub Actions / clippy (blocking-rustls-webpki)

the following explicit lifetimes could be elided: 'a

Check failure on line 164 in src/params.rs

View workflow job for this annotation

GitHub Actions / clippy (blocking-rustls)

the following explicit lifetimes could be elided: 'a
impl AsCursor for String {}

impl<T> Paginate for T
Expand Down Expand Up @@ -321,7 +321,14 @@
/// Repeatedly queries Stripe for more data until all elements in list are fetched, using
/// Stripe's default page size.
///
/// Requires `feature = "blocking"`.
/// Requires `feature = "blocking"`. There is also an async version of this function available with the same name.
///
/// ```no_run
/// use stripe::{Customer, ListCustomers, StripeError, Client};
/// let client = Client::new("sk_test_123");
/// let params = ListCustomers { ..Default::default() };
/// let all_customers = Customer::list(&client, &params).unwrap().paginate(params).get_all(&client).unwrap()
/// ```
#[cfg(feature = "blocking")]
pub fn get_all(self, client: &Client) -> Response<Vec<T::O>> {
let mut data = Vec::with_capacity(self.page.get_total_count().unwrap_or(0) as usize);
Expand All @@ -338,6 +345,27 @@
Ok(data)
}

/// Get all values in this list, consuming self and paginating until all values are fetched.
///
/// This function repeatedly queries Stripe for more data until all elements in list are fetched, using
/// the page size specified in params, or Stripe's default page size if none is specified.
///
/// Requires `feature = "stream"`. There is also a blocking version of this function available with the same name.
///
/// ```no_run
/// use stripe::{Customer, ListCustomers, StripeError, Client};
/// let client = Client::new("sk_test_123");
/// let params = ListCustomers { ..Default::default() };
/// let all_customers = Customer::list(&client, &params).await.unwrap().paginate(params).get_all(&client).await.unwrap()
/// ```
#[cfg(all(feature = "async", feature = "stream", not(feature = "blocking")))]
pub async fn get_all(self, client: &Client) -> Result<Vec<T::O>, StripeError> {
use futures_util::TryStreamExt;

let stream = self.stream(&client);
stream.try_collect::<Vec<_>>().await
}

/// Get all values in this List, consuming self and lazily paginating until all values are fetched.
///
/// This function repeatedly queries Stripe for more data until all elements in list are fetched, using
Expand Down
Loading