EdgegapDeploymentsApi.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using System;
  2. using System.Net;
  3. using System.Net.Http;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using Edgegap.Editor.Api.Models.Requests;
  7. using Edgegap.Editor.Api.Models.Results;
  8. using UnityEngine.Assertions;
  9. namespace Edgegap.Editor.Api
  10. {
  11. /// <summary>
  12. /// Wraps the v1/[deploy | status | stop] API endpoints: Deployments Control API.
  13. /// - API Doc | https://docs.edgegap.com/api/#tag/Deployments
  14. /// </summary>
  15. public class EdgegapDeploymentsApi : EdgegapApiBase
  16. {
  17. public EdgegapDeploymentsApi(
  18. ApiEnvironment apiEnvironment,
  19. string apiToken,
  20. EdgegapWindowMetadata.LogLevel logLevel = EdgegapWindowMetadata.LogLevel.Error)
  21. : base(apiEnvironment, apiToken, logLevel)
  22. {
  23. }
  24. #region API Methods
  25. /// <summary>
  26. /// POST v1/deploy
  27. /// - Create a new deployment. Deployment is a server instance of your application version.
  28. /// - API Doc | https://docs.edgegap.com/api/#tag/Deployments
  29. /// </summary>
  30. /// <returns>
  31. /// Http info with CreateDeploymentResult data model
  32. /// - Success: 200
  33. /// </returns>
  34. public async Task<EdgegapHttpResult<CreateDeploymentResult>> CreateDeploymentAsync(
  35. CreateDeploymentRequest request)
  36. {
  37. HttpResponseMessage response = await PostAsync("v1/deploy", request.ToString());
  38. EdgegapHttpResult<CreateDeploymentResult> result = new EdgegapHttpResult<CreateDeploymentResult>(response); // MIRROR CHANGE: 'new()' not supported in Unity 2020
  39. bool isSuccess = response.StatusCode == HttpStatusCode.OK; // 200
  40. if (!isSuccess)
  41. return result;
  42. return result;
  43. }
  44. /// <summary>
  45. /// GET v1/status/{requestId}
  46. /// - Retrieve the information for a deployment.
  47. /// - API Doc | https://docs.edgegap.com/api/#tag/Deployments/operation/deployment-status-get
  48. /// </summary>
  49. /// <param name="requestId">
  50. /// Unique Identifier to keep track of your request across all Arbitrium ecosystem.
  51. /// It's included in the response of the app deploy. Ex: "93924761ccde"</param>
  52. /// <returns>
  53. /// Http info with GetDeploymentStatusResult data model
  54. /// - Success: 200
  55. /// </returns>
  56. public async Task<EdgegapHttpResult<GetDeploymentStatusResult>> GetDeploymentStatusAsync(string requestId)
  57. {
  58. HttpResponseMessage response = await GetAsync($"v1/status/{requestId}");
  59. EdgegapHttpResult<GetDeploymentStatusResult> result = new EdgegapHttpResult<GetDeploymentStatusResult>(response); // MIRROR CHANGE: 'new()' not supported in Unity 2020
  60. bool isSuccess = response.StatusCode == HttpStatusCode.OK; // 200
  61. if (!isSuccess)
  62. return result;
  63. return result;
  64. }
  65. /// <summary>
  66. /// DELETE v1/stop/{requestId}
  67. /// - Delete an instance of deployment. It will stop the running container and all its games.
  68. /// - API Doc | https://docs.edgegap.com/api/#tag/Deployments/operation/deployment-status-get
  69. /// </summary>
  70. /// <param name="requestId">
  71. /// Unique Identifier to keep track of your request across all Arbitrium ecosystem.
  72. /// It's included in the response of the app deploy. Ex: "93924761ccde"</param>
  73. /// <returns>
  74. /// Http info with GetDeploymentStatusResult data model
  75. /// - Success: 200
  76. /// </returns>
  77. public async Task<EdgegapHttpResult<StopActiveDeploymentResult>> StopActiveDeploymentAsync(string requestId)
  78. {
  79. HttpResponseMessage response = await DeleteAsync($"v1/stop/{requestId}");
  80. EdgegapHttpResult<StopActiveDeploymentResult> result = new EdgegapHttpResult<StopActiveDeploymentResult>(response); // MIRROR CHANGE: 'new()' not supported in Unity 2020
  81. bool isSuccess = response.StatusCode == HttpStatusCode.OK; // 200
  82. if (!isSuccess)
  83. return result;
  84. return result;
  85. }
  86. #endregion // API Methods
  87. #region Chained API Methods
  88. /// <summary>
  89. /// POST v1/deploy => GET v1/status/{requestId}
  90. /// - Create a new deployment. Deployment is a server instance of your application version.
  91. /// - Then => await READY status.
  92. /// - API Doc | https://docs.edgegap.com/api/#tag/Deployments
  93. /// </summary>
  94. /// <returns>
  95. /// Http info with CreateDeploymentResult data model (with a READY deployment status)
  96. /// - Success: 200
  97. /// - Error: If createResult.HasErr, returns createResult
  98. /// </returns>
  99. public async Task<EdgegapHttpResult<CreateDeploymentResult>> CreateDeploymentAwaitReadyStatusAsync(
  100. CreateDeploymentRequest request, TimeSpan pollInterval)
  101. {
  102. EdgegapHttpResult<CreateDeploymentResult> createResponse = await CreateDeploymentAsync(request);
  103. // Create =>
  104. bool isCreateSuccess = createResponse.StatusCode == HttpStatusCode.OK; // 200
  105. if (!isCreateSuccess)
  106. return createResponse;
  107. // Await Status READY =>
  108. string requestId = createResponse.Data.RequestId;
  109. _ = await AwaitReadyStatusAsync(requestId, pollInterval);
  110. // Return no matter what the result; no need to validate
  111. return createResponse;
  112. }
  113. /// <summary>If you recently deployed but want to await READY status.</summary>
  114. /// <param name="requestId"></param>
  115. /// <param name="pollInterval"></param>
  116. public async Task<EdgegapHttpResult<GetDeploymentStatusResult>> AwaitReadyStatusAsync(
  117. string requestId,
  118. TimeSpan pollInterval)
  119. {
  120. Assert.IsTrue(!string.IsNullOrEmpty(requestId)); // Validate
  121. EdgegapHttpResult<GetDeploymentStatusResult> statusResponse = null;
  122. CancellationTokenSource cts = new CancellationTokenSource (TimeSpan.FromMinutes( // MIRROR CHANGE: 'new()' not supported in Unity 2020
  123. EdgegapWindowMetadata.DEPLOYMENT_AWAIT_READY_STATUS_TIMEOUT_MINS));
  124. bool isReady = false;
  125. while (!isReady && !cts.Token.IsCancellationRequested)
  126. {
  127. await Task.Delay(pollInterval, cts.Token);
  128. statusResponse = await GetDeploymentStatusAsync(requestId);
  129. isReady = statusResponse.Data.CurrentStatus == EdgegapWindowMetadata.READY_STATUS;
  130. }
  131. return statusResponse;
  132. }
  133. /// <summary>If you recently stopped a deployment, but want to await TERMINATED (410) status.</summary>
  134. /// <param name="requestId"></param>
  135. /// <param name="pollInterval"></param>
  136. public async Task<EdgegapHttpResult<StopActiveDeploymentResult>> AwaitTerminatedDeleteStatusAsync(
  137. string requestId,
  138. TimeSpan pollInterval)
  139. {
  140. EdgegapHttpResult<StopActiveDeploymentResult> deleteResponse = null;
  141. CancellationTokenSource cts = new CancellationTokenSource(TimeSpan.FromMinutes( // MIRROR CHANGE: 'new()' not supported in Unity 2020
  142. EdgegapWindowMetadata.DEPLOYMENT_AWAIT_READY_STATUS_TIMEOUT_MINS));
  143. bool isStopped = false;
  144. while (!isStopped && !cts.Token.IsCancellationRequested)
  145. {
  146. await Task.Delay(pollInterval, cts.Token);
  147. deleteResponse = await StopActiveDeploymentAsync(requestId);
  148. isStopped = deleteResponse.StatusCode == HttpStatusCode.Gone; // 410
  149. }
  150. return deleteResponse;
  151. }
  152. #endregion Chained API Methods
  153. }
  154. }