openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Custom/FineTuning/Internal/Pagination/FineTuningJobCheckpointCollectionPageToken.cs
135lines · modecode
| 1 | using System; |
| 2 | using System.ClientModel; |
| 3 | using System.ClientModel.Primitives; |
| 4 | using System.Diagnostics; |
| 5 | using System.IO; |
| 6 | using System.Text.Json; |
| 7 | |
| 8 | #nullable enable |
| 9 | |
| 10 | namespace OpenAI.FineTuning; |
| 11 | |
| 12 | internal class FineTuningJobCheckpointCollectionPageToken : ContinuationToken |
| 13 | { |
| 14 | protected FineTuningJobCheckpointCollectionPageToken(string jobId, int? limit, string? after) |
| 15 | { |
| 16 | JobId = jobId; |
| 17 | Limit = limit; |
| 18 | After = after; |
| 19 | } |
| 20 | |
| 21 | public string JobId { get; } |
| 22 | |
| 23 | public int? Limit { get; } |
| 24 | |
| 25 | public string? After { get; } |
| 26 | |
| 27 | public override BinaryData ToBytes() |
| 28 | { |
| 29 | using MemoryStream stream = new(); |
| 30 | using Utf8JsonWriter writer = new(stream); |
| 31 | |
| 32 | writer.WriteStartObject(); |
| 33 | writer.WriteString("jobId", JobId); |
| 34 | |
| 35 | if (Limit.HasValue) |
| 36 | { |
| 37 | writer.WriteNumber("limit", Limit.Value); |
| 38 | } |
| 39 | |
| 40 | if (After is not null) |
| 41 | { |
| 42 | writer.WriteString("after", After); |
| 43 | } |
| 44 | |
| 45 | writer.WriteEndObject(); |
| 46 | |
| 47 | writer.Flush(); |
| 48 | stream.Position = 0; |
| 49 | |
| 50 | return BinaryData.FromStream(stream); |
| 51 | } |
| 52 | |
| 53 | public static FineTuningJobCheckpointCollectionPageToken FromToken(ContinuationToken pageToken) |
| 54 | { |
| 55 | if (pageToken is FineTuningJobCheckpointCollectionPageToken token) |
| 56 | { |
| 57 | return token; |
| 58 | } |
| 59 | |
| 60 | BinaryData data = pageToken.ToBytes(); |
| 61 | |
| 62 | if (data.ToMemory().Length == 0) |
| 63 | { |
| 64 | throw new ArgumentException("Failed to create FineTuningJobCheckpointCollectionPageToken from provided pageToken.", nameof(pageToken)); |
| 65 | } |
| 66 | |
| 67 | Utf8JsonReader reader = new(data); |
| 68 | |
| 69 | string jobId = null!; |
| 70 | int? limit = null; |
| 71 | string? after = null; |
| 72 | |
| 73 | reader.Read(); |
| 74 | |
| 75 | Debug.Assert(reader.TokenType == JsonTokenType.StartObject); |
| 76 | |
| 77 | while (reader.Read()) |
| 78 | { |
| 79 | if (reader.TokenType == JsonTokenType.EndObject) |
| 80 | { |
| 81 | break; |
| 82 | } |
| 83 | |
| 84 | Debug.Assert(reader.TokenType == JsonTokenType.PropertyName); |
| 85 | |
| 86 | string propertyName = reader.GetString()!; |
| 87 | |
| 88 | switch (propertyName) |
| 89 | { |
| 90 | case "jobId": |
| 91 | reader.Read(); |
| 92 | Debug.Assert(reader.TokenType == JsonTokenType.String); |
| 93 | jobId = reader.GetString()!; |
| 94 | break; |
| 95 | case "limit": |
| 96 | reader.Read(); |
| 97 | Debug.Assert(reader.TokenType == JsonTokenType.Number); |
| 98 | limit = reader.GetInt32(); |
| 99 | break; |
| 100 | case "after": |
| 101 | reader.Read(); |
| 102 | Debug.Assert(reader.TokenType == JsonTokenType.String); |
| 103 | after = reader.GetString(); |
| 104 | break; |
| 105 | default: |
| 106 | throw new JsonException($"Unrecognized property '{propertyName}'."); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | if (jobId is null) |
| 111 | { |
| 112 | throw new ArgumentException("Failed to create FineTuningJobCheckpointCollectionPageToken from provided pageToken.", nameof(pageToken)); |
| 113 | } |
| 114 | |
| 115 | return new(jobId, limit, after); |
| 116 | } |
| 117 | |
| 118 | public static FineTuningJobCheckpointCollectionPageToken FromOptions(string jobId, int? limit, string? after) |
| 119 | => new(jobId, limit, after); |
| 120 | |
| 121 | public static FineTuningJobCheckpointCollectionPageToken? FromResponse(ClientResult result, string jobId, int? limit) |
| 122 | { |
| 123 | PipelineResponse response = result.GetRawResponse(); |
| 124 | using JsonDocument doc = JsonDocument.Parse(response.Content); |
| 125 | string lastId = doc.RootElement.GetProperty("last_id"u8).GetString()!; |
| 126 | bool hasMore = doc.RootElement.GetProperty("has_more"u8).GetBoolean(); |
| 127 | |
| 128 | if (!hasMore || lastId is null) |
| 129 | { |
| 130 | return null; |
| 131 | } |
| 132 | |
| 133 | return new(jobId, limit, lastId); |
| 134 | } |
| 135 | } |
| 136 | |