openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Custom/Common/PageableResultHelpers.cs
93lines · modecode
| 1 | using System; |
| 2 | using System.ClientModel; |
| 3 | using System.Collections.Generic; |
| 4 | using System.Threading.Tasks; |
| 5 | |
| 6 | #nullable enable |
| 7 | |
| 8 | namespace OpenAI; |
| 9 | |
| 10 | internal class PageableResultHelpers |
| 11 | { |
| 12 | public static PageableCollection<T> Create<T>(Func<int?, ResultPage<T>> firstPageFunc, Func<string?, int?, ResultPage<T>>? nextPageFunc, int? pageSize = default) where T : notnull |
| 13 | { |
| 14 | ResultPage<T> first(string? _, int? pageSizeHint) => firstPageFunc(pageSizeHint); |
| 15 | return new FuncPageable<T>(first, nextPageFunc, pageSize); |
| 16 | } |
| 17 | |
| 18 | public static AsyncPageableCollection<T> Create<T>(Func<int?, Task<ResultPage<T>>> firstPageFunc, Func<string?, int?, Task<ResultPage<T>>>? nextPageFunc, int? pageSize = default) where T : notnull |
| 19 | { |
| 20 | Task<ResultPage<T>> first(string? _, int? pageSizeHint) => firstPageFunc(pageSizeHint); |
| 21 | return new FuncAsyncPageable<T>(first, nextPageFunc, pageSize); |
| 22 | } |
| 23 | |
| 24 | private class FuncAsyncPageable<T> : AsyncPageableCollection<T> where T : notnull |
| 25 | { |
| 26 | private readonly Func<string?, int?, Task<ResultPage<T>>> _firstPageFunc; |
| 27 | private readonly Func<string?, int?, Task<ResultPage<T>>>? _nextPageFunc; |
| 28 | private readonly int? _defaultPageSize; |
| 29 | |
| 30 | public FuncAsyncPageable(Func<string?, int?, Task<ResultPage<T>>> firstPageFunc, Func<string?, int?, Task<ResultPage<T>>>? nextPageFunc, int? defaultPageSize = default) |
| 31 | { |
| 32 | _firstPageFunc = firstPageFunc; |
| 33 | _nextPageFunc = nextPageFunc; |
| 34 | _defaultPageSize = defaultPageSize; |
| 35 | } |
| 36 | |
| 37 | public override async IAsyncEnumerable<ResultPage<T>> AsPages(string? continuationToken = default, int? pageSizeHint = default) |
| 38 | { |
| 39 | Func<string?, int?, Task<ResultPage<T>>>? pageFunc = string.IsNullOrEmpty(continuationToken) ? _firstPageFunc : _nextPageFunc; |
| 40 | |
| 41 | if (pageFunc == null) |
| 42 | { |
| 43 | yield break; |
| 44 | } |
| 45 | |
| 46 | int? pageSize = pageSizeHint ?? _defaultPageSize; |
| 47 | do |
| 48 | { |
| 49 | ResultPage<T> page = await pageFunc(continuationToken, pageSize).ConfigureAwait(false); |
| 50 | SetRawResponse(page.GetRawResponse()); |
| 51 | yield return page; |
| 52 | continuationToken = page.ContinuationToken; |
| 53 | pageFunc = _nextPageFunc; |
| 54 | } |
| 55 | while (!string.IsNullOrEmpty(continuationToken) && pageFunc != null); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | private class FuncPageable<T> : PageableCollection<T> where T : notnull |
| 60 | { |
| 61 | private readonly Func<string?, int?, ResultPage<T>> _firstPageFunc; |
| 62 | private readonly Func<string?, int?, ResultPage<T>>? _nextPageFunc; |
| 63 | private readonly int? _defaultPageSize; |
| 64 | |
| 65 | public FuncPageable(Func<string?, int?, ResultPage<T>> firstPageFunc, Func<string?, int?, ResultPage<T>>? nextPageFunc, int? defaultPageSize = default) |
| 66 | { |
| 67 | _firstPageFunc = firstPageFunc; |
| 68 | _nextPageFunc = nextPageFunc; |
| 69 | _defaultPageSize = defaultPageSize; |
| 70 | } |
| 71 | |
| 72 | public override IEnumerable<ResultPage<T>> AsPages(string? continuationToken = default, int? pageSizeHint = default) |
| 73 | { |
| 74 | Func<string?, int?, ResultPage<T>>? pageFunc = string.IsNullOrEmpty(continuationToken) ? _firstPageFunc : _nextPageFunc; |
| 75 | |
| 76 | if (pageFunc == null) |
| 77 | { |
| 78 | yield break; |
| 79 | } |
| 80 | |
| 81 | int? pageSize = pageSizeHint ?? _defaultPageSize; |
| 82 | do |
| 83 | { |
| 84 | ResultPage<T> page = pageFunc(continuationToken, pageSize); |
| 85 | SetRawResponse(page.GetRawResponse()); |
| 86 | yield return page; |
| 87 | continuationToken = page.ContinuationToken; |
| 88 | pageFunc = _nextPageFunc; |
| 89 | } |
| 90 | while (!string.IsNullOrEmpty(continuationToken) && pageFunc != null); |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |