Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
216 changes: 108 additions & 108 deletions Google.GenAI.Tests/GoogleGenAIExtensionsTest.cs

Large diffs are not rendered by default.

52 changes: 35 additions & 17 deletions Google.GenAI/Batches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1618,8 +1618,11 @@ private async Task<BatchJob> PrivateCreateAsync(string? model, BatchJobSource sr
ApiResponse response = await this._apiClient.RequestAsync(
HttpMethod.Post, path, JsonSerializer.Serialize(body), requestHttpOptions);
HttpContent httpContent = response.GetEntity();
string contentString = await httpContent.ReadAsStringAsync();
JsonNode? httpContentNode = JsonNode.Parse(contentString);
JsonNode? httpContentNode;
using (var stream = await httpContent.ReadAsStreamAsync())
{
httpContentNode = await JsonNode.ParseAsync(stream);
}
if (httpContentNode == null) {
throw new NotSupportedException("Failed to parse response to JsonNode.");
}
Expand All @@ -1633,7 +1636,7 @@ private async Task<BatchJob> PrivateCreateAsync(string? model, BatchJobSource sr
responseNode = BatchJobFromMldev(httpContentNode, new JsonObject());
}

return JsonSerializer.Deserialize<BatchJob>(responseNode.ToString()) ??
return JsonSerializer.Deserialize<BatchJob>(responseNode) ??
throw new InvalidOperationException("Failed to deserialize Task<BatchJob>.");
}

Expand Down Expand Up @@ -1680,8 +1683,11 @@ private async Task<BatchJob> PrivateCreateEmbeddingsAsync(
ApiResponse response = await this._apiClient.RequestAsync(
HttpMethod.Post, path, JsonSerializer.Serialize(body), requestHttpOptions);
HttpContent httpContent = response.GetEntity();
string contentString = await httpContent.ReadAsStringAsync();
JsonNode? httpContentNode = JsonNode.Parse(contentString);
JsonNode? httpContentNode;
using (var stream = await httpContent.ReadAsStreamAsync())
{
httpContentNode = await JsonNode.ParseAsync(stream);
}
if (httpContentNode == null) {
throw new NotSupportedException("Failed to parse response to JsonNode.");
}
Expand All @@ -1696,7 +1702,7 @@ private async Task<BatchJob> PrivateCreateEmbeddingsAsync(
responseNode = BatchJobFromMldev(httpContentNode, new JsonObject());
}

return JsonSerializer.Deserialize<BatchJob>(responseNode.ToString()) ??
return JsonSerializer.Deserialize<BatchJob>(responseNode) ??
throw new InvalidOperationException("Failed to deserialize Task<BatchJob>.");
}

Expand Down Expand Up @@ -1748,8 +1754,11 @@ public async Task<BatchJob> GetAsync(string name, GetBatchJobConfig? config = nu
ApiResponse response = await this._apiClient.RequestAsync(
HttpMethod.Get, path, JsonSerializer.Serialize(body), requestHttpOptions);
HttpContent httpContent = response.GetEntity();
string contentString = await httpContent.ReadAsStringAsync();
JsonNode? httpContentNode = JsonNode.Parse(contentString);
JsonNode? httpContentNode;
using (var stream = await httpContent.ReadAsStreamAsync())
{
httpContentNode = await JsonNode.ParseAsync(stream);
}
if (httpContentNode == null) {
throw new NotSupportedException("Failed to parse response to JsonNode.");
}
Expand All @@ -1763,7 +1772,7 @@ public async Task<BatchJob> GetAsync(string name, GetBatchJobConfig? config = nu
responseNode = BatchJobFromMldev(httpContentNode, new JsonObject());
}

return JsonSerializer.Deserialize<BatchJob>(responseNode.ToString()) ??
return JsonSerializer.Deserialize<BatchJob>(responseNode) ??
throw new InvalidOperationException("Failed to deserialize Task<BatchJob>.");
}

