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 12KB

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