No Description
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.

WebRestApiClient.cs 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 WebRestApiClient
  13. {
  14. internal static class PlasticScm
  15. {
  16. internal static TokenExchangeResponse TokenExchange(string unityAccessToken)
  17. {
  18. Uri endpoint = mWebApiUris.GetFullUri(
  19. string.Format(TokenExchangeEndpoint, unityAccessToken));
  20. try
  21. {
  22. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(endpoint);
  23. request.Method = "GET";
  24. request.ContentType = "application/json";
  25. return GetResponse<TokenExchangeResponse>(request);
  26. }
  27. catch (Exception ex)
  28. {
  29. mLog.ErrorFormat(
  30. "Unable to exchange tokens '{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 IsCollabProjectMigratedResponse IsCollabProjectMigrated(string bearerToken, string projectId)
  39. {
  40. Uri endpoint = mWebApiUris.GetFullUri(string.Format(
  41. IsCollabProjectMigratedEndpoint, projectId));
  42. try
  43. {
  44. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(endpoint);
  45. request.Method = "GET";
  46. request.ContentType = "application/json";
  47. request.Headers.Add(
  48. HttpRequestHeader.Authorization,
  49. string.Format("Bearer {0}", bearerToken));
  50. return GetResponse<IsCollabProjectMigratedResponse>(request);
  51. }
  52. catch (Exception ex)
  53. {
  54. mLog.ErrorFormat(
  55. "Unable to retrieve is collab migrated '{0}': {1}",
  56. endpoint.ToString(), ex.Message);
  57. mLog.DebugFormat(
  58. "StackTrace:{0}{1}",
  59. Environment.NewLine, ex.StackTrace);
  60. return null;
  61. }
  62. }
  63. internal static NewVersionResponse GetLastVersion(Edition plasticEdition)
  64. {
  65. Uri endpoint = mWebApiUris.GetFullUri(
  66. WebApiEndpoints.LastVersion.NewVersion,
  67. "9.0.0.0",
  68. WebApiEndpoints.LastVersion.GetEditionString(plasticEdition),
  69. WebApiEndpoints.LastVersion.GetPlatformString());
  70. try
  71. {
  72. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(endpoint);
  73. request.Method = "GET";
  74. request.ContentType = "application/json";
  75. return GetResponse<NewVersionResponse>(request);
  76. }
  77. catch (Exception ex)
  78. {
  79. mLog.ErrorFormat(
  80. "Unable to retrieve new versions from '{0}': {1}",
  81. endpoint.ToString(), ex.Message);
  82. mLog.DebugFormat(
  83. "StackTrace:{0}{1}",
  84. Environment.NewLine, ex.StackTrace);
  85. return null;
  86. }
  87. }
  88. internal static CredentialsResponse GetCredentials(string unityToken)
  89. {
  90. Uri endpoint = mWebApiUris.GetFullUri(
  91. WebApiEndpoints.Authentication.Credentials,
  92. unityToken);
  93. try
  94. {
  95. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(endpoint);
  96. request.Method = "GET";
  97. request.ContentType = "application/json";
  98. return GetResponse<CredentialsResponse>(request);
  99. }
  100. catch (Exception ex)
  101. {
  102. return new CredentialsResponse
  103. {
  104. Error = BuildLoggedErrorFields(ex, endpoint)
  105. };
  106. }
  107. }
  108. internal static CurrentUserAdminCheckResponse IsUserAdmin(
  109. string organizationName,
  110. string authToken)
  111. {
  112. Uri endpoint = mWebApiUris.GetFullUri(
  113. IsUserAdminEnpoint,
  114. organizationName);
  115. try
  116. {
  117. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(endpoint);
  118. request.Method = "GET";
  119. request.ContentType = "application/json";
  120. string authenticationToken = "Basic " + authToken;
  121. request.Headers.Add(
  122. HttpRequestHeader.Authorization, authenticationToken);
  123. return GetResponse<CurrentUserAdminCheckResponse>(request);
  124. }
  125. catch (Exception ex)
  126. {
  127. mLog.ErrorFormat(
  128. "Unable to retrieve is user admin '{0}': {1}",
  129. endpoint.ToString(), ex.Message);
  130. mLog.DebugFormat(
  131. "StackTrace:{0}{1}",
  132. Environment.NewLine, ex.StackTrace);
  133. return new CurrentUserAdminCheckResponse
  134. {
  135. Error = BuildLoggedErrorFields(ex, endpoint)
  136. };
  137. }
  138. }
  139. const string IsBetaEnabledEndpoint = "api/unity-package/beta/is-enabled";
  140. const string TokenExchangeEndpoint = "api/oauth/unityid/exchange/{0}";
  141. const string IsCollabProjectMigratedEndpoint = "api/cloud/unity/projects/{0}/is-migrated";
  142. const string IsUserAdminEnpoint = "api/cloud/organizations/{0}/is-user-admin";
  143. static readonly PlasticWebApiUris mWebApiUris = PlasticWebApiUris.BuildDefault();
  144. }
  145. internal static class CloudServer
  146. {
  147. internal static string WebLogin(
  148. string webServerUri,
  149. string organizationName,
  150. OrganizationCredentials credentials)
  151. {
  152. Uri endpoint = new Uri(
  153. new Uri(webServerUri),
  154. string.Format(WebLoginEndPoint, organizationName));
  155. try
  156. {
  157. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(endpoint);
  158. request.Method = "POST";
  159. request.ContentType = "application/json";
  160. request.Timeout = 5000;
  161. WriteBody(request, credentials);
  162. return GetResponse<string>(request);
  163. }
  164. catch (Exception ex)
  165. {
  166. mLog.ErrorFormat(
  167. "Unable to retrieve the organization login '{0}': {1}",
  168. endpoint.ToString(), ex.Message);
  169. mLog.DebugFormat(
  170. "StackTrace:{0}{1}",
  171. Environment.NewLine, ex.StackTrace);
  172. return null;
  173. }
  174. }
  175. internal static ChangesetFromCollabCommitResponse GetChangesetFromCollabCommit(
  176. string webServerUri,
  177. string organizationName,
  178. string webLoginAccessToken,
  179. string projectId,
  180. string commitSha)
  181. {
  182. Uri endpoint = new Uri(
  183. new Uri(webServerUri),
  184. string.Format(GetChangesetFromCollabCommitEndpoint,
  185. organizationName, projectId, commitSha));
  186. try
  187. {
  188. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(endpoint);
  189. request.Method = "GET";
  190. request.ContentType = "application/json";
  191. request.Headers.Add(
  192. HttpRequestHeader.Authorization,
  193. string.Format("Bearer {0}", webLoginAccessToken));
  194. return GetResponse<ChangesetFromCollabCommitResponse>(request);
  195. }
  196. catch (Exception ex)
  197. {
  198. mLog.ErrorFormat(
  199. "Unable to retrieve the changeset from collab commit '{0}': {1}",
  200. endpoint.ToString(), ex.Message);
  201. mLog.DebugFormat(
  202. "StackTrace:{0}{1}",
  203. Environment.NewLine, ex.StackTrace);
  204. return null;
  205. }
  206. }
  207. const string WebLoginEndPoint = "api/v1/organizations/{0}/login/accesstoken";
  208. const string GetChangesetFromCollabCommitEndpoint = "cloudapi/v1/organizations/{0}/repos/{1}/collabcommit/{2}/changeset";
  209. }
  210. static void WriteBody(WebRequest request, object body)
  211. {
  212. using (Stream st = request.GetRequestStream())
  213. using (StreamWriter writer = new StreamWriter(st))
  214. {
  215. writer.Write(JsonConvert.SerializeObject(body));
  216. }
  217. }
  218. static TRes GetResponse<TRes>(WebRequest request)
  219. {
  220. using (WebResponse response = request.GetResponse())
  221. using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  222. {
  223. string json = reader.ReadToEnd();
  224. if (string.IsNullOrEmpty(json))
  225. return default(TRes);
  226. return JsonConvert.DeserializeObject<TRes>(json);
  227. }
  228. }
  229. static ErrorResponse.ErrorFields BuildLoggedErrorFields(
  230. Exception ex, Uri endpoint)
  231. {
  232. LogException(ex, endpoint);
  233. return new ErrorResponse.ErrorFields
  234. {
  235. ErrorCode = ErrorCodes.ClientError,
  236. Message = ex.Message
  237. };
  238. }
  239. static void LogException(Exception ex, Uri endpoint)
  240. {
  241. mLog.ErrorFormat(
  242. "There was an error while calling '{0}': {1}",
  243. endpoint.ToString(), ex.Message);
  244. mLog.DebugFormat(
  245. "StackTrace:{0}{1}",
  246. Environment.NewLine, ex.StackTrace);
  247. }
  248. static readonly ILog mLog = LogManager.GetLogger("WebRestApiClient");
  249. }
  250. }