Skip to content

Commit d72a9a3

Browse files
Merge pull request #2239 from stripe/richardm-support-webforms
Avoid async in autopagination when NET461
2 parents 38362c4 + 063e666 commit d72a9a3

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

src/Stripe.net/Services/_base/Service.cs

+74
Original file line numberDiff line numberDiff line change
@@ -263,10 +263,84 @@ protected IEnumerable<T> ListRequestAutoPaging<T>(
263263
RequestOptions requestOptions)
264264
where T : IStripeEntity
265265
{
266+
#if NET461
267+
return
268+
this.ListRequestAutoPagingSync<T>(url, options, requestOptions);
269+
#else
266270
return AsyncUtils.ToEnumerable(
267271
this.ListRequestAutoPagingAsync<T>(url, options, requestOptions));
272+
#endif
268273
}
269274

275+
#if NET461
276+
protected IEnumerable<T> ListRequestAutoPagingSync<T>(
277+
string url,
278+
ListOptions options,
279+
RequestOptions requestOptions)
280+
where T : IStripeEntity
281+
{
282+
var page = this.Request<StripeList<T>>(
283+
HttpMethod.Get,
284+
url,
285+
options,
286+
requestOptions);
287+
288+
options = options ?? new ListOptions();
289+
bool iterateBackward = false;
290+
291+
// Backward iterating activates if we have an `EndingBefore`
292+
// constraint and not a `StartingAfter` constraint
293+
if (!string.IsNullOrEmpty(options.EndingBefore) && string.IsNullOrEmpty(options.StartingAfter))
294+
{
295+
iterateBackward = true;
296+
}
297+
298+
while (true)
299+
{
300+
if (iterateBackward)
301+
{
302+
page.Reverse();
303+
}
304+
305+
string itemId = null;
306+
foreach (var item in page)
307+
{
308+
// Elements in `StripeList` instances are decoded by `StripeObjectConverter`,
309+
// which returns `null` for objects it doesn't know how to decode.
310+
// When auto-paginating, we simply ignore these null elements but still return
311+
// other elements.
312+
if (item == null)
313+
{
314+
continue;
315+
}
316+
317+
itemId = ((IHasId)item).Id;
318+
yield return item;
319+
}
320+
321+
if (!page.HasMore || string.IsNullOrEmpty(itemId))
322+
{
323+
break;
324+
}
325+
326+
if (iterateBackward)
327+
{
328+
options.EndingBefore = itemId;
329+
}
330+
else
331+
{
332+
options.StartingAfter = itemId;
333+
}
334+
335+
page = this.Request<StripeList<T>>(
336+
HttpMethod.Get,
337+
url,
338+
options,
339+
requestOptions);
340+
}
341+
}
342+
343+
#endif
270344
protected async IAsyncEnumerable<T> ListRequestAutoPagingAsync<T>(
271345
string url,
272346
ListOptions options,

0 commit comments

Comments
 (0)