Skip to content

Commit

Permalink
Fixed cachedContents.patch.
Browse files Browse the repository at this point in the history
  • Loading branch information
Uralstech committed Aug 30, 2024
1 parent 60d3610 commit cd987a4
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Uralstech.UGemini.FileAPI
public class GeminiFileUploadMetaData
{
/// <summary>
/// The <see cref="GeminiFileUploadRequest"/> resource name. This does not work right now.
/// The <see cref="GeminiFileUploadRequest"/> resource name, in format "files/{fileId}".
/// </summary>
/// <remarks>
/// The ID (name excluding the "files/" prefix) can contain up to 40 characters that are lowercase alphanumeric or dashes (-).<br/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
using System.ComponentModel;

namespace Uralstech.UGemini.Models.Caching
{
/// <summary>
/// Data to patch an existing cached content resource with new data.
/// </summary>
[JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))]
public class GeminiCachedContentPatchData
{
/// <summary>
/// Timestamp in UTC of when this resource is considered expired.
/// </summary>
/// <remarks>
/// If not provided, <see cref="TimeToLive"/> must be provided.
/// </remarks>
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore), DefaultValue(null)]
public DateTime? ExpireTime = null;

/// <summary>
/// New TTL for this resource.
/// </summary>
/// <remarks>
/// If not provided, <see cref="ExpireTime"/> must be provided.
/// </remarks>
[JsonProperty("ttl", DefaultValueHandling = DefaultValueHandling.Ignore), JsonConverter(typeof(GeminiSecondsToTimeSpanJsonConverter)), DefaultValue(null)]
public TimeSpan? TimeToLive = null;
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
namespace Uralstech.UGemini.Models.Caching
{
/// <summary>
/// Patches a <see cref="GeminiCachedContent"/> resource.
/// Patches a <see cref="GeminiCachedContent"/> resource. Response type is <see cref="GeminiCachedContent"/>.
/// </summary>
/// <remarks>
/// Only available in the beta API.
/// </remarks>
public class GeminiCachedContentPatchRequest : IGeminiPatchRequest
{
/// <summary>
/// The patch content.
/// The patch data.
/// </summary>
public GeminiCachedContent Content;
public GeminiCachedContentPatchData Patch;

/// <summary>
/// The ID of the cached content.
Expand All @@ -40,20 +40,20 @@ public string GetEndpointUri(GeminiRequestMetadata metadata)
/// <remarks>
/// Only available in the beta API.
/// </remarks>
/// <param name="content">The patch content.</param>
/// <param name="patch">The patch data.</param>
/// <param name="cachedContentIdOrName">The ID or name (format cachedContents/{contentId}) of the cached content to patch.</param>
/// <param name="useBetaApi">Should the request use the Beta API?</param>
public GeminiCachedContentPatchRequest(GeminiCachedContent content, string cachedContentIdOrName, bool useBetaApi = true)
public GeminiCachedContentPatchRequest(GeminiCachedContentPatchData patch, string cachedContentIdOrName, bool useBetaApi = true)
{
Content = content;
Patch = patch;
ContentId = cachedContentIdOrName.Split('/')[^1];
ApiVersion = useBetaApi ? "v1beta" : "v1";
}

/// <inheritdoc/>
public string GetUtf8EncodedData()
{
return JsonConvert.SerializeObject(Content);
return JsonConvert.SerializeObject(Patch);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public async Task<TResponse> Request<TResponse>(IGeminiMultiPartPostRequest requ
/// </typeparam>
///
/// <param name="request">The request object.</param>
/// <returns>The computed response.</returns>
/// <exception cref="GeminiRequestException">Thrown when the API request fails.</exception>
public async Task<TResponse> Request<TResponse>(IGeminiGetRequest request)
{
Expand Down Expand Up @@ -116,18 +117,34 @@ public async Task Request(IGeminiDeleteRequest request)
/// <summary>
/// Computes a PATCH request on the Gemini API.
/// </summary>
///
/// <typeparam name="TResponse">
/// The response type. For example, a request of type <see cref="Models.Generation.Chat.GeminiChatRequest"/> corresponds
/// to a response type of <see cref="Models.Generation.Chat.GeminiChatResponse"/>, and a request of type <see cref="Models.CountTokens.GeminiTokenCountRequest"/>
/// corresponds to a response of type <see cref="Models.CountTokens.GeminiTokenCountResponse"/>.
/// </typeparam>
///
/// <param name="request">The request object.</param>
/// <returns>The computed response.</returns>
/// <exception cref="GeminiRequestException">Thrown when the API request fails.</exception>
public async Task Request(IGeminiPatchRequest request)
public async Task<TResponse> Request<TResponse>(IGeminiPatchRequest request)
{
string utf8RequestData = request.GetUtf8EncodedData();
string requestEndpoint = request.GetEndpointUri(null);

using UnityWebRequest webRequest = new(requestEndpoint, "PATCH");
using UnityWebRequest webRequest = new(
requestEndpoint, "PATCH",
new DownloadHandlerBuffer(),
new UploadHandlerRaw(Encoding.UTF8.GetBytes(utf8RequestData))
{
contentType = request.ContentType
}
);

webRequest.uploadHandler = new UploadHandlerRaw(Encoding.UTF8.GetBytes(utf8RequestData));
webRequest.SetRequestHeader("Content-Type", request.ContentType);
await ComputeRequest(webRequest);

return JsonConvert.DeserializeObject<TResponse>(webRequest.downloadHandler.text);
}

#if UTILITIES_ASYNC_1_0_0_OR_GREATER
Expand All @@ -145,6 +162,7 @@ public async Task Request(IGeminiPatchRequest request)
/// </typeparam>
///
/// <param name="request">The request object.</param>
/// <returns>The computed response.</returns>
/// <exception cref="GeminiRequestException">Thrown when the API request fails.</exception>
public async Task<TResponse> StreamRequest<TResponse>(IGeminiStreamablePostRequest<TResponse> request)
where TResponse : IAppendableData<TResponse>
Expand Down
2 changes: 1 addition & 1 deletion UGemini/Packages/com.uralstech.ugemini/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"AI",
"Integration"
],
"version": "2.0.0-preview.5",
"version": "2.0.0-preview.6",
"unity": "2022.3",
"hideInEditor": false,
"documentationUrl": "https://github.com/Uralstech/UGemini/blob/master/UGemini/Packages/com.uralstech.ugemini/Documentation~/README.md",
Expand Down

0 comments on commit cd987a4

Please sign in to comment.