openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Utility/PageCollectionHelpers.cs
49lines · modecode
| 1 | using System.ClientModel; |
| 2 | using System.Collections.Generic; |
| 3 | using System.Threading; |
| 4 | using System.Threading.Tasks; |
| 5 | |
| 6 | #nullable enable |
| 7 | |
| 8 | namespace OpenAI; |
| 9 | |
| 10 | internal class PageCollectionHelpers |
| 11 | { |
| 12 | public static PageCollection<T> Create<T>(PageEnumerator<T> enumerator) |
| 13 | => new EnumeratorPageCollection<T>(enumerator); |
| 14 | |
| 15 | public static AsyncPageCollection<T> CreateAsync<T>(PageEnumerator<T> enumerator) |
| 16 | => new AsyncEnumeratorPageCollection<T>(enumerator); |
| 17 | |
| 18 | private class EnumeratorPageCollection<T> : PageCollection<T> |
| 19 | { |
| 20 | private readonly PageEnumerator<T> _enumerator; |
| 21 | |
| 22 | public EnumeratorPageCollection(PageEnumerator<T> enumerator) |
| 23 | { |
| 24 | _enumerator = enumerator; |
| 25 | } |
| 26 | |
| 27 | protected override PageResult<T> GetCurrentPageCore() |
| 28 | => _enumerator.GetCurrentPage(); |
| 29 | |
| 30 | protected override IEnumerator<PageResult<T>> GetEnumeratorCore() |
| 31 | => _enumerator; |
| 32 | } |
| 33 | |
| 34 | private class AsyncEnumeratorPageCollection<T> : AsyncPageCollection<T> |
| 35 | { |
| 36 | private readonly PageEnumerator<T> _enumerator; |
| 37 | |
| 38 | public AsyncEnumeratorPageCollection(PageEnumerator<T> enumerator) |
| 39 | { |
| 40 | _enumerator = enumerator; |
| 41 | } |
| 42 | |
| 43 | protected override async Task<PageResult<T>> GetCurrentPageAsyncCore() |
| 44 | => await _enumerator.GetCurrentPageAsync().ConfigureAwait(false); |
| 45 | |
| 46 | protected override IAsyncEnumerator<PageResult<T>> GetAsyncEnumeratorCore(CancellationToken cancellationToken = default) |
| 47 | => _enumerator; |
| 48 | } |
| 49 | } |
| 50 | |