openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
OpenAI/src/Generated/CollectionResults/BatchClientGetBatchesAsyncCollectionResultOfT.cs
71lines · modecode
| 1 | // <auto-generated/> |
| 2 | |
| 3 | #nullable disable |
| 4 | |
| 5 | using System; |
| 6 | using System.ClientModel; |
| 7 | using System.ClientModel.Primitives; |
| 8 | using System.Collections.Generic; |
| 9 | using System.Threading.Tasks; |
| 10 | |
| 11 | namespace OpenAI.Batch |
| 12 | { |
| 13 | internal partial class BatchClientGetBatchesAsyncCollectionResultOfT : AsyncCollectionResult<BatchJob> |
| 14 | { |
| 15 | private readonly BatchClient _client; |
| 16 | private readonly string _after; |
| 17 | private readonly int? _limit; |
| 18 | private readonly RequestOptions _options; |
| 19 | |
| 20 | public BatchClientGetBatchesAsyncCollectionResultOfT(BatchClient client, string after, int? limit, RequestOptions options) |
| 21 | { |
| 22 | _client = client; |
| 23 | _after = after; |
| 24 | _limit = limit; |
| 25 | _options = options; |
| 26 | } |
| 27 | |
| 28 | public override async IAsyncEnumerable<ClientResult> GetRawPagesAsync() |
| 29 | { |
| 30 | PipelineMessage message = _client.CreateGetBatchesRequest(_after, _limit, _options); |
| 31 | string nextToken = null; |
| 32 | while (true) |
| 33 | { |
| 34 | ClientResult result = ClientResult.FromResponse(await _client.Pipeline.ProcessMessageAsync(message, _options).ConfigureAwait(false)); |
| 35 | yield return result; |
| 36 | |
| 37 | // Plugin customization: add hasMore assignment |
| 38 | bool hasMore = ((InternalListBatchesResponse)result).HasMore; |
| 39 | nextToken = ((InternalListBatchesResponse)result).LastId; |
| 40 | // Plugin customization: add hasMore == false check to pagination condition |
| 41 | if (string.IsNullOrEmpty(nextToken) || !hasMore) |
| 42 | { |
| 43 | yield break; |
| 44 | } |
| 45 | message = _client.CreateGetBatchesRequest(nextToken, _limit, _options); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | public override ContinuationToken GetContinuationToken(ClientResult page) |
| 50 | { |
| 51 | string nextPage = ((InternalListBatchesResponse)page).LastId; |
| 52 | if (!string.IsNullOrEmpty(nextPage)) |
| 53 | { |
| 54 | return ContinuationToken.FromBytes(BinaryData.FromString(nextPage)); |
| 55 | } |
| 56 | else |
| 57 | { |
| 58 | return null; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | protected override async IAsyncEnumerable<BatchJob> GetValuesFromPageAsync(ClientResult page) |
| 63 | { |
| 64 | foreach (BatchJob item in ((InternalListBatchesResponse)page).Data) |
| 65 | { |
| 66 | yield return item; |
| 67 | await Task.Yield(); |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | } |