Ei kuvausta
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RequestGameIds.cs 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #if SERVICES_SDK_CORE_ENABLED
  2. using System;
  3. using System.Text;
  4. using JetBrains.Annotations;
  5. using UnityEditor;
  6. using UnityEngine.Networking;
  7. namespace UnityEngine.Advertisements.Editor
  8. {
  9. class RequestGameIds
  10. {
  11. [Serializable]
  12. public class Response
  13. {
  14. public string iOSGameKey;
  15. public string androidGameKey;
  16. }
  17. [Serializable]
  18. class Body
  19. {
  20. public string projectGUID;
  21. [CanBeNull]
  22. public string projectName;
  23. public string token;
  24. }
  25. const string k_ProductionDomain = "https://legacy-editor-integration.dashboard.unity3d.com";
  26. const string k_GameIdApiUrl = "/unity/v1/games/";
  27. public void SendWithRetry(Action<Response> onSuccess, Action<Exception> onError, int retryDelayInSeconds = 1, int maxRetryCount = 10)
  28. {
  29. var currentRetry = 0;
  30. var timer = new EditorTimer
  31. {
  32. IntervalInSeconds = retryDelayInSeconds
  33. };
  34. timer.Elapsed += OnTimerElapsed;
  35. SendAndMonitorRequest();
  36. void SendAndMonitorRequest()
  37. {
  38. Send(OnRequestCompletedSuccess, OnRequestCompletedError);
  39. }
  40. void OnRequestCompletedSuccess(Response response)
  41. {
  42. onSuccess?.Invoke(response);
  43. }
  44. void OnRequestCompletedError(Exception exception)
  45. {
  46. if (currentRetry < maxRetryCount)
  47. {
  48. timer.Restart();
  49. }
  50. else
  51. {
  52. onError?.Invoke(exception);
  53. }
  54. }
  55. void OnTimerElapsed()
  56. {
  57. ++currentRetry;
  58. SendAndMonitorRequest();
  59. }
  60. }
  61. public void Send(Action<Response> onSuccess, Action<Exception> onError)
  62. {
  63. try
  64. {
  65. var webRequest = CreateWebRequest(k_ProductionDomain);
  66. webRequest.SendWebRequest().completed += OnUnityWebRequestCompleted;
  67. }
  68. catch (Exception e)
  69. {
  70. onError?.Invoke(e);
  71. }
  72. void OnUnityWebRequestCompleted(AsyncOperation webOperation)
  73. {
  74. using (var webRequest = ((UnityWebRequestAsyncOperation)webOperation).webRequest)
  75. {
  76. var hasRequestFailed = false;
  77. #if UNITY_2020_2_OR_NEWER
  78. hasRequestFailed = webRequest.result != UnityWebRequest.Result.Success;
  79. #else
  80. hasRequestFailed = webRequest.isNetworkError
  81. || webRequest.isHttpError;
  82. #endif
  83. if (hasRequestFailed)
  84. {
  85. var message = "Couldn't fetch Ads Service game Ids.\n" +
  86. $"Error: {webRequest.error}" +
  87. $"Message: {webRequest.downloadHandler.text}";
  88. onError?.Invoke(new Exception(message));
  89. return;
  90. }
  91. var response = JsonUtility.FromJson<Response>(webRequest.downloadHandler.text);
  92. onSuccess?.Invoke(response);
  93. }
  94. }
  95. }
  96. static UnityWebRequest CreateWebRequest(string environmentDomain)
  97. {
  98. var body = new Body
  99. {
  100. projectGUID = CloudProjectSettings.projectId,
  101. projectName = CloudProjectSettings.projectName,
  102. token = CloudProjectSettings.accessToken
  103. };
  104. var serializeBody = JsonUtility.ToJson(body);
  105. var webRequest = new UnityWebRequest(environmentDomain + k_GameIdApiUrl, UnityWebRequest.kHttpVerbPOST)
  106. {
  107. downloadHandler = new DownloadHandlerBuffer(),
  108. uploadHandler = new UploadHandlerRaw(Encoding.UTF8.GetBytes(serializeBody))
  109. };
  110. webRequest.SetRequestHeader("Content-Type", "application/json;charset=UTF-8");
  111. return webRequest;
  112. }
  113. }
  114. }
  115. #endif