| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | #nullable enable |
| | 5 | |
|
| | 6 | | using System; |
| | 7 | | using System.Collections.Generic; |
| | 8 | | using System.Threading.Tasks; |
| | 9 | |
|
| | 10 | | namespace Azure.Core |
| | 11 | | { |
| | 12 | | internal static class PageResponseEnumerator |
| | 13 | | { |
| | 14 | | public static FuncPageable<T> CreateEnumerable<T>(Func<string?, Page<T>> pageFunc) where T : notnull |
| | 15 | | { |
| 0 | 16 | | return new FuncPageable<T>((continuationToken, pageSizeHint) => pageFunc(continuationToken)); |
| | 17 | | } |
| | 18 | |
|
| | 19 | | public static FuncPageable<T> CreateEnumerable<T>(Func<string?, int?, Page<T>> pageFunc) where T : notnull |
| | 20 | | { |
| 0 | 21 | | return new FuncPageable<T>(pageFunc); |
| | 22 | | } |
| | 23 | |
|
| | 24 | | public static AsyncPageable<T> CreateAsyncEnumerable<T>(Func<string?, Task<Page<T>>> pageFunc) where T : notnull |
| | 25 | | { |
| 40 | 26 | | return new FuncAsyncPageable<T>((continuationToken, pageSizeHint) => pageFunc(continuationToken)); |
| | 27 | | } |
| | 28 | |
|
| | 29 | | public static AsyncPageable<T> CreateAsyncEnumerable<T>(Func<string?, int?, Task<Page<T>>> pageFunc) where T : n |
| | 30 | | { |
| 0 | 31 | | return new FuncAsyncPageable<T>(pageFunc); |
| | 32 | | } |
| | 33 | |
|
| | 34 | | internal class FuncAsyncPageable<T> : AsyncPageable<T> where T : notnull |
| | 35 | | { |
| | 36 | | private readonly Func<string?, int?, Task<Page<T>>> _pageFunc; |
| | 37 | |
|
| 20 | 38 | | public FuncAsyncPageable(Func<string?, int?, Task<Page<T>>> pageFunc) |
| | 39 | | { |
| 20 | 40 | | _pageFunc = pageFunc; |
| 20 | 41 | | } |
| | 42 | |
|
| | 43 | | public override async IAsyncEnumerable<Page<T>> AsPages(string? continuationToken = default, int? pageSizeHi |
| | 44 | | { |
| | 45 | | do |
| | 46 | | { |
| 20 | 47 | | Page<T> pageResponse = await _pageFunc(continuationToken, pageSizeHint).ConfigureAwait(false); |
| 20 | 48 | | yield return pageResponse; |
| 20 | 49 | | continuationToken = pageResponse.ContinuationToken; |
| 40 | 50 | | } while (continuationToken != null); |
| 20 | 51 | | } |
| | 52 | | } |
| | 53 | |
|
| | 54 | | internal class FuncPageable<T> : Pageable<T> where T : notnull |
| | 55 | | { |
| | 56 | | private readonly Func<string?, int?, Page<T>> _pageFunc; |
| | 57 | |
|
| 0 | 58 | | public FuncPageable(Func<string?, int?, Page<T>> pageFunc) |
| | 59 | | { |
| 0 | 60 | | _pageFunc = pageFunc; |
| 0 | 61 | | } |
| | 62 | |
|
| | 63 | | public override IEnumerable<Page<T>> AsPages(string? continuationToken = default, int? pageSizeHint = defaul |
| | 64 | | { |
| | 65 | | do |
| | 66 | | { |
| 0 | 67 | | Page<T> pageResponse = _pageFunc(continuationToken, pageSizeHint); |
| 0 | 68 | | yield return pageResponse; |
| 0 | 69 | | continuationToken = pageResponse.ContinuationToken; |
| 0 | 70 | | } while (continuationToken != null); |
| 0 | 71 | | } |
| | 72 | | } |
| | 73 | | } |
| | 74 | | } |