From 063e666653a54285536799aa9a80aea80836340e Mon Sep 17 00:00:00 2001 From: Richard Marmorstein Date: Thu, 29 Oct 2020 16:25:48 -0400 Subject: [PATCH] Avoid async when NET461 --- src/Stripe.net/Services/_base/Service.cs | 74 ++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/src/Stripe.net/Services/_base/Service.cs b/src/Stripe.net/Services/_base/Service.cs index d5f43e036b..640de6b9bc 100644 --- a/src/Stripe.net/Services/_base/Service.cs +++ b/src/Stripe.net/Services/_base/Service.cs @@ -263,10 +263,84 @@ protected IEnumerable ListRequestAutoPaging( RequestOptions requestOptions) where T : IStripeEntity { +#if NET461 + return + this.ListRequestAutoPagingSync(url, options, requestOptions); +#else return AsyncUtils.ToEnumerable( this.ListRequestAutoPagingAsync(url, options, requestOptions)); +#endif } +#if NET461 + protected IEnumerable ListRequestAutoPagingSync( + string url, + ListOptions options, + RequestOptions requestOptions) + where T : IStripeEntity + { + var page = this.Request>( + 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>( + HttpMethod.Get, + url, + options, + requestOptions); + } + } + +#endif protected async IAsyncEnumerable ListRequestAutoPagingAsync( string url, ListOptions options,