PlasticScmRestApiClient.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using Unity.Plastic.Newtonsoft.Json;
  5. using Codice.Client.Common.WebApi;
  6. using Codice.CM.Common;
  7. using Codice.LogWrapper;
  8. using PlasticGui.Help.NewVersions;
  9. using PlasticGui.WebApi.Responses;
  10. namespace Unity.PlasticSCM.Editor.WebApi
  11. {
  12. internal static class PlasticScmRestApiClient
  13. {
  14. internal static UnityPackageBetaEnrollResponse IsBetaEnabled(string bearerToken)
  15. {
  16. Uri endpoint = mWebApiUris.GetFullUri(IsBetaEnabledEndpoint);
  17. try
  18. {
  19. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(endpoint);
  20. request.Method = "GET";
  21. request.ContentType = "application/json";
  22. request.Headers.Add(
  23. HttpRequestHeader.Authorization,
  24. string.Format("Bearer {0}", bearerToken));
  25. return GetResponse<UnityPackageBetaEnrollResponse>(request);
  26. }
  27. catch (Exception ex)
  28. {
  29. mLog.ErrorFormat(
  30. "Unable to retrieve is beta enabled '{0}': {1}",
  31. endpoint.ToString(), ex.Message);
  32. mLog.DebugFormat(
  33. "StackTrace:{0}{1}",
  34. Environment.NewLine, ex.StackTrace);
  35. return null;
  36. }
  37. }
  38. internal static TokenExchangeResponse TokenExchange(string unityAccessToken)
  39. {
  40. Uri endpoint = mWebApiUris.GetFullUri(
  41. string.Format("{0}/{1}",TokenExchangeEndpoint,unityAccessToken ));
  42. try
  43. {
  44. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(endpoint);
  45. request.Method = "GET";
  46. request.ContentType = "application/json";
  47. return GetResponse<TokenExchangeResponse>(request);
  48. }
  49. catch (Exception ex)
  50. {
  51. mLog.ErrorFormat(
  52. "Unable to exchange tokens '{0}': {1}",
  53. endpoint.ToString(), ex.Message);
  54. mLog.DebugFormat(
  55. "StackTrace:{0}{1}",
  56. Environment.NewLine, ex.StackTrace);
  57. return null;
  58. }
  59. }
  60. internal static NewVersionResponse GetLastVersion(Edition plasticEdition)
  61. {
  62. Uri endpoint = mWebApiUris.GetFullUri(
  63. WebApiEndpoints.LastVersion.NewVersion,
  64. "9.0.0.0",
  65. WebApiEndpoints.LastVersion.GetEditionString(plasticEdition),
  66. WebApiEndpoints.LastVersion.GetPlatformString());
  67. try
  68. {
  69. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(endpoint);
  70. request.Method = "GET";
  71. request.ContentType = "application/json";
  72. return GetResponse<NewVersionResponse>(request);
  73. }
  74. catch (Exception ex)
  75. {
  76. mLog.ErrorFormat(
  77. "Unable to retrieve new versions from '{0}': {1}",
  78. endpoint.ToString(), ex.Message);
  79. mLog.DebugFormat(
  80. "StackTrace:{0}{1}",
  81. Environment.NewLine, ex.StackTrace);
  82. return null;
  83. }
  84. }
  85. internal static CredentialsResponse GetCredentials(string unityToken)
  86. {
  87. Uri endpoint = mWebApiUris.GetFullUri(
  88. WebApiEndpoints.Authentication.Credentials,
  89. unityToken);
  90. try
  91. {
  92. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(endpoint);
  93. request.Method = "GET";
  94. request.ContentType = "application/json";
  95. return GetResponse<CredentialsResponse>(request);
  96. }
  97. catch (Exception ex)
  98. {
  99. return new CredentialsResponse
  100. {
  101. Error = BuildLoggedErrorFields(ex, endpoint)
  102. };
  103. }
  104. }
  105. static TRes GetResponse<TRes>(WebRequest request)
  106. {
  107. using (WebResponse response = request.GetResponse())
  108. using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  109. {
  110. string json = reader.ReadToEnd();
  111. if (string.IsNullOrEmpty(json))
  112. return default(TRes);
  113. return JsonConvert.DeserializeObject<TRes>(json);
  114. }
  115. }
  116. static ErrorResponse.ErrorFields BuildLoggedErrorFields(
  117. Exception ex, Uri endpoint)
  118. {
  119. LogException(ex, endpoint);
  120. return new ErrorResponse.ErrorFields
  121. {
  122. ErrorCode = ErrorCodes.ClientError,
  123. Message = ex.Message
  124. };
  125. }
  126. static void LogException(Exception ex, Uri endpoint)
  127. {
  128. mLog.ErrorFormat(
  129. "There was an error while calling '{0}': {1}",
  130. endpoint.ToString(), ex.Message);
  131. mLog.DebugFormat(
  132. "StackTrace:{0}{1}",
  133. Environment.NewLine, ex.StackTrace);
  134. }
  135. const string IsBetaEnabledEndpoint = "api/unity-package/beta/is-enabled";
  136. const string TokenExchangeEndpoint = "api/oauth/unityid/exchange";
  137. static readonly PlasticWebApiUris mWebApiUris = PlasticWebApiUris.BuildDefault();
  138. static readonly ILog mLog = LogManager.GetLogger("PlasticScmRestApiClient");
  139. }
  140. }