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

Avoid async in autopagination when NET461 #2239

Merged
merged 1 commit into from
Oct 29, 2020
Merged
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
74 changes: 74 additions & 0 deletions src/Stripe.net/Services/_base/Service.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,84 @@ protected IEnumerable<T> ListRequestAutoPaging<T>(
RequestOptions requestOptions)
where T : IStripeEntity
{
#if NET461
return
this.ListRequestAutoPagingSync<T>(url, options, requestOptions);
#else
return AsyncUtils.ToEnumerable(
this.ListRequestAutoPagingAsync<T>(url, options, requestOptions));
#endif
}

#if NET461
protected IEnumerable<T> ListRequestAutoPagingSync<T>(
string url,
ListOptions options,
RequestOptions requestOptions)
where T : IStripeEntity
{
var page = this.Request<StripeList<T>>(
HttpMethod.Get,
url,
options,
requestOptions);

options = options ?? new ListOptions();
bool iterateBackward = false;

// Backward iterating activates if we have an `EndingBefore`
// constraint and not a `StartingAfter` constraint
if (!string.IsNullOrEmpty(options.EndingBefore) && string.IsNullOrEmpty(options.StartingAfter))
{
iterateBackward = true;
}

while (true)
{
if (iterateBackward)
{
page.Reverse();
}

string itemId = null;
foreach (var item in page)
{
// Elements in `StripeList` instances are decoded by `StripeObjectConverter`,
// which returns `null` for objects it doesn't know how to decode.
// When auto-paginating, we simply ignore these null elements but still return
// other elements.
if (item == null)
{
continue;
}

itemId = ((IHasId)item).Id;
yield return item;
}

if (!page.HasMore || string.IsNullOrEmpty(itemId))
{
break;
}

if (iterateBackward)
{
options.EndingBefore = itemId;
}
else
{
options.StartingAfter = itemId;
}

page = this.Request<StripeList<T>>(
HttpMethod.Get,
url,
options,
requestOptions);
}
}

#endif
protected async IAsyncEnumerable<T> ListRequestAutoPagingAsync<T>(
string url,
ListOptions options,
Expand Down