openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Utility/PageEnumerator.cs
60lines · modecode
| 1 | using System.ClientModel; |
| 2 | using System.Collections.Generic; |
| 3 | using System.Threading.Tasks; |
| 4 | |
| 5 | #nullable enable |
| 6 | |
| 7 | namespace OpenAI; |
| 8 | |
| 9 | internal abstract class PageEnumerator<T> : PageResultEnumerator, |
| 10 | IAsyncEnumerator<PageResult<T>>, |
| 11 | IEnumerator<PageResult<T>> |
| 12 | { |
| 13 | public abstract PageResult<T> GetPageFromResult(ClientResult result); |
| 14 | |
| 15 | public PageResult<T> GetCurrentPage() |
| 16 | { |
| 17 | if (Current is null) |
| 18 | { |
| 19 | return GetPageFromResult(GetFirst()); |
| 20 | } |
| 21 | |
| 22 | return ((IEnumerator<PageResult<T>>)this).Current; |
| 23 | } |
| 24 | |
| 25 | public async Task<PageResult<T>> GetCurrentPageAsync() |
| 26 | { |
| 27 | if (Current is null) |
| 28 | { |
| 29 | return GetPageFromResult(await GetFirstAsync().ConfigureAwait(false)); |
| 30 | } |
| 31 | |
| 32 | return ((IEnumerator<PageResult<T>>)this).Current; |
| 33 | } |
| 34 | |
| 35 | PageResult<T> IEnumerator<PageResult<T>>.Current |
| 36 | { |
| 37 | get |
| 38 | { |
| 39 | if (Current is null) |
| 40 | { |
| 41 | return default!; |
| 42 | } |
| 43 | |
| 44 | return GetPageFromResult(Current); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | PageResult<T> IAsyncEnumerator<PageResult<T>>.Current |
| 49 | { |
| 50 | get |
| 51 | { |
| 52 | if (Current is null) |
| 53 | { |
| 54 | return default!; |
| 55 | } |
| 56 | |
| 57 | return GetPageFromResult(Current); |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |