openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Custom/Batch/CreateBatchOperation.Protocol.cs
201lines · modecode
| 1 | using System; |
| 2 | using System.ClientModel; |
| 3 | using System.ClientModel.Primitives; |
| 4 | using System.Diagnostics.CodeAnalysis; |
| 5 | using System.Text.Json; |
| 6 | using System.Threading; |
| 7 | using System.Threading.Tasks; |
| 8 | |
| 9 | #nullable enable |
| 10 | |
| 11 | namespace 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")] |
| 18 | public class CreateBatchOperation : OperationResult |
| 19 | { |
| 20 | private readonly BatchClient _parentClient; |
| 21 | private readonly Uri _endpoint; |
| 22 | private readonly string _batchId; |
| 23 | |
| 24 | internal CreateBatchOperation( |
| 25 | BatchClient parentClient, |
| 26 | Uri endpoint, |
| 27 | string batchId, |
| 28 | string status, |
| 29 | PipelineResponse response) |
| 30 | : base(response) |
| 31 | { |
| 32 | _parentClient = parentClient; |
| 33 | _endpoint = endpoint; |
| 34 | _batchId = batchId; |
| 35 | |
| 36 | HasCompleted = GetHasCompleted(status); |
| 37 | RehydrationToken = new CreateBatchOperationToken(batchId); |
| 38 | } |
| 39 | |
| 40 | public string BatchId => _batchId; |
| 41 | |
| 42 | /// <inheritdoc/> |
| 43 | public 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> |
| 56 | public static async Task<CreateBatchOperation> RehydrateAsync(BatchClient client, ContinuationToken rehydrationToken, CancellationToken cancellationToken = default) |
| 57 | { |
| 58 | Argument.AssertNotNull(client, nameof(client)); |
| 59 | Argument.AssertNotNull(rehydrationToken, nameof(rehydrationToken)); |
| 60 | |
| 61 | CreateBatchOperationToken token = CreateBatchOperationToken.FromToken(rehydrationToken); |
| 62 | |
| 63 | ClientResult result = await client.GetBatchAsync(token.BatchId, cancellationToken.ToRequestOptions()).ConfigureAwait(false); |
| 64 | PipelineResponse response = result.GetRawResponse(); |
| 65 | |
| 66 | using JsonDocument doc = JsonDocument.Parse(response.Content); |
| 67 | string status = doc.RootElement.GetProperty("status"u8).GetString()!; |
| 68 | |
| 69 | return client.CreateCreateBatchOperation(token.BatchId, status, response); |
| 70 | } |
| 71 | |
| 72 | /// <summary> |
| 73 | /// Recreates a <see cref="CreateBatchOperation"/> from a rehydration token. |
| 74 | /// </summary> |
| 75 | /// <param name="client"> The <see cref="BatchClient"/> used to obtain the |
| 76 | /// operation status from the service. </param> |
| 77 | /// <param name="rehydrationToken"> The rehydration token corresponding to |
| 78 | /// the operation to rehydrate. </param> |
| 79 | /// <param name="cancellationToken"> A token that can be used to cancel the |
| 80 | /// request. </param> |
| 81 | /// <returns> The rehydrated operation. </returns> |
| 82 | /// <exception cref="ArgumentNullException"> <paramref name="client"/> or <paramref name="rehydrationToken"/> is null. </exception> |
| 83 | public static CreateBatchOperation Rehydrate(BatchClient client, ContinuationToken rehydrationToken, CancellationToken cancellationToken = default) |
| 84 | { |
| 85 | Argument.AssertNotNull(client, nameof(client)); |
| 86 | Argument.AssertNotNull(rehydrationToken, nameof(rehydrationToken)); |
| 87 | |
| 88 | CreateBatchOperationToken token = CreateBatchOperationToken.FromToken(rehydrationToken); |
| 89 | |
| 90 | ClientResult result = client.GetBatch(token.BatchId, cancellationToken.ToRequestOptions()); |
| 91 | PipelineResponse response = result.GetRawResponse(); |
| 92 | |
| 93 | using JsonDocument doc = JsonDocument.Parse(response.Content); |
| 94 | string status = doc.RootElement.GetProperty("status"u8).GetString()!; |
| 95 | |
| 96 | return client.CreateCreateBatchOperation(token.BatchId, status, response); |
| 97 | } |
| 98 | |
| 99 | /// <inheritdoc/> |
| 100 | public override async ValueTask<ClientResult> UpdateStatusAsync(RequestOptions? options = null) |
| 101 | { |
| 102 | ClientResult result = await GetBatchAsync(options).ConfigureAwait(false); |
| 103 | |
| 104 | ApplyUpdate(result); |
| 105 | |
| 106 | return result; |
| 107 | } |
| 108 | |
| 109 | /// <inheritdoc/> |
| 110 | public override ClientResult UpdateStatus(RequestOptions? options = null) |
| 111 | { |
| 112 | ClientResult result = GetBatch(options); |
| 113 | |
| 114 | ApplyUpdate(result); |
| 115 | |
| 116 | return result; |
| 117 | } |
| 118 | |
| 119 | internal async Task<CreateBatchOperation> WaitUntilAsync(bool waitUntilCompleted, RequestOptions? options) |
| 120 | { |
| 121 | if (!waitUntilCompleted) return this; |
| 122 | await WaitForCompletionAsync(options?.CancellationToken ?? default).ConfigureAwait(false); |
| 123 | return this; |
| 124 | } |
| 125 | |
| 126 | internal CreateBatchOperation WaitUntil(bool waitUntilCompleted, RequestOptions? options) |
| 127 | { |
| 128 | if (!waitUntilCompleted) return this; |
| 129 | WaitForCompletion(options?.CancellationToken ?? default); |
| 130 | return this; |
| 131 | } |
| 132 | |
| 133 | private void ApplyUpdate(ClientResult result) |
| 134 | { |
| 135 | PipelineResponse response = result.GetRawResponse(); |
| 136 | |
| 137 | using JsonDocument doc = JsonDocument.Parse(response.Content); |
| 138 | string? status = doc.RootElement.GetProperty("status"u8).GetString(); |
| 139 | |
| 140 | HasCompleted = GetHasCompleted(status); |
| 141 | SetRawResponse(response); |
| 142 | } |
| 143 | |
| 144 | private static bool GetHasCompleted(string? status) |
| 145 | { |
| 146 | return status == InternalBatchStatus.Completed || |
| 147 | status == InternalBatchStatus.Cancelled || |
| 148 | status == InternalBatchStatus.Expired || |
| 149 | status == InternalBatchStatus.Failed; |
| 150 | } |
| 151 | |
| 152 | // Generated protocol methods |
| 153 | |
| 154 | /// <summary> |
| 155 | /// [Protocol Method] Retrieves a batch. |
| 156 | /// </summary> |
| 157 | /// <param name="options"> The request options, which can override default behaviors of the client pipeline on a per-call basis. </param> |
| 158 | /// <exception cref="ClientResultException"> Service returned a non-success status code. </exception> |
| 159 | /// <returns> The response returned from the service. </returns> |
| 160 | public virtual async Task<ClientResult> GetBatchAsync(RequestOptions? options) |
| 161 | { |
| 162 | using PipelineMessage message = _parentClient.CreateRetrieveBatchRequest(_batchId, options); |
| 163 | return ClientResult.FromResponse(await _parentClient.Pipeline.ProcessMessageAsync(message, options).ConfigureAwait(false)); |
| 164 | } |
| 165 | |
| 166 | /// <summary> |
| 167 | /// [Protocol Method] Retrieves a batch. |
| 168 | /// </summary> |
| 169 | /// <param name="options"> The request options, which can override default behaviors of the client pipeline on a per-call basis. </param> |
| 170 | /// <exception cref="ClientResultException"> Service returned a non-success status code. </exception> |
| 171 | /// <returns> The response returned from the service. </returns> |
| 172 | public virtual ClientResult GetBatch(RequestOptions? options) |
| 173 | { |
| 174 | using PipelineMessage message = _parentClient.CreateRetrieveBatchRequest(_batchId, options); |
| 175 | return ClientResult.FromResponse(_parentClient.Pipeline.ProcessMessage(message, options)); |
| 176 | } |
| 177 | |
| 178 | /// <summary> |
| 179 | /// [Protocol Method] Cancels an in-progress batch. |
| 180 | /// </summary> |
| 181 | /// <param name="options"> The request options, which can override default behaviors of the client pipeline on a per-call basis. </param> |
| 182 | /// <exception cref="ClientResultException"> Service returned a non-success status code. </exception> |
| 183 | /// <returns> The response returned from the service. </returns> |
| 184 | public virtual async Task<ClientResult> CancelAsync(RequestOptions? options) |
| 185 | { |
| 186 | using PipelineMessage message = _parentClient.CreateCancelBatchRequest(_batchId, options); |
| 187 | return ClientResult.FromResponse(await _parentClient.Pipeline.ProcessMessageAsync(message, options).ConfigureAwait(false)); |
| 188 | } |
| 189 | |
| 190 | /// <summary> |
| 191 | /// [Protocol Method] Cancels an in-progress batch. |
| 192 | /// </summary> |
| 193 | /// <param name="options"> The request options, which can override default behaviors of the client pipeline on a per-call basis. </param> |
| 194 | /// <exception cref="ClientResultException"> Service returned a non-success status code. </exception> |
| 195 | /// <returns> The response returned from the service. </returns> |
| 196 | public virtual ClientResult Cancel(RequestOptions? options) |
| 197 | { |
| 198 | using PipelineMessage message = _parentClient.CreateCancelBatchRequest(_batchId, options); |
| 199 | return ClientResult.FromResponse(_parentClient.Pipeline.ProcessMessage(message, options)); |
| 200 | } |
| 201 | } |