openai/openai-dotnet

Public

mirrored from https://github.com/openai/openai-dotnetAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
OpenAI_2.3.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/Custom/Assistants/Internal/Pagination/AssistantCollectionResult.cs

96lines · modecode

1using System.ClientModel;
2using System.ClientModel.Primitives;
3using System.Collections.Generic;
4using System.Text.Json;
5
6#nullable enable
7
8namespace OpenAI.Assistants;
9
10internal class AssistantCollectionResult : CollectionResult<Assistant>
11{
12 private readonly AssistantClient _assistantClient;
13 private readonly ClientPipeline _pipeline;
14 private readonly RequestOptions? _options;
15
16 // Initial values
17 private readonly int? _limit;
18 private readonly string? _order;
19 private readonly string? _after;
20 private readonly string? _before;
21
22 public AssistantCollectionResult(AssistantClient assistantClient,
23 ClientPipeline pipeline, RequestOptions? options,
24 int? limit, string? order, string? after, string? before)
25 {
26 _assistantClient = assistantClient;
27 _pipeline = pipeline;
28 _options = options;
29
30 _limit = limit;
31 _order = order;
32 _after = after;
33 _before = before;
34 }
35
36 public override IEnumerable<ClientResult> GetRawPages()
37 {
38 ClientResult page = GetFirstPage();
39 yield return page;
40
41 while (HasNextPage(page))
42 {
43 page = GetNextPage(page);
44 yield return page;
45 }
46 }
47
48 protected override IEnumerable<Assistant> GetValuesFromPage(ClientResult page)
49 {
50 Argument.AssertNotNull(page, nameof(page));
51
52 PipelineResponse response = page.GetRawResponse();
53 InternalListAssistantsResponse list = ModelReaderWriter.Read<InternalListAssistantsResponse>(response.Content)!;
54 return list.Data;
55 }
56
57 public override ContinuationToken? GetContinuationToken(ClientResult page)
58 {
59 Argument.AssertNotNull(page, nameof(page));
60
61 return AssistantCollectionPageToken.FromResponse(page, _limit, _order, _before);
62 }
63
64 public ClientResult GetFirstPage()
65 => GetAssistants(_limit, _order, _after, _before, _options);
66
67 public ClientResult GetNextPage(ClientResult result)
68 {
69 Argument.AssertNotNull(result, nameof(result));
70
71 PipelineResponse response = result.GetRawResponse();
72
73 using JsonDocument doc = JsonDocument.Parse(response.Content);
74 string lastId = doc.RootElement.GetProperty("last_id"u8).GetString()!;
75
76 return GetAssistants(_limit, _order, lastId, _before, _options);
77 }
78
79 public static bool HasNextPage(ClientResult result)
80 {
81 Argument.AssertNotNull(result, nameof(result));
82
83 PipelineResponse response = result.GetRawResponse();
84
85 using JsonDocument doc = JsonDocument.Parse(response.Content);
86 bool hasMore = doc.RootElement.GetProperty("has_more"u8).GetBoolean();
87
88 return hasMore;
89 }
90
91 internal virtual ClientResult GetAssistants(int? limit, string? order, string? after, string? before, RequestOptions? options)
92 {
93 using PipelineMessage message = _assistantClient.CreateGetAssistantsRequest(limit, order, after, before, options);
94 return ClientResult.FromResponse(_pipeline.ProcessMessage(message, options));
95 }
96}
97