Expand Down Expand Up @@ -1815,8 +1824,11 @@ public async Task CancelAsync(string name, CancelBatchJobConfig? config = null)
ApiResponse response = await this._apiClient.RequestAsync(
HttpMethod.Post, path, JsonSerializer.Serialize(body), requestHttpOptions);
HttpContent httpContent = response.GetEntity();
string contentString = await httpContent.ReadAsStringAsync();
JsonNode? httpContentNode = JsonNode.Parse(contentString);
JsonNode? httpContentNode;
using (var stream = await httpContent.ReadAsStreamAsync())
{
httpContentNode = await JsonNode.ParseAsync(stream);
}
if (httpContentNode == null) {
throw new NotSupportedException("Failed to parse response to JsonNode.");
}
Expand Down Expand Up @@ -1867,8 +1879,11 @@ private async Task<ListBatchJobsResponse> PrivateListAsync(ListBatchJobsConfig?
ApiResponse response = await this._apiClient.RequestAsync(
HttpMethod.Get, path, JsonSerializer.Serialize(body), requestHttpOptions);
HttpContent httpContent = response.GetEntity();
string contentString = await httpContent.ReadAsStringAsync();
JsonNode? httpContentNode = JsonNode.Parse(contentString);
JsonNode? httpContentNode;
using (var stream = await httpContent.ReadAsStreamAsync())
{
httpContentNode = await JsonNode.ParseAsync(stream);
}
if (httpContentNode == null) {
throw new NotSupportedException("Failed to parse response to JsonNode.");
}
Expand All @@ -1882,7 +1897,7 @@ private async Task<ListBatchJobsResponse> PrivateListAsync(ListBatchJobsConfig?
responseNode = ListBatchJobsResponseFromMldev(httpContentNode, new JsonObject());
}

return JsonSerializer.Deserialize<ListBatchJobsResponse>(responseNode.ToString()) ??
return JsonSerializer.Deserialize<ListBatchJobsResponse>(responseNode) ??
throw new InvalidOperationException(
"Failed to deserialize Task<ListBatchJobsResponse>.");
}
Expand Down Expand Up @@ -1936,8 +1951,11 @@ public async Task<DeleteResourceJob> DeleteAsync(string name,
ApiResponse response = await this._apiClient.RequestAsync(
HttpMethod.Delete, path, JsonSerializer.Serialize(body), requestHttpOptions);
HttpContent httpContent = response.GetEntity();
string contentString = await httpContent.ReadAsStringAsync();
JsonNode? httpContentNode = JsonNode.Parse(contentString);
JsonNode? httpContentNode;
using (var stream = await httpContent.ReadAsStreamAsync())
{
httpContentNode = await JsonNode.ParseAsync(stream);
}
if (httpContentNode == null) {
throw new NotSupportedException("Failed to parse response to JsonNode.");
}
Expand All @@ -1951,7 +1969,7 @@ public async Task<DeleteResourceJob> DeleteAsync(string name,
responseNode = DeleteResourceJobFromMldev(httpContentNode, new JsonObject());
}

return JsonSerializer.Deserialize<DeleteResourceJob>(responseNode.ToString()) ??
return JsonSerializer.Deserialize<DeleteResourceJob>(responseNode) ??
throw new InvalidOperationException("Failed to deserialize Task<DeleteResourceJob>.");
}

Expand Down
45 changes: 30 additions & 15 deletions Google.GenAI/Caches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -921,8 +921,11 @@ public async Task<CachedContent> CreateAsync(string model,
ApiResponse response = await this._apiClient.RequestAsync(
HttpMethod.Post, path, JsonSerializer.Serialize(body), requestHttpOptions);
HttpContent httpContent = response.GetEntity();
string contentString = await httpContent.ReadAsStringAsync();
JsonNode? httpContentNode = JsonNode.Parse(contentString);
JsonNode? httpContentNode;
using (var stream = await httpContent.ReadAsStreamAsync())
{
httpContentNode = await JsonNode.ParseAsync(stream);
}
if (httpContentNode == null) {
throw new NotSupportedException("Failed to parse response to JsonNode.");
}
Expand All @@ -936,7 +939,7 @@ public async Task<CachedContent> CreateAsync(string model,
responseNode = httpContentNode;
}

return JsonSerializer.Deserialize<CachedContent>(responseNode.ToString()) ??
return JsonSerializer.Deserialize<CachedContent>(responseNode) ??
throw new InvalidOperationException("Failed to deserialize Task<CachedContent>.");
}

Expand Down Expand Up @@ -977,8 +980,11 @@ public async Task<CachedContent> GetAsync(string name, GetCachedContentConfig? c
ApiResponse response = await this._apiClient.RequestAsync(
HttpMethod.Get, path, JsonSerializer.Serialize(body), requestHttpOptions);
HttpContent httpContent = response.GetEntity();
string contentString = await httpContent.ReadAsStringAsync();
JsonNode? httpContentNode = JsonNode.Parse(contentString);
JsonNode? httpContentNode;
using (var stream = await httpContent.ReadAsStreamAsync())
{
httpContentNode = await JsonNode.ParseAsync(stream);
}
if (httpContentNode == null) {
throw new NotSupportedException("Failed to parse response to JsonNode.");
}
Expand All @@ -992,7 +998,7 @@ public async Task<CachedContent> GetAsync(string name, GetCachedContentConfig? c
responseNode = httpContentNode;
}

return JsonSerializer.Deserialize<CachedContent>(responseNode.ToString()) ??
return JsonSerializer.Deserialize<CachedContent>(responseNode) ??
throw new InvalidOperationException("Failed to deserialize Task<CachedContent>.");
}

