-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathApiClient.cs
More file actions
311 lines (271 loc) · 10.3 KB
/
ApiClient.cs
File metadata and controls
311 lines (271 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System.Collections.Immutable;
using System.Reflection;
using System.Runtime.CompilerServices;
using Google.Apis.Auth.OAuth2;
using Google.GenAI.Types;
namespace Google.GenAI
{
/// <summary>
/// Abstract base class for an API client which issues HTTP requests to the GenAI APIs.
/// </summary>
public abstract class ApiClient : IDisposable, IAsyncDisposable
{
private static readonly string SdkVersion = GetSdkVersion();
protected HttpMessageInvoker HttpClient { get; }
public string? ApiKey { get; }
public string? Project { get; }
public string? Location { get; }
public ICredential? Credentials { get; }
public HttpOptions HttpOptions { get; protected set; }
public bool VertexAI { get; }
private int _disposed = 0;
/// <summary>
/// Constructs an ApiClient for Gemini API.
/// </summary>
protected ApiClient(string? apiKey, HttpOptions? customHttpOptions)
{
this.ApiKey = apiKey ?? System.Environment.GetEnvironmentVariable("GOOGLE_API_KEY");
if (string.IsNullOrEmpty(this.ApiKey))
{
throw new ArgumentException(
"API key must either be provided or set in the environment variable GOOGLE_API_KEY.");
}
this.Project = null;
this.Location = null;
this.Credentials = null;
this.VertexAI = false;
this.HttpOptions = GetDefaultHttpOptions(false, this.Location);
if (customHttpOptions is not null)
{
this.HttpOptions = MergeHttpOptions(customHttpOptions);
}
this.HttpClient = CreateHttpClient(this.HttpOptions);
}
/// <summary>
/// Constructs an ApiClient for Vertex AI APIs.
/// </summary>
protected ApiClient(
string? project,
string? location,
ICredential? credentials,
HttpOptions? customHttpOptions)
{
this.Project = project ?? System.Environment.GetEnvironmentVariable("GOOGLE_CLOUD_PROJECT");
if (string.IsNullOrEmpty(this.Project))
{
throw new ArgumentException(
"Project must either be provided or set in the environment variable GOOGLE_CLOUD_PROJECT.");
}
this.Location = location ?? System.Environment.GetEnvironmentVariable("GOOGLE_CLOUD_LOCATION");
if (string.IsNullOrEmpty(this.Location))
{
throw new ArgumentException(
"Location must either be provided or set in the environment variable GOOGLE_CLOUD_LOCATION.");
}
this.Credentials = credentials ?? GetDefaultCredentials();
this.HttpOptions = GetDefaultHttpOptions(true, this.Location);
if (customHttpOptions is not null)
{
this.HttpOptions = MergeHttpOptions(customHttpOptions);
}
this.ApiKey = null;
this.VertexAI = true;
this.HttpClient = CreateHttpClient(this.HttpOptions);
}
private static HttpClient CreateHttpClient(HttpOptions httpOptions)
{
var client = new HttpClient();
if (httpOptions.Timeout != null)
{
client.Timeout = System.TimeSpan.FromMilliseconds(httpOptions.Timeout.Value);
}
return client;
}
/// <summary>
/// Sends an HTTP request given the HTTP method, path, and request JSON string.
/// </summary>
public abstract Task<ApiResponse> RequestAsync(
HttpMethod httpMethod, string path, string requestJson, HttpOptions? requestHttpOptions,
CancellationToken cancellationToken = default);
/// <summary>
/// Sends an HTTP request for streaming responses that return Server-Sent Events.
/// </summary>
/// <param name="httpMethod">The HTTP method to use.</param>
/// <param name="path">The API path.</param>
/// <param name="requestJson">The request body as JSON string.</param>
/// <param name="requestHttpOptions">Optional HTTP options.</param>
/// <param name="cancellationToken">The cancellation token to use.</param>
/// <returns>An async enumerable of ApiResponse chunks.</returns>
public abstract IAsyncEnumerable<ApiResponse> RequestStreamAsync(
HttpMethod httpMethod,
string path,
string requestJson,
HttpOptions? requestHttpOptions,
[EnumeratorCancellation] CancellationToken cancellationToken = default);
/// <summary>
/// Returns the library version string.
/// </summary>
internal static string GetLibraryVersion()
{
string libraryLabel = $"google-genai-sdk/{SdkVersion}";
// Using RuntimeInformation for .NET version
string languageLabel = $"gl-dotnet/{8.0}";
return $"{libraryLabel} {languageLabel}";
}
/// <summary>
/// Merges the given HttpOptions with the client's current HttpOptions.
/// </summary>
/// <param name="optionsToApply">The HttpOptions to apply.</param>
/// <returns>A new HttpOptions instance with merged values.</returns>
protected HttpOptions MergeHttpOptions(HttpOptions? optionsToApply)
{
if (optionsToApply is null)
{
return this.HttpOptions with { };
}
var mergedOptions = this.HttpOptions with { };
if (!string.IsNullOrEmpty(optionsToApply?.BaseUrl))
{
mergedOptions.BaseUrl = optionsToApply?.BaseUrl;
}
if (!string.IsNullOrEmpty(optionsToApply?.ApiVersion))
{
mergedOptions.ApiVersion = optionsToApply?.ApiVersion;
}
if (optionsToApply?.Timeout != null)
{
mergedOptions.Timeout = optionsToApply?.Timeout;
}
var currentHeaders = this.HttpOptions.Headers ?? new Dictionary<string, string>(ImmutableDictionary<string, string>.Empty);
var newHeaders = optionsToApply.Headers ?? new Dictionary<string, string>(ImmutableDictionary<string, string>.Empty);
var headersBuilder = ImmutableDictionary.CreateBuilder<string, string>(StringComparer.OrdinalIgnoreCase);
foreach (var header in currentHeaders)
{
headersBuilder[header.Key] = header.Value;
}
foreach (var header in newHeaders)
{
if (string.Equals(header.Key, "User-Agent", StringComparison.OrdinalIgnoreCase) ||
string.Equals(header.Key, "x-goog-api-client", StringComparison.OrdinalIgnoreCase))
{
if (headersBuilder.TryGetValue(header.Key, out var existingValue))
{
headersBuilder[header.Key] = $"{existingValue} {header.Value}";
}
else
{
headersBuilder[header.Key] = header.Value;
}
}
else
{
headersBuilder[header.Key] = header.Value;
}
}
mergedOptions.Headers = new Dictionary<string, string>(headersBuilder.ToImmutable());
return mergedOptions;
}
internal static HttpOptions GetDefaultHttpOptions(bool vertexAI, string? location)
{
var defaultHeadersBuilder = ImmutableDictionary.CreateBuilder<string, string>(StringComparer.OrdinalIgnoreCase);
defaultHeadersBuilder.Add("Content-Type", "application/json");
defaultHeadersBuilder.Add("User-Agent", GetLibraryVersion());
defaultHeadersBuilder.Add("x-goog-api-client", GetLibraryVersion());
var defaultHttpOptions = new HttpOptions
{
Headers = new Dictionary<string, string>(defaultHeadersBuilder.ToImmutable()),
};
if (vertexAI)
{
if (string.IsNullOrEmpty(location))
{
throw new ArgumentException("Location must be provided for Vertex AI APIs.");
}
defaultHttpOptions.BaseUrl = location.Equals("global", StringComparison.OrdinalIgnoreCase)
? "https://aiplatform.googleapis.com"
: $"https://{location}-aiplatform.googleapis.com";
defaultHttpOptions.ApiVersion = "v1beta1";
}
else
{
defaultHttpOptions.BaseUrl = "https://generativelanguage.googleapis.com";
defaultHttpOptions.ApiVersion = "v1beta";
}
return defaultHttpOptions;
}
private GoogleCredential GetDefaultCredentials()
{
try
{
var credentialTask = GoogleCredential.GetApplicationDefaultAsync();
GoogleCredential credential = credentialTask.GetAwaiter().GetResult();
if (credential.IsCreateScopedRequired)
{
credential = credential.CreateScoped("https://www.googleapis.com/auth/cloud-platform");
}
return credential;
}
catch (Exception e)
{
throw new ArgumentException(
"Failed to get application default credentials. Please ensure credentials are set up correctly (e.g., via gcloud auth application-default login) or provide them explicitly.",
e);
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (Interlocked.CompareExchange(ref _disposed, 1, 0) != 0)
{
return;
}
if (disposing)
{
HttpClient?.Dispose();
}
}
/// <summary>
/// Asynchronously disposes the client and its underlying resources.
/// </summary>
/// <returns>A <see cref="ValueTask"/> that represents the asynchronous dispose operation.</returns>
public virtual ValueTask DisposeAsync()
{
Dispose();
#if NETSTANDARD2_1
return new ValueTask(Task.CompletedTask);
#else
return ValueTask.CompletedTask;
#endif
}
private static string GetSdkVersion()
{
// This reads AssemblyInformationalVersionAttribute from the assembly,
// which is generated during build from the <Version> property.
// Google.GenAI.csproj imports ReleaseVersion.xml to set <Version>,
// so this effectively reads the version from ReleaseVersion.xml.
return typeof(ApiClient)
.Assembly
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()
?.InformationalVersion ?? "";
}
}
}