Geen omschrijving
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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 CurrentUserAdminCheckResponse IsUserAdmin(
  88. string organizationName,
  89. string authToken)
  90. {
  91. Uri endpoint = mWebApiUris.GetFullUri(
  92. IsUserAdminEnpoint,
  93. organizationName);
  94. try
  95. {
  96. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(endpoint);
  97. request.Method = "GET";
  98. request.ContentType = "application/json";
  99. string authenticationToken = "Basic " + authToken;
  100. request.Headers.Add(
  101. HttpRequestHeader.Authorization, authenticationToken);
  102. return GetResponse<CurrentUserAdminCheckResponse>(request);
  103. }
  104. catch (Exception ex)
  105. {
  106. mLog.ErrorFormat(
  107. "Unable to retrieve is user admin '{0}': {1}",
  108. endpoint.ToString(), ex.Message);
  109. mLog.DebugFormat(
  110. "StackTrace:{0}{1}",
  111. Environment.NewLine, ex.StackTrace);
  112. return new CurrentUserAdminCheckResponse
  113. {
  114. Error = BuildLoggedErrorFields(ex, endpoint)
  115. };
  116. }
  117. }
  118. internal static SubscriptionDetailsResponse GetSubscriptionDetails(
  119. string organizationName,
  120. string authToken)
  121. {
  122. Uri endpoint = mWebApiUris.GetFullUri(
  123. SubscriptionDetailsEndpoint,
  124. organizationName);
  125. try
  126. {
  127. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(endpoint);
  128. request.Method = "GET";
  129. request.ContentType = "application/json";
  130. string authenticationToken = "Basic " + authToken;
  131. request.Headers.Add(
  132. HttpRequestHeader.Authorization, authenticationToken);
  133. return GetResponse<SubscriptionDetailsResponse>(request);
  134. }
  135. catch (Exception ex)
  136. {
  137. mLog.ErrorFormat(
  138. "Unable to retrieve subscription details '{0}': {1}",
  139. endpoint.ToString(), ex.Message);
  140. mLog.DebugFormat(
  141. "StackTrace:{0}{1}",
  142. Environment.NewLine, ex.StackTrace);
  143. return null;
  144. }
  145. }
  146. const string IsBetaEnabledEndpoint = "api/unity-package/beta/is-enabled";
  147. const string TokenExchangeEndpoint = "api/oauth/unityid/exchange/{0}";
  148. const string IsCollabProjectMigratedEndpoint = "api/cloud/unity/projects/{0}/is-migrated";
  149. const string IsUserAdminEnpoint = "api/cloud/organizations/{0}/is-user-admin";
  150. const string SubscriptionDetailsEndpoint = "api/cloud/organizations/{0}/subscription-details";
  151. static readonly PlasticWebApiUris mWebApiUris = PlasticWebApiUris.BuildDefault();
  152. }
  153. internal static class CloudServer
  154. {
  155. internal static string WebLogin(
  156. string webServerUri,
  157. string organizationName,
  158. OrganizationCredentials credentials)
  159. {
  160. Uri endpoint = new Uri(
  161. new Uri(webServerUri),
  162. string.Format(WebLoginEndPoint, organizationName));
  163. try
  164. {
  165. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(endpoint);
  166. request.Method = "POST";
  167. request.ContentType = "application/json";
  168. request.Timeout = 5000;
  169. WriteBody(request, credentials);
  170. return GetResponse<string>(request);
  171. }
  172. catch (Exception ex)
  173. {
  174. mLog.ErrorFormat(
  175. "Unable to retrieve the organization login '{0}': {1}",
  176. endpoint.ToString(), ex.Message);
  177. mLog.DebugFormat(
  178. "StackTrace:{0}{1}",
  179. Environment.NewLine, ex.StackTrace);
  180. return null;
  181. }
  182. }
  183. internal static ChangesetFromCollabCommitResponse GetChangesetFromCollabCommit(
  184. string webServerUri,
  185. string organizationName,
  186. string webLoginAccessToken,
  187. string projectId,
  188. string commitSha)
  189. {
  190. Uri endpoint = new Uri(
  191. new Uri(webServerUri),
  192. string.Format(GetChangesetFromCollabCommitEndpoint,
  193. organizationName, projectId, commitSha));
  194. try
  195. {
  196. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(endpoint);
  197. request.Method = "GET";
  198. request.ContentType = "application/json";
  199. request.Headers.Add(
  200. HttpRequestHeader.Authorization,
  201. string.Format("Bearer {0}", webLoginAccessToken));
  202. return GetResponse<ChangesetFromCollabCommitResponse>(request);
  203. }
  204. catch (Exception ex)
  205. {
  206. mLog.ErrorFormat(
  207. "Unable to retrieve the changeset from collab commit '{0}': {1}",
  208. endpoint.ToString(), ex.Message);
  209. mLog.DebugFormat(
  210. "StackTrace:{0}{1}",
  211. Environment.NewLine, ex.StackTrace);
  212. return null;
  213. }
  214. }
  215. const string WebLoginEndPoint = "api/v1/organizations/{0}/login/accesstoken";
  216. const string GetChangesetFromCollabCommitEndpoint = "cloudapi/v1/organizations/{0}/repos/{1}/collabcommit/{2}/changeset";
  217. }
  218. static void WriteBody(WebRequest request, object body)
  219. {
  220. using (Stream st = request.GetRequestStream())
  221. using (StreamWriter writer = new StreamWriter(st))
  222. {
  223. writer.Write(JsonConvert.SerializeObject(body));
  224. }
  225. }
  226. static TRes GetResponse<TRes>(WebRequest request)
  227. {
  228. using (WebResponse response = request.GetResponse())
  229. using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  230. {
  231. string json = reader.ReadToEnd();
  232. if (string.IsNullOrEmpty(json))
  233. return default(TRes);
  234. return JsonConvert.DeserializeObject<TRes>(json);
  235. }
  236. }
  237. static ErrorResponse.ErrorFields BuildLoggedErrorFields(
  238. Exception ex, Uri endpoint)
  239. {
  240. LogException(ex, endpoint);
  241. return new ErrorResponse.ErrorFields
  242. {
  243. ErrorCode = ErrorCodes.ClientError,
  244. Message = ex.Message
  245. };
  246. }
  247. static void LogException(Exception ex, Uri endpoint)
  248. {
  249. mLog.ErrorFormat(
  250. "There was an error while calling '{0}': {1}",
  251. endpoint.ToString(), ex.Message);
  252. mLog.DebugFormat(
  253. "StackTrace:{0}{1}",
  254. Environment.NewLine, ex.StackTrace);
  255. }
  256. static readonly ILog mLog = PlasticApp.GetLogger("WebRestApiClient");
  257. }
  258. }