openai/openai-dotnet

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
achandmsft-patch-1

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/Custom/Batch/CreateBatchOperation.Protocol.cs

235lines · modeblame

a330c2e7Jose Arriaga Maldonado1 years ago1using System;
2using System.ClientModel;
3using System.ClientModel.Primitives;
4using System.Diagnostics.CodeAnalysis;
5using System.Text.Json;
6using System.Threading;
7using System.Threading.Tasks;
8
9#nullable enable
10
11namespace OpenAI.Batch;
12
13/// <summary>
14/// A long-running operation for executing a batch from an uploaded file of
15/// requests.
16/// </summary>
17[Experimental("OPENAI001")]
18public class CreateBatchOperation : OperationResult
19{
0ca4c062Jose Arriaga Maldonado1 years ago20private readonly BatchClient _parentClient;
a330c2e7Jose Arriaga Maldonado1 years ago21private readonly Uri _endpoint;
22private readonly string _batchId;
23
24internal CreateBatchOperation(
0ca4c062Jose Arriaga Maldonado1 years ago25BatchClient parentClient,
a330c2e7Jose Arriaga Maldonado1 years ago26Uri endpoint,
27string batchId,
28string status,
29PipelineResponse response)
0ca4c062Jose Arriaga Maldonado1 years ago30: base(response)
a330c2e7Jose Arriaga Maldonado1 years ago31{
0ca4c062Jose Arriaga Maldonado1 years ago32_parentClient = parentClient;
a330c2e7Jose Arriaga Maldonado1 years ago33_endpoint = endpoint;
34_batchId = batchId;
35
36HasCompleted = GetHasCompleted(status);
37RehydrationToken = new CreateBatchOperationToken(batchId);
38}
39
40public string BatchId => _batchId;
41
42/// <inheritdoc/>
43public override ContinuationToken? RehydrationToken { get; protected set; }
44
45/// <summary>
46/// Recreates a <see cref="CreateBatchOperation"/> from a rehydration token.
47/// </summary>
48/// <param name="client"> The <see cref="BatchClient"/> used to obtain the
49/// operation status from the service. </param>
50/// <param name="rehydrationToken"> The rehydration token corresponding to
51/// the operation to rehydrate. </param>
52/// <param name="cancellationToken"> A token that can be used to cancel the
53/// request. </param>
54/// <returns> The rehydrated operation. </returns>
55/// <exception cref="ArgumentNullException"> <paramref name="client"/> or <paramref name="rehydrationToken"/> is null. </exception>
56public static async Task<CreateBatchOperation> RehydrateAsync(BatchClient client, ContinuationToken rehydrationToken, CancellationToken cancellationToken = default)
57{
58Argument.AssertNotNull(client, nameof(client));
59Argument.AssertNotNull(rehydrationToken, nameof(rehydrationToken));
60
61CreateBatchOperationToken token = CreateBatchOperationToken.FromToken(rehydrationToken);
62
8a902599Krzysztof Cwalina1 years ago63return await RehydrateAsync(client, token.BatchId, cancellationToken).ConfigureAwait(false);
a330c2e7Jose Arriaga Maldonado1 years ago64}
65
66/// <summary>
67/// Recreates a <see cref="CreateBatchOperation"/> from a rehydration token.
68/// </summary>
69/// <param name="client"> The <see cref="BatchClient"/> used to obtain the
70/// operation status from the service. </param>
71/// <param name="rehydrationToken"> The rehydration token corresponding to
72/// the operation to rehydrate. </param>
73/// <param name="cancellationToken"> A token that can be used to cancel the
74/// request. </param>
75/// <returns> The rehydrated operation. </returns>
76/// <exception cref="ArgumentNullException"> <paramref name="client"/> or <paramref name="rehydrationToken"/> is null. </exception>
77public static CreateBatchOperation Rehydrate(BatchClient client, ContinuationToken rehydrationToken, CancellationToken cancellationToken = default)
78{
79Argument.AssertNotNull(client, nameof(client));
80Argument.AssertNotNull(rehydrationToken, nameof(rehydrationToken));
81
82CreateBatchOperationToken token = CreateBatchOperationToken.FromToken(rehydrationToken);
83
8a902599Krzysztof Cwalina1 years ago84return Rehydrate(client, token.BatchId, cancellationToken);
85}
86
87/// <summary>
88/// Recreates a <see cref="CreateBatchOperation"/> from a batch ID.
89/// </summary>
90/// <param name="client"> The <see cref="BatchClient"/> used to obtain the
91/// operation status from the service. </param>
92/// <param name="batchId"> The ID of the batch operation to rehydrate. </param>
93/// <param name="cancellationToken"> A token that can be used to cancel the
94/// request. </param>
95/// <returns> The rehydrated operation. </returns>
96/// <exception cref="ArgumentNullException"> <paramref name="client"/> is null. </exception>
97public static async Task<CreateBatchOperation> RehydrateAsync(BatchClient client, string batchId, CancellationToken cancellationToken = default)
98{
99Argument.AssertNotNull(client, nameof(client));
100
101ClientResult result = await client.GetBatchAsync(batchId, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
102PipelineResponse response = result.GetRawResponse();
103
104using JsonDocument doc = JsonDocument.Parse(response.Content);
105string status = doc.RootElement.GetProperty("status"u8).GetString()!;
106
107return client.CreateCreateBatchOperation(batchId, status, response);
108}
109
110/// <summary>
111/// Recreates a <see cref="CreateBatchOperation"/> from a batch ID.
112/// </summary>
113/// <param name="client"> The <see cref="BatchClient"/> used to obtain the
114/// operation status from the service. </param>
115/// <param name="batchId"> The ID of the batch operation to rehydrate. </param>
116/// <param name="cancellationToken"> A token that can be used to cancel the
117/// request. </param>
118/// <returns> The rehydrated operation. </returns>
119/// <exception cref="ArgumentNullException"> <paramref name="client"/> is null. </exception>
120public static CreateBatchOperation Rehydrate(BatchClient client, string batchId, CancellationToken cancellationToken = default)
121{
122Argument.AssertNotNull(client, nameof(client));
123
124ClientResult result = client.GetBatch(batchId, cancellationToken.ToRequestOptions());
a330c2e7Jose Arriaga Maldonado1 years ago125PipelineResponse response = result.GetRawResponse();
126
127using JsonDocument doc = JsonDocument.Parse(response.Content);
128string status = doc.RootElement.GetProperty("status"u8).GetString()!;
129
8a902599Krzysztof Cwalina1 years ago130return client.CreateCreateBatchOperation(batchId, status, response);
a330c2e7Jose Arriaga Maldonado1 years ago131}
132
133/// <inheritdoc/>
134public override async ValueTask<ClientResult> UpdateStatusAsync(RequestOptions? options = null)
135{
136ClientResult result = await GetBatchAsync(options).ConfigureAwait(false);
137
138ApplyUpdate(result);
139
140return result;
141}
142
143/// <inheritdoc/>
144public override ClientResult UpdateStatus(RequestOptions? options = null)
145{
146ClientResult result = GetBatch(options);
147
148ApplyUpdate(result);
149
150return result;
151}
152
153internal async Task<CreateBatchOperation> WaitUntilAsync(bool waitUntilCompleted, RequestOptions? options)
154{
155if (!waitUntilCompleted) return this;
156await WaitForCompletionAsync(options?.CancellationToken ?? default).ConfigureAwait(false);
157return this;
158}
159
160internal CreateBatchOperation WaitUntil(bool waitUntilCompleted, RequestOptions? options)
161{
162if (!waitUntilCompleted) return this;
163WaitForCompletion(options?.CancellationToken ?? default);
164return this;
165}
166
167private void ApplyUpdate(ClientResult result)
168{
169PipelineResponse response = result.GetRawResponse();
170
171using JsonDocument doc = JsonDocument.Parse(response.Content);
172string? status = doc.RootElement.GetProperty("status"u8).GetString();
173
174HasCompleted = GetHasCompleted(status);
175SetRawResponse(response);
176}
177
178private static bool GetHasCompleted(string? status)
179{
180return status == InternalBatchStatus.Completed ||
181status == InternalBatchStatus.Cancelled ||
182status == InternalBatchStatus.Expired ||
183status == InternalBatchStatus.Failed;
184}
185
186// Generated protocol methods
187
188/// <summary>
189/// [Protocol Method] Retrieves a batch.
190/// </summary>
191/// <param name="options"> The request options, which can override default behaviors of the client pipeline on a per-call basis. </param>
192/// <exception cref="ClientResultException"> Service returned a non-success status code. </exception>
193/// <returns> The response returned from the service. </returns>
194public virtual async Task<ClientResult> GetBatchAsync(RequestOptions? options)
195{
0ca4c062Jose Arriaga Maldonado1 years ago196using PipelineMessage message = _parentClient.CreateRetrieveBatchRequest(_batchId, options);
197return ClientResult.FromResponse(await _parentClient.Pipeline.ProcessMessageAsync(message, options).ConfigureAwait(false));
a330c2e7Jose Arriaga Maldonado1 years ago198}
199
200/// <summary>
201/// [Protocol Method] Retrieves a batch.
202/// </summary>
203/// <param name="options"> The request options, which can override default behaviors of the client pipeline on a per-call basis. </param>
204/// <exception cref="ClientResultException"> Service returned a non-success status code. </exception>
205/// <returns> The response returned from the service. </returns>
206public virtual ClientResult GetBatch(RequestOptions? options)
207{
0ca4c062Jose Arriaga Maldonado1 years ago208using PipelineMessage message = _parentClient.CreateRetrieveBatchRequest(_batchId, options);
209return ClientResult.FromResponse(_parentClient.Pipeline.ProcessMessage(message, options));
a330c2e7Jose Arriaga Maldonado1 years ago210}
211
212/// <summary>
213/// [Protocol Method] Cancels an in-progress batch.
214/// </summary>
215/// <param name="options"> The request options, which can override default behaviors of the client pipeline on a per-call basis. </param>
216/// <exception cref="ClientResultException"> Service returned a non-success status code. </exception>
217/// <returns> The response returned from the service. </returns>
218public virtual async Task<ClientResult> CancelAsync(RequestOptions? options)
219{
0ca4c062Jose Arriaga Maldonado1 years ago220using PipelineMessage message = _parentClient.CreateCancelBatchRequest(_batchId, options);
221return ClientResult.FromResponse(await _parentClient.Pipeline.ProcessMessageAsync(message, options).ConfigureAwait(false));
a330c2e7Jose Arriaga Maldonado1 years ago222}
223
224/// <summary>
225/// [Protocol Method] Cancels an in-progress batch.
226/// </summary>
227/// <param name="options"> The request options, which can override default behaviors of the client pipeline on a per-call basis. </param>
228/// <exception cref="ClientResultException"> Service returned a non-success status code. </exception>
229/// <returns> The response returned from the service. </returns>
230public virtual ClientResult Cancel(RequestOptions? options)
231{
0ca4c062Jose Arriaga Maldonado1 years ago232using PipelineMessage message = _parentClient.CreateCancelBatchRequest(_batchId, options);
233return ClientResult.FromResponse(_parentClient.Pipeline.ProcessMessage(message, options));
a330c2e7Jose Arriaga Maldonado1 years ago234}
235}