Table of Contents

Documentation

Please note that the code provided in this page is purely for learning purposes and is far from perfect. Remember to null-check all responses!

Setup

Add an instance of OperationsManager to your scene. You will need OAuth access tokens to authenticate with the operations API. The OAuth permissions required depend on the services you will be using.

OperationsManager

There's only one method in OperationsManager, with a few variants. The Request method is used to send requests to the operations API. The following examples will be using the Gemini API to demonstrate the capabilities of this package.

Operations.List

Lists service operations that match the specified filter in the request.

Filters are not guaranteed to work.

using Uralstech.UCloud.Operations;

private async void RunOperationsListRequest(string oauthAccessToken)
{
    Debug.Log("Listing all operations.");

    OperationsListResponse response = await OperationsManager.Instance.Request<OperationsListResponse>(oauthAccessToken,
        new OperationsListRequest() // From my testing, filters don't work for this service.
        {
            // This should be replaced with the base URI to the service you are using.
            // For example, if your service's operations endpoint is: abc.com/v1/operations,
            // the URI will be abc.com/v1. If it is abc.com/v1/resource/cba/operations, the
            // URI will be abc.com/v1/resource/cba.
            BaseServiceUri = "https://generativelanguage.googleapis.com/v1",
        });

    Debug.Log($"All operations: {JsonConvert.SerializeObject(response)}");
}

Since operations on different services have different result types, there are two versions of OperationsListResponse. A non-generic version, which is being used above, OperationsListResponse, and a generic version, in the Generic namespace, OperationsListResponse<TOperation>, which can be overridden with any type that inherits from Operation<TMetadata, TResponse>.

The non-generic version overrides OperationsListResponse<TOperation> with Operation, which uses the ProtobufObject type as the data. An example of Operation<TMetadata, TResponse> being overridden is GeminiTunedModelCreateResponse Where TMetadata is GeminiTunedModelCreationOperationMetadata and TResponse is GeminiTunedModel.

See OperationsListRequest for more details.

Operations.Get

Gets the latest state of a long-running operation.

using Uralstech.UCloud.Operations;

private async void RunOperationGetRequest(string oauthAccessToken, string operationResourceName)
{
    Debug.Log("Getting operation.");

    Operation response = await OperationsManager.Instance.Request<Operation>(oauthAccessToken,
        new OperationGetRequest(operationResourceName)
        {
            BaseServiceUri = "https://generativelanguage.googleapis.com/v1",
        });

    Debug.Log($"Got operation: {JsonConvert.SerializeObject(response)}");
}

You can use Operation<TMetadata, TResponse> instead of Operation, just like for OperationsListRequest.

See OperationGetRequest for more details.

Operations.Cancel

Cancels an ongoing long-running operation.

This is not supported for all services.

using Uralstech.UCloud.Operations;

private async void RunOperationCancelRequest(string oauthAccessToken, string operationResourceName)
{
    Debug.Log("Cancelling operation.");

    await OperationsManager.Instance.Request(oauthAccessToken,
        new OperationCancelRequest(operationResourceName)
        {
            BaseServiceUri = "https://generativelanguage.googleapis.com/v1",
        });

    Debug.Log("Operation cancelled.");
}

See OperationCancelRequest for more details.

Operations.Delete

Deletes an ongoing long-running operation. This may or may not actually stop the operation process.

This is not supported for all services.

using Uralstech.UCloud.Operations;

private async void RunOperationDeleteRequest(string oauthAccessToken, string operationResourceName)
{
    Debug.Log("Deleting operation.");

    await OperationsManager.Instance.Request(oauthAccessToken,
        new OperationDeleteRequest(operationResourceName)
        {
            // generativelanguage.googleapis.com does not support this operation!
            BaseServiceUri = "https://abcdefg.someuri.com/v90",
        });

    Debug.Log("Operation deleted.");
}

See OperationDeleteRequest for more details.