using System; using System.Net; using System.Net.Http; using System.Threading; using System.Threading.Tasks; using Edgegap.Editor.Api.Models.Requests; using Edgegap.Editor.Api.Models.Results; using UnityEngine.Assertions; namespace Edgegap.Editor.Api { /// /// Wraps the v1/[deploy | status | stop] API endpoints: Deployments Control API. /// - API Doc | https://docs.edgegap.com/api/#tag/Deployments /// public class EdgegapDeploymentsApi : EdgegapApiBase { public EdgegapDeploymentsApi( ApiEnvironment apiEnvironment, string apiToken, EdgegapWindowMetadata.LogLevel logLevel = EdgegapWindowMetadata.LogLevel.Error) : base(apiEnvironment, apiToken, logLevel) { } #region API Methods /// /// POST v1/deploy /// - Create a new deployment. Deployment is a server instance of your application version. /// - API Doc | https://docs.edgegap.com/api/#tag/Deployments /// /// /// Http info with CreateDeploymentResult data model /// - Success: 200 /// public async Task> CreateDeploymentAsync( CreateDeploymentRequest request) { HttpResponseMessage response = await PostAsync("v1/deploy", request.ToString()); EdgegapHttpResult result = new EdgegapHttpResult(response); // MIRROR CHANGE: 'new()' not supported in Unity 2020 bool isSuccess = response.StatusCode == HttpStatusCode.OK; // 200 if (!isSuccess) return result; return result; } /// /// GET v1/status/{requestId} /// - Retrieve the information for a deployment. /// - API Doc | https://docs.edgegap.com/api/#tag/Deployments/operation/deployment-status-get /// /// /// Unique Identifier to keep track of your request across all Arbitrium ecosystem. /// It's included in the response of the app deploy. Ex: "93924761ccde" /// /// Http info with GetDeploymentStatusResult data model /// - Success: 200 /// public async Task> GetDeploymentStatusAsync(string requestId) { HttpResponseMessage response = await GetAsync($"v1/status/{requestId}"); EdgegapHttpResult result = new EdgegapHttpResult(response); // MIRROR CHANGE: 'new()' not supported in Unity 2020 bool isSuccess = response.StatusCode == HttpStatusCode.OK; // 200 if (!isSuccess) return result; return result; } /// /// DELETE v1/stop/{requestId} /// - Delete an instance of deployment. It will stop the running container and all its games. /// - API Doc | https://docs.edgegap.com/api/#tag/Deployments/operation/deployment-status-get /// /// /// Unique Identifier to keep track of your request across all Arbitrium ecosystem. /// It's included in the response of the app deploy. Ex: "93924761ccde" /// /// Http info with GetDeploymentStatusResult data model /// - Success: 200 /// public async Task> StopActiveDeploymentAsync(string requestId) { HttpResponseMessage response = await DeleteAsync($"v1/stop/{requestId}"); EdgegapHttpResult result = new EdgegapHttpResult(response); // MIRROR CHANGE: 'new()' not supported in Unity 2020 bool isSuccess = response.StatusCode == HttpStatusCode.OK; // 200 if (!isSuccess) return result; return result; } #endregion // API Methods #region Chained API Methods /// /// POST v1/deploy => GET v1/status/{requestId} /// - Create a new deployment. Deployment is a server instance of your application version. /// - Then => await READY status. /// - API Doc | https://docs.edgegap.com/api/#tag/Deployments /// /// /// Http info with CreateDeploymentResult data model (with a READY deployment status) /// - Success: 200 /// - Error: If createResult.HasErr, returns createResult /// public async Task> CreateDeploymentAwaitReadyStatusAsync( CreateDeploymentRequest request, TimeSpan pollInterval) { EdgegapHttpResult createResponse = await CreateDeploymentAsync(request); // Create => bool isCreateSuccess = createResponse.StatusCode == HttpStatusCode.OK; // 200 if (!isCreateSuccess) return createResponse; // Await Status READY => string requestId = createResponse.Data.RequestId; _ = await AwaitReadyStatusAsync(requestId, pollInterval); // Return no matter what the result; no need to validate return createResponse; } /// If you recently deployed but want to await READY status. /// /// public async Task> AwaitReadyStatusAsync( string requestId, TimeSpan pollInterval) { Assert.IsTrue(!string.IsNullOrEmpty(requestId)); // Validate EdgegapHttpResult statusResponse = null; CancellationTokenSource cts = new CancellationTokenSource (TimeSpan.FromMinutes( // MIRROR CHANGE: 'new()' not supported in Unity 2020 EdgegapWindowMetadata.DEPLOYMENT_AWAIT_READY_STATUS_TIMEOUT_MINS)); bool isReady = false; while (!isReady && !cts.Token.IsCancellationRequested) { await Task.Delay(pollInterval, cts.Token); statusResponse = await GetDeploymentStatusAsync(requestId); isReady = statusResponse.Data.CurrentStatus == EdgegapWindowMetadata.READY_STATUS; } return statusResponse; } /// If you recently stopped a deployment, but want to await TERMINATED (410) status. /// /// public async Task> AwaitTerminatedDeleteStatusAsync( string requestId, TimeSpan pollInterval) { EdgegapHttpResult deleteResponse = null; CancellationTokenSource cts = new CancellationTokenSource(TimeSpan.FromMinutes( // MIRROR CHANGE: 'new()' not supported in Unity 2020 EdgegapWindowMetadata.DEPLOYMENT_AWAIT_READY_STATUS_TIMEOUT_MINS)); bool isStopped = false; while (!isStopped && !cts.Token.IsCancellationRequested) { await Task.Delay(pollInterval, cts.Token); deleteResponse = await StopActiveDeploymentAsync(requestId); isStopped = deleteResponse.StatusCode == HttpStatusCode.Gone; // 410 } return deleteResponse; } #endregion Chained API Methods } }