Skip to content

Commit

Permalink
Added support for delete operation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Uralstech committed Sep 7, 2024
1 parent f6bc2c6 commit 0d318dc
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 1 deletion.
Binary file not shown.

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
@@ -0,0 +1,31 @@
namespace Uralstech.UCloud.Operations
{
/// <summary>
/// Deletes a long-running operation. This method indicates that the client is no longer interested in the operation result. It does not cancel the operation.
/// </summary>
public class OperationDeleteRequest : IOperationsDeleteRequest
{
/// <summary>
/// The resource name of the operation to delete.
/// </summary>
public string OperationName;

/// <inheritdoc/>
public string BaseServiceUri { get; set; } = "https://servicemanagement.googleapis.com/v1";

/// <inheritdoc/>
public string GetEndpointUri()
{
return $"{BaseServiceUri}/{OperationName}";
}

/// <summary>
/// Creates a new <see cref="OperationDeleteRequest"/>.
/// </summary>
/// <param name="operationName">The resource name of the operation to delete.</param>
public OperationDeleteRequest(string operationName)
{
OperationName = operationName;
}
}
}

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
@@ -0,0 +1,7 @@
namespace Uralstech.UCloud.Operations
{
/// <summary>
/// All google.longrunning API DELETE requests must inherit from this interface.
/// </summary>
public interface IOperationsDeleteRequest : IOperationsRequest { }
}

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 @@ -18,6 +18,19 @@ public class OperationResponseParsingException : Exception
/// </summary>
public string DownloadedText;

/// <summary>
/// Creates a new <see cref="OperationResponseParsingException"/>.
/// </summary>
/// <param name="webRequest">The request that caused the exception.</param>
internal OperationResponseParsingException(UnityWebRequest webRequest)
: base($"Failed to parse google.longrunning API response: " +
$"Request Endpoint: {webRequest.uri.AbsolutePath} | " +
$"Downloaded Text:\n{webRequest.downloadHandler?.text}")
{
RequestEndpoint = webRequest.uri;
DownloadedText = webRequest.downloadHandler?.text;
}

/// <summary>
/// Creates a new <see cref="OperationResponseParsingException"/>.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,24 @@ public async Task<TResponse> Request<TResponse>(string accessToken, IOperationsG
return ConfirmResponse<TResponse>(webRequest);
}

/// <summary>
/// Computes a DELETE request on the google.longrunning API.
/// </summary>
/// <param name="accessToken">The OAuth Access Token to use for authentication.</param>
/// <param name="request">The request object.</param>
/// <exception cref="OperationOAuthException">Thrown if the request could not be authenticated.</exception>
/// <exception cref="OperationRequestException">Thrown if the API request fails.</exception>
/// <exception cref="OperationResponseParsingException">Thrown if the response could not be parsed.</exception>
public async Task Request(string accessToken, IOperationsDeleteRequest request)
{
string requestEndpoint = request.GetEndpointUri();

using UnityWebRequest webRequest = UnityWebRequest.Delete(requestEndpoint);
await ComputeRequest(accessToken, webRequest);

ConfirmResponse(webRequest);
}

/// <summary>
/// Sets up, sends and verifies a <see cref="UnityWebRequest"/>.
/// </summary>
Expand Down Expand Up @@ -105,6 +123,20 @@ private TResponse ConfirmResponse<TResponse>(UnityWebRequest request)
throw new OperationResponseParsingException(request, e);
}
}

/// <summary>
/// Checks if the downloaded response was empty, as to be expected of some endpoints.
/// </summary>
/// <param name="request">The web request.</param>
/// <exception cref="OperationResponseParsingException">Thrown if the response was not empty.</exception>
private void ConfirmResponse(UnityWebRequest request)
{
if (!string.IsNullOrEmpty(request.downloadHandler?.text))
{
Debug.LogError($"Failed to confirm successful API response:\n{request.downloadHandler?.text}");
throw new OperationResponseParsingException(request);
}
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"Cloud",
"Integration"
],
"version": "1.0.0",
"version": "1.1.0-preview.1",
"unity": "2022.3",
"hideInEditor": false,
"documentationUrl": "https://uralstech.github.io/UCloud.Operations/",
Expand Down

0 comments on commit 0d318dc

Please sign in to comment.