Expand Down Expand Up @@ -1037,8 +1043,11 @@ public async Task<DeleteCachedContentResponse> DeleteAsync(
ApiResponse response = await this._apiClient.RequestAsync(
HttpMethod.Delete, path, JsonSerializer.Serialize(body), requestHttpOptions);
HttpContent httpContent = response.GetEntity();
string contentString = await httpContent.ReadAsStringAsync();
JsonNode? httpContentNode = JsonNode.Parse(contentString);
JsonNode? httpContentNode;
using (var stream = await httpContent.ReadAsStreamAsync())
{
httpContentNode = await JsonNode.ParseAsync(stream);
}
if (httpContentNode == null) {
throw new NotSupportedException("Failed to parse response to JsonNode.");
}
Expand All @@ -1052,7 +1061,7 @@ public async Task<DeleteCachedContentResponse> DeleteAsync(
responseNode = DeleteCachedContentResponseFromMldev(httpContentNode, new JsonObject());
}

return JsonSerializer.Deserialize<DeleteCachedContentResponse>(responseNode.ToString()) ??
return JsonSerializer.Deserialize<DeleteCachedContentResponse>(responseNode) ??
throw new InvalidOperationException(
"Failed to deserialize Task<DeleteCachedContentResponse>.");
}
Expand Down Expand Up @@ -1098,8 +1107,11 @@ public async Task<CachedContent> UpdateAsync(string name,
ApiResponse response = await this._apiClient.RequestAsync(
new HttpMethod("PATCH"), path, JsonSerializer.Serialize(body), requestHttpOptions);
HttpContent httpContent = response.GetEntity();
string contentString = await httpContent.ReadAsStringAsync();
JsonNode? httpContentNode = JsonNode.Parse(contentString);
JsonNode? httpContentNode;
using (var stream = await httpContent.ReadAsStreamAsync())
{
httpContentNode = await JsonNode.ParseAsync(stream);
}
if (httpContentNode == null) {
throw new NotSupportedException("Failed to parse response to JsonNode.");
}
Expand All @@ -1113,7 +1125,7 @@ public async Task<CachedContent> UpdateAsync(string name,
responseNode = httpContentNode;
}

return JsonSerializer.Deserialize<CachedContent>(responseNode.ToString()) ??
return JsonSerializer.Deserialize<CachedContent>(responseNode) ??
throw new InvalidOperationException("Failed to deserialize Task<CachedContent>.");
}

Expand Down Expand Up @@ -1153,8 +1165,11 @@ private async Task<ListCachedContentsResponse> PrivateListAsync(
ApiResponse response = await this._apiClient.RequestAsync(
HttpMethod.Get, path, JsonSerializer.Serialize(body), requestHttpOptions);
HttpContent httpContent = response.GetEntity();
string contentString = await httpContent.ReadAsStringAsync();
JsonNode? httpContentNode = JsonNode.Parse(contentString);
JsonNode? httpContentNode;
using (var stream = await httpContent.ReadAsStreamAsync())
{
httpContentNode = await JsonNode.ParseAsync(stream);
}
if (httpContentNode == null) {
throw new NotSupportedException("Failed to parse response to JsonNode.");
}
Expand All @@ -1168,7 +1183,7 @@ private async Task<ListCachedContentsResponse> PrivateListAsync(
responseNode = ListCachedContentsResponseFromMldev(httpContentNode, new JsonObject());
}

return JsonSerializer.Deserialize<ListCachedContentsResponse>(responseNode.ToString()) ??
return JsonSerializer.Deserialize<ListCachedContentsResponse>(responseNode) ??
throw new InvalidOperationException(
"Failed to deserialize Task<ListCachedContentsResponse>.");
}
Expand Down
38 changes: 25 additions & 13 deletions Google.GenAI/Files.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,11 @@ private async Task<ListFilesResponse> PrivateListAsync(ListFilesConfig? config)
ApiResponse response = await this._apiClient.RequestAsync(
HttpMethod.Get, path, JsonSerializer.Serialize(body), requestHttpOptions);
HttpContent httpContent = response.GetEntity();
string contentString = await httpContent.ReadAsStringAsync();
JsonNode? httpContentNode = JsonNode.Parse(contentString);
JsonNode? httpContentNode;
using (var stream = await httpContent.ReadAsStreamAsync())
{
httpContentNode = await JsonNode.ParseAsync(stream);
}
if (httpContentNode == null) {
throw new NotSupportedException("Failed to parse response to JsonNode.");
}
Expand All @@ -197,7 +200,7 @@ private async Task<ListFilesResponse> PrivateListAsync(ListFilesConfig? config)
responseNode = httpContentNode;
}

return JsonSerializer.Deserialize<ListFilesResponse>(responseNode.ToString()) ??
return JsonSerializer.Deserialize<ListFilesResponse>(responseNode) ??
throw new InvalidOperationException("Failed to deserialize Task<ListFilesResponse>.");
}

Expand Down Expand Up @@ -272,7 +275,7 @@ private async Task<CreateFileResponse> PrivateCreateAsync(Google.GenAI.Types.Fil
responseNode = httpContentNode;
}

return JsonSerializer.Deserialize<CreateFileResponse>(responseNode.ToString()) ??
return JsonSerializer.Deserialize<CreateFileResponse>(responseNode) ??
throw new InvalidOperationException("Failed to deserialize Task<CreateFileResponse>.");
}

Expand Down Expand Up @@ -313,8 +316,11 @@ private async Task<CreateFileResponse> PrivateCreateAsync(Google.GenAI.Types.Fil
ApiResponse response = await this._apiClient.RequestAsync(
HttpMethod.Get, path, JsonSerializer.Serialize(body), requestHttpOptions);
HttpContent httpContent = response.GetEntity();
string contentString = await httpContent.ReadAsStringAsync();
JsonNode? httpContentNode = JsonNode.Parse(contentString);
JsonNode? httpContentNode;
using (var stream = await httpContent.ReadAsStreamAsync())
{
httpContentNode = await JsonNode.ParseAsync(stream);
}
if (httpContentNode == null) {
throw new NotSupportedException("Failed to parse response to JsonNode.");
}
Expand All @@ -329,7 +335,7 @@ private async Task<CreateFileResponse> PrivateCreateAsync(Google.GenAI.Types.Fil
responseNode = httpContentNode;
}

return JsonSerializer.Deserialize<Google.GenAI.Types.File>(responseNode.ToString()) ??
return JsonSerializer.Deserialize<Google.GenAI.Types.File>(responseNode) ??
throw new InvalidOperationException(
"Failed to deserialize Task<Google.GenAI.Types.File>.");
}
Expand Down Expand Up @@ -372,8 +378,11 @@ public async Task<DeleteFileResponse> DeleteAsync(string name,
ApiResponse response = await this._apiClient.RequestAsync(
HttpMethod.Delete, path, JsonSerializer.Serialize(body), requestHttpOptions);
HttpContent httpContent = response.GetEntity();
string contentString = await httpContent.ReadAsStringAsync();
JsonNode? httpContentNode = JsonNode.Parse(contentString);
JsonNode? httpContentNode;
using (var stream = await httpContent.ReadAsStreamAsync())
{
httpContentNode = await JsonNode.ParseAsync(stream);
}
if (httpContentNode == null) {
throw new NotSupportedException("Failed to parse response to JsonNode.");
}
Expand All @@ -388,7 +397,7 @@ public async Task<DeleteFileResponse> DeleteAsync(string name,
responseNode = httpContentNode;
}

return JsonSerializer.Deserialize<DeleteFileResponse>(responseNode.ToString()) ??
return JsonSerializer.Deserialize<DeleteFileResponse>(responseNode) ??
throw new InvalidOperationException("Failed to deserialize Task<DeleteFileResponse>.");
}

Expand Down Expand Up @@ -637,14 +646,17 @@ public async Task DownloadToFileAsync(Google.GenAI.Types.Video video, string out

private async Task<Google.GenAI.Types.File> FileFromUploadResponseBodyAsync(
HttpContent responseContent) {
string responseString = await responseContent.ReadAsStringAsync();
JsonNode? responseNode = JsonNode.Parse(responseString);
JsonNode? responseNode;
using (var stream = await responseContent.ReadAsStreamAsync())
{
responseNode = await JsonNode.ParseAsync(stream);
}

if (responseNode?["file"] is not JsonNode fileNode) {
throw new InvalidOperationException("Upload response does not contain file object");
}

return JsonSerializer.Deserialize<Google.GenAI.Types.File>(fileNode.ToString()) ??
return JsonSerializer.Deserialize<Google.GenAI.Types.File>(fileNode) ??
throw new InvalidOperationException("Failed to deserialize File");
}

Expand Down
Loading