Skip to content

Commit c63049e

Browse files
authored
Add improvements to pagination utilities and client methods (#163)
1 parent 5b0af8c commit c63049e

File tree

10 files changed

+80
-269
lines changed

10 files changed

+80
-269
lines changed

src/Custom/Assistants/AssistantClient.Protocol.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public virtual ClientResult CreateAssistant(BinaryContent content, RequestOption
7070
[EditorBrowsable(EditorBrowsableState.Never)]
7171
public virtual IAsyncEnumerable<ClientResult> GetAssistantsAsync(int? limit, string order, string after, string before, RequestOptions options)
7272
{
73-
AssistantsPageEnumerator enumerator = new AssistantsPageEnumerator(_pipeline, _endpoint, limit, order, after, before, options);
73+
AssistantsPageEnumerator enumerator = new(_pipeline, _endpoint, limit, order, after, before, options);
7474
return PageCollectionHelpers.CreateAsync(enumerator);
7575
}
7676

@@ -101,7 +101,7 @@ public virtual IAsyncEnumerable<ClientResult> GetAssistantsAsync(int? limit, str
101101
[EditorBrowsable(EditorBrowsableState.Never)]
102102
public virtual IEnumerable<ClientResult> GetAssistants(int? limit, string order, string after, string before, RequestOptions options)
103103
{
104-
AssistantsPageEnumerator enumerator = new AssistantsPageEnumerator(_pipeline, _endpoint, limit, order, after, before, options);
104+
AssistantsPageEnumerator enumerator = new(_pipeline, _endpoint, limit, order, after, before, options);
105105
return PageCollectionHelpers.Create(enumerator);
106106
}
107107

@@ -259,7 +259,7 @@ public virtual IAsyncEnumerable<ClientResult> GetMessagesAsync(string threadId,
259259
{
260260
Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
261261

262-
MessagesPageEnumerator enumerator = new MessagesPageEnumerator(_pipeline, _endpoint, threadId, limit, order, after, before, options);
262+
MessagesPageEnumerator enumerator = new(_pipeline, _endpoint, threadId, limit, order, after, before, options);
263263
return PageCollectionHelpers.CreateAsync(enumerator);
264264
}
265265

@@ -295,7 +295,7 @@ public virtual IEnumerable<ClientResult> GetMessages(string threadId, int? limit
295295
{
296296
Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
297297

298-
MessagesPageEnumerator enumerator = new MessagesPageEnumerator(_pipeline, _endpoint, threadId, limit, order, after, before, options);
298+
MessagesPageEnumerator enumerator = new(_pipeline, _endpoint, threadId, limit, order, after, before, options);
299299
return PageCollectionHelpers.Create(enumerator);
300300
}
301301

@@ -381,7 +381,7 @@ public virtual IAsyncEnumerable<ClientResult> GetRunsAsync(string threadId, int?
381381
{
382382
Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
383383

384-
RunsPageEnumerator enumerator = new RunsPageEnumerator(_pipeline, _endpoint, threadId, limit, order, after, before, options);
384+
RunsPageEnumerator enumerator = new(_pipeline, _endpoint, threadId, limit, order, after, before, options);
385385
return PageCollectionHelpers.CreateAsync(enumerator);
386386
}
387387

@@ -417,7 +417,7 @@ public virtual IEnumerable<ClientResult> GetRuns(string threadId, int? limit, st
417417
{
418418
Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
419419

420-
RunsPageEnumerator enumerator = new RunsPageEnumerator(_pipeline, _endpoint, threadId, limit, order, after, before, options);
420+
RunsPageEnumerator enumerator = new(_pipeline, _endpoint, threadId, limit, order, after, before, options);
421421
return PageCollectionHelpers.Create(enumerator);
422422
}
423423

@@ -495,7 +495,7 @@ public virtual IAsyncEnumerable<ClientResult> GetRunStepsAsync(string threadId,
495495
Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
496496
Argument.AssertNotNullOrEmpty(runId, nameof(runId));
497497

498-
RunStepsPageEnumerator enumerator = new RunStepsPageEnumerator(_pipeline, _endpoint, threadId, runId, limit, order, after, before, options);
498+
RunStepsPageEnumerator enumerator = new(_pipeline, _endpoint, threadId, runId, limit, order, after, before, options);
499499
return PageCollectionHelpers.CreateAsync(enumerator);
500500
}
501501

@@ -533,7 +533,7 @@ public virtual IEnumerable<ClientResult> GetRunSteps(string threadId, string run
533533
Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
534534
Argument.AssertNotNullOrEmpty(runId, nameof(runId));
535535

536-
RunStepsPageEnumerator enumerator = new RunStepsPageEnumerator(_pipeline, _endpoint, threadId, runId, limit, order, after, before, options);
536+
RunStepsPageEnumerator enumerator = new(_pipeline, _endpoint, threadId, runId, limit, order, after, before, options);
537537
return PageCollectionHelpers.Create(enumerator);
538538
}
539539

src/Custom/Assistants/AssistantClient.cs

Lines changed: 33 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,8 @@ public virtual AsyncPageCollection<Assistant> GetAssistantsAsync(
118118
AssistantCollectionOptions options = default,
119119
CancellationToken cancellationToken = default)
120120
{
121-
AssistantsPageEnumerator enumerator = new(_pipeline, _endpoint,
122-
options?.PageSize,
123-
options?.Order?.ToString(),
124-
options?.AfterId,
125-
options?.BeforeId,
126-
cancellationToken.ToRequestOptions());
127-
128-
return PageCollectionHelpers.CreateAsync(enumerator);
121+
return GetAssistantsAsync(options?.PageSize, options?.Order?.ToString(), options?.AfterId, options?.BeforeId, cancellationToken.ToRequestOptions())
122+
as AsyncPageCollection<Assistant>;
129123
}
130124

131125
/// <summary>
@@ -144,14 +138,8 @@ public virtual AsyncPageCollection<Assistant> GetAssistantsAsync(
144138
Argument.AssertNotNull(firstPageToken, nameof(firstPageToken));
145139

146140
AssistantsPageToken pageToken = AssistantsPageToken.FromToken(firstPageToken);
147-
AssistantsPageEnumerator enumerator = new(_pipeline, _endpoint,
148-
pageToken.Limit,
149-
pageToken.Order,
150-
pageToken.After,
151-
pageToken.Before,
152-
cancellationToken.ToRequestOptions());
153-
154-
return PageCollectionHelpers.CreateAsync(enumerator);
141+
return GetAssistantsAsync(pageToken?.Limit, pageToken?.Order, pageToken?.After, pageToken.Before, cancellationToken.ToRequestOptions())
142+
as AsyncPageCollection<Assistant>;
155143
}
156144

157145
/// <summary>
@@ -167,14 +155,8 @@ public virtual PageCollection<Assistant> GetAssistants(
167155
AssistantCollectionOptions options = default,
168156
CancellationToken cancellationToken = default)
169157
{
170-
AssistantsPageEnumerator enumerator = new(_pipeline, _endpoint,
171-
options?.PageSize,
172-
options?.Order?.ToString(),
173-
options?.AfterId,
174-
options?.BeforeId,
175-
cancellationToken.ToRequestOptions());
176-
177-
return PageCollectionHelpers.Create(enumerator);
158+
return GetAssistants(options?.PageSize, options?.Order?.ToString(), options?.AfterId, options?.BeforeId, cancellationToken.ToRequestOptions())
159+
as PageCollection<Assistant>;
178160
}
179161

180162
/// <summary>
@@ -193,14 +175,8 @@ public virtual PageCollection<Assistant> GetAssistants(
193175
Argument.AssertNotNull(firstPageToken, nameof(firstPageToken));
194176

195177
AssistantsPageToken pageToken = AssistantsPageToken.FromToken(firstPageToken);
196-
AssistantsPageEnumerator enumerator = new(_pipeline, _endpoint,
197-
pageToken.Limit,
198-
pageToken.Order,
199-
pageToken.After,
200-
pageToken.Before,
201-
cancellationToken.ToRequestOptions());
202-
203-
return PageCollectionHelpers.Create(enumerator);
178+
return GetAssistants(pageToken?.Limit, pageToken?.Order, pageToken?.After, pageToken.Before, cancellationToken.ToRequestOptions())
179+
as PageCollection<Assistant>;
204180
}
205181

206182
/// <summary>
@@ -486,15 +462,8 @@ public virtual AsyncPageCollection<ThreadMessage> GetMessagesAsync(
486462
{
487463
Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
488464

489-
MessagesPageEnumerator enumerator = new(_pipeline, _endpoint,
490-
threadId,
491-
options?.PageSize,
492-
options?.Order?.ToString(),
493-
options?.AfterId,
494-
options?.BeforeId,
495-
cancellationToken.ToRequestOptions());
496-
497-
return PageCollectionHelpers.CreateAsync(enumerator);
465+
return GetMessagesAsync(threadId, options?.PageSize, options?.Order?.ToString(), options?.AfterId, options?.BeforeId, cancellationToken.ToRequestOptions())
466+
as AsyncPageCollection<ThreadMessage>;
498467
}
499468

500469
/// <summary>
@@ -513,15 +482,8 @@ public virtual AsyncPageCollection<ThreadMessage> GetMessagesAsync(
513482
Argument.AssertNotNull(firstPageToken, nameof(firstPageToken));
514483

515484
MessagesPageToken pageToken = MessagesPageToken.FromToken(firstPageToken);
516-
MessagesPageEnumerator enumerator = new(_pipeline, _endpoint,
517-
pageToken.ThreadId,
518-
pageToken.Limit,
519-
pageToken.Order,
520-
pageToken.After,
521-
pageToken.Before,
522-
cancellationToken.ToRequestOptions());
523-
524-
return PageCollectionHelpers.CreateAsync(enumerator);
485+
return GetMessagesAsync(pageToken?.ThreadId, pageToken?.Limit, pageToken?.Order, pageToken?.After, pageToken?.Before, cancellationToken.ToRequestOptions())
486+
as AsyncPageCollection<ThreadMessage>;
525487
}
526488

527489
/// <summary>
@@ -541,15 +503,8 @@ public virtual PageCollection<ThreadMessage> GetMessages(
541503
{
542504
Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
543505

544-
MessagesPageEnumerator enumerator = new(_pipeline, _endpoint,
545-
threadId,
546-
options?.PageSize,
547-
options?.Order?.ToString(),
548-
options?.AfterId,
549-
options?.BeforeId,
550-
cancellationToken.ToRequestOptions());
551-
552-
return PageCollectionHelpers.Create(enumerator);
506+
return GetMessages(threadId, options?.PageSize, options?.Order?.ToString(), options?.AfterId, options?.BeforeId, cancellationToken.ToRequestOptions())
507+
as PageCollection<ThreadMessage>;
553508
}
554509

555510
/// <summary>
@@ -568,15 +523,9 @@ public virtual PageCollection<ThreadMessage> GetMessages(
568523
Argument.AssertNotNull(firstPageToken, nameof(firstPageToken));
569524

570525
MessagesPageToken pageToken = MessagesPageToken.FromToken(firstPageToken);
571-
MessagesPageEnumerator enumerator = new(_pipeline, _endpoint,
572-
pageToken.ThreadId,
573-
pageToken.Limit,
574-
pageToken.Order,
575-
pageToken.After,
576-
pageToken.Before,
577-
cancellationToken.ToRequestOptions());
578-
579-
return PageCollectionHelpers.Create(enumerator);
526+
return GetMessages(pageToken?.ThreadId, pageToken?.Limit, pageToken?.Order, pageToken?.After, pageToken?.Before, cancellationToken.ToRequestOptions())
527+
as PageCollection<ThreadMessage>;
528+
580529
}
581530

582531
/// <summary>
@@ -888,15 +837,8 @@ public virtual AsyncPageCollection<ThreadRun> GetRunsAsync(
888837
{
889838
Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
890839

891-
RunsPageEnumerator enumerator = new(_pipeline, _endpoint,
892-
threadId,
893-
options?.PageSize,
894-
options?.Order?.ToString(),
895-
options?.AfterId,
896-
options?.BeforeId,
897-
cancellationToken.ToRequestOptions());
898-
899-
return PageCollectionHelpers.CreateAsync(enumerator);
840+
return GetRunsAsync(threadId, options?.PageSize, options?.Order?.ToString(), options?.AfterId, options?.BeforeId, cancellationToken.ToRequestOptions())
841+
as AsyncPageCollection<ThreadRun>;
900842
}
901843

902844
/// <summary>
@@ -915,15 +857,8 @@ public virtual AsyncPageCollection<ThreadRun> GetRunsAsync(
915857
Argument.AssertNotNull(firstPageToken, nameof(firstPageToken));
916858

917859
RunsPageToken pageToken = RunsPageToken.FromToken(firstPageToken);
918-
RunsPageEnumerator enumerator = new(_pipeline, _endpoint,
919-
pageToken.ThreadId,
920-
pageToken.Limit,
921-
pageToken.Order,
922-
pageToken.After,
923-
pageToken.Before,
924-
cancellationToken.ToRequestOptions());
925-
926-
return PageCollectionHelpers.CreateAsync(enumerator);
860+
return GetRunsAsync(pageToken?.ThreadId, pageToken?.Limit, pageToken?.Order, pageToken?.After, pageToken?.Before, cancellationToken.ToRequestOptions())
861+
as AsyncPageCollection<ThreadRun>;
927862
}
928863

929864
/// <summary>
@@ -943,15 +878,8 @@ public virtual PageCollection<ThreadRun> GetRuns(
943878
{
944879
Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
945880

946-
RunsPageEnumerator enumerator = new(_pipeline, _endpoint,
947-
threadId,
948-
options?.PageSize,
949-
options?.Order?.ToString(),
950-
options?.AfterId,
951-
options?.BeforeId,
952-
cancellationToken.ToRequestOptions());
953-
954-
return PageCollectionHelpers.Create(enumerator);
881+
return GetRuns(threadId, options?.PageSize, options?.Order?.ToString(), options?.AfterId, options?.BeforeId, cancellationToken.ToRequestOptions())
882+
as PageCollection<ThreadRun>;
955883
}
956884

957885
/// <summary>
@@ -970,15 +898,8 @@ public virtual PageCollection<ThreadRun> GetRuns(
970898
Argument.AssertNotNull(firstPageToken, nameof(firstPageToken));
971899

972900
RunsPageToken pageToken = RunsPageToken.FromToken(firstPageToken);
973-
RunsPageEnumerator enumerator = new(_pipeline, _endpoint,
974-
pageToken.ThreadId,
975-
pageToken.Limit,
976-
pageToken.Order,
977-
pageToken.After,
978-
pageToken.Before,
979-
cancellationToken.ToRequestOptions());
980-
981-
return PageCollectionHelpers.Create(enumerator);
901+
return GetRuns(pageToken?.ThreadId, pageToken?.Limit, pageToken?.Order, pageToken?.After, pageToken?.Before, cancellationToken.ToRequestOptions())
902+
as PageCollection<ThreadRun>;
982903
}
983904

984905
/// <summary>
@@ -1168,16 +1089,8 @@ public virtual AsyncPageCollection<RunStep> GetRunStepsAsync(
11681089
Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
11691090
Argument.AssertNotNullOrEmpty(runId, nameof(runId));
11701091

1171-
RunStepsPageEnumerator enumerator = new(_pipeline, _endpoint,
1172-
threadId,
1173-
runId,
1174-
options?.PageSize,
1175-
options?.Order?.ToString(),
1176-
options?.AfterId,
1177-
options?.BeforeId,
1178-
cancellationToken.ToRequestOptions());
1179-
1180-
return PageCollectionHelpers.CreateAsync(enumerator);
1092+
return GetRunStepsAsync(threadId, runId, options?.PageSize, options?.Order?.ToString(), options?.AfterId, options?.BeforeId, cancellationToken.ToRequestOptions())
1093+
as AsyncPageCollection<RunStep>;
11811094
}
11821095

11831096
/// <summary>
@@ -1196,16 +1109,8 @@ public virtual AsyncPageCollection<RunStep> GetRunStepsAsync(
11961109
Argument.AssertNotNull(firstPageToken, nameof(firstPageToken));
11971110

11981111
RunStepsPageToken pageToken = RunStepsPageToken.FromToken(firstPageToken);
1199-
RunStepsPageEnumerator enumerator = new(_pipeline, _endpoint,
1200-
pageToken.ThreadId,
1201-
pageToken.RunId,
1202-
pageToken.Limit,
1203-
pageToken.Order,
1204-
pageToken.After,
1205-
pageToken.Before,
1206-
cancellationToken.ToRequestOptions());
1207-
1208-
return PageCollectionHelpers.CreateAsync(enumerator);
1112+
return GetRunStepsAsync(pageToken?.ThreadId, pageToken?.RunId, pageToken?.Limit, pageToken?.Order, pageToken?.After, pageToken?.Before, cancellationToken.ToRequestOptions())
1113+
as AsyncPageCollection<RunStep>;
12091114
}
12101115

12111116
/// <summary>
@@ -1228,16 +1133,8 @@ public virtual PageCollection<RunStep> GetRunSteps(
12281133
Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
12291134
Argument.AssertNotNullOrEmpty(runId, nameof(runId));
12301135

1231-
RunStepsPageEnumerator enumerator = new(_pipeline, _endpoint,
1232-
threadId,
1233-
runId,
1234-
options?.PageSize,
1235-
options?.Order?.ToString(),
1236-
options?.AfterId,
1237-
options?.BeforeId,
1238-
cancellationToken.ToRequestOptions());
1239-
1240-
return PageCollectionHelpers.Create(enumerator);
1136+
return GetRunSteps(threadId, runId, options?.PageSize, options?.Order?.ToString(), options?.AfterId, options?.BeforeId, cancellationToken.ToRequestOptions())
1137+
as PageCollection<RunStep>;
12411138
}
12421139

12431140
/// <summary>
@@ -1256,16 +1153,8 @@ public virtual PageCollection<RunStep> GetRunSteps(
12561153
Argument.AssertNotNull(firstPageToken, nameof(firstPageToken));
12571154

12581155
RunStepsPageToken pageToken = RunStepsPageToken.FromToken(firstPageToken);
1259-
RunStepsPageEnumerator enumerator = new(_pipeline, _endpoint,
1260-
pageToken.ThreadId,
1261-
pageToken.RunId,
1262-
pageToken.Limit,
1263-
pageToken.Order,
1264-
pageToken.After,
1265-
pageToken.Before,
1266-
cancellationToken.ToRequestOptions());
1267-
1268-
return PageCollectionHelpers.Create(enumerator);
1156+
return GetRunSteps(pageToken?.ThreadId, pageToken?.RunId, pageToken?.Limit, pageToken?.Order, pageToken?.After, pageToken?.Before, cancellationToken.ToRequestOptions())
1157+
as PageCollection<RunStep>;
12691158
}
12701159

12711160
/// <summary>

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ internal partial class AssistantsPageEnumerator : PageEnumerator<Assistant>
2121
private readonly string _before;
2222
private readonly RequestOptions _options;
2323

24+
public virtual ClientPipeline Pipeline => _pipeline;
25+
2426
public AssistantsPageEnumerator(
2527
ClientPipeline pipeline,
2628
Uri endpoint,

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ internal partial class MessagesPageEnumerator : PageEnumerator<ThreadMessage>
2222
private readonly string _before;
2323
private readonly RequestOptions _options;
2424

25+
public virtual ClientPipeline Pipeline => _pipeline;
26+
2527
public MessagesPageEnumerator(
2628
ClientPipeline pipeline,
2729
Uri endpoint,

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ internal partial class RunStepsPageEnumerator : PageEnumerator<RunStep>
2323

2424
private string? _after;
2525

26+
public virtual ClientPipeline Pipeline => _pipeline;
27+
2628
public RunStepsPageEnumerator(
2729
ClientPipeline pipeline,
2830
Uri endpoint,

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ internal partial class RunsPageEnumerator : PageEnumerator<ThreadRun>
2222
private readonly string _before;
2323
private readonly RequestOptions _options;
2424

25+
public virtual ClientPipeline Pipeline => _pipeline;
26+
2527
public RunsPageEnumerator(
2628
ClientPipeline pipeline,
2729
Uri endpoint,

0 commit comments

Comments
 (0)