Connection.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Threading.Tasks;
  5. using BO.AppServer.Metadata.Dto;
  6. using BO.AppServer.Metadata.Enums;
  7. using BO.AppServer.Metadata.Extensions;
  8. using Quadarax.Foundation.Core.Data.Interface;
  9. using Quadarax.Foundation.Core.Data.Interface.Entity.Dto;
  10. using Quadarax.Foundation.Core.Logging;
  11. namespace BO.Connector.Console
  12. {
  13. public class Connection : AbstractConnection
  14. {
  15. #region *** Private fields ***
  16. protected override string ApiName => "Console";
  17. #endregion
  18. #region *** Constructors ***
  19. public Connection(Uri uriApiBase, TimeSpan timeout, ILogHandler log = null, bool isDumpContentEnabled = false) : base(uriApiBase, timeout, log, isDumpContentEnabled)
  20. {
  21. }
  22. #endregion
  23. #region *** CRUD User ***
  24. public async Task<UserRDto> GetUserAsync(IdentificationTypeEnum identificationType, string identificationValue)
  25. {
  26. CheckIsOpen();
  27. var result = await CallRequestAsync<ResultValueDto<UserRDto>>($"{Ticket}/user/{identificationType}/{identificationValue}", RestCallTypeEnum.Get);
  28. return result.Value;
  29. }
  30. public async Task<IEnumerable<UserRDto>> GetUsersAsync(UserScopeEnums scope, PagingDto paging = null)
  31. {
  32. CheckIsOpen();
  33. var result = await CallRequestAsync<ResultsValueDto<UserRDto>>($"{Ticket}/users/{scope}", RestCallTypeEnum.Get,null, GetPagingHeaderAttributes(paging));
  34. return result.Values;
  35. }
  36. public async Task<UserRDto> CreateUserAsync(UserCDto newUser)
  37. {
  38. CheckIsOpen();
  39. var result = await CallRequestAsync<ResultValueDto<UserRDto>>($"{Ticket}/user", RestCallTypeEnum.Post, newUser);
  40. return result.Value;
  41. }
  42. public async Task<UserRDto> UpdateUserAsnc(IdentificationTypeEnum identificationType, string identificationValue, UserUDto updateUser)
  43. {
  44. CheckIsOpen();
  45. var result = await CallRequestAsync<ResultValueDto<UserRDto>>($"{Ticket}/user/{identificationType}/{identificationValue}", RestCallTypeEnum.Put, updateUser);
  46. return result.Value;
  47. }
  48. public async Task<UserRDto> DeleteUserAsync(IdentificationTypeEnum identificationType, string identificationValue)
  49. {
  50. CheckIsOpen();
  51. var result = await CallRequestAsync<ResultValueDto<UserRDto>>($"{Ticket}/user/{identificationType}/{identificationValue}", RestCallTypeEnum.Delete);
  52. return result.Value;
  53. }
  54. #endregion
  55. #region *** Statistics ***
  56. public async Task<IEnumerable<StatisticsDto>> GetStatisticsAsync(StatisticsScopeEnums scope, PagingDto paging = null)
  57. {
  58. CheckIsOpen();
  59. var result = await CallRequestAsync<ResultsValueDto<StatisticsDto>>($"{Ticket}/statistics/{scope}", RestCallTypeEnum.Get,null,GetPagingHeaderAttributes(paging));
  60. return result.Values;
  61. }
  62. public async Task<IEnumerable<AssemblyInfoDto>> GetVersionsAsync()
  63. {
  64. CheckIsOpen();
  65. var result = await CallRequestAsync<ResultsValueDto<AssemblyInfoDto>>($"{Ticket}/versions", RestCallTypeEnum.Get);
  66. return result.Values;
  67. }
  68. #endregion
  69. #region *** CRUD BillingPlan ***
  70. public async Task<BillingPlanRDto> CreateBillingPlanAsync(BillingPlanCDto newBillingPlan)
  71. {
  72. CheckIsOpen();
  73. var result = await CallRequestAsync<ResultValueDto<BillingPlanRDto>>($"{Ticket}/bplan", RestCallTypeEnum.Post,newBillingPlan);
  74. return result.Value;
  75. }
  76. public async Task<BillingPlanRDto> UpdateBillingPlanAsync(BillingPlanUDto updBillingPlan)
  77. {
  78. CheckIsOpen();
  79. var result = await CallRequestAsync<ResultValueDto<BillingPlanRDto>>($"{Ticket}/bplan", RestCallTypeEnum.Put,updBillingPlan);
  80. return result.Value;
  81. }
  82. public async Task<IEnumerable<BillingPlanRDto>> GetBillingPlansAsync(BPlanScopeEnums scope, PagingDto paging = null)
  83. {
  84. CheckIsOpen();
  85. var result = await CallRequestAsync<ResultsValueDto<BillingPlanRDto>>($"{Ticket}/bplans/{scope}", RestCallTypeEnum.Get,null,GetPagingHeaderAttributes(paging));
  86. return result.Values;
  87. }
  88. #endregion
  89. #region *** CRUD MIME Type ***
  90. public async Task<MimeTypeRDto> CreateMimeTypeAsync(MimeTypeCDto newMimeType)
  91. {
  92. CheckIsOpen();
  93. var result = await CallRequestAsync<ResultValueDto<MimeTypeRDto>>($"{Ticket}/mtype", RestCallTypeEnum.Post,newMimeType);
  94. return result.Value;
  95. }
  96. public async Task<MimeTypeRDto> UpdateMimeTypeAsync(MimeTypeUDto updMimeType)
  97. {
  98. CheckIsOpen();
  99. var result = await CallRequestAsync<ResultValueDto<MimeTypeRDto>>($"{Ticket}/mtype", RestCallTypeEnum.Put,updMimeType);
  100. return result.Value;
  101. }
  102. public async Task<IEnumerable<MimeTypeRDto>> GetMimeTypesAsync(MimeTypeScopeEnums scope, PagingDto paging = null)
  103. {
  104. CheckIsOpen();
  105. var result = await CallRequestAsync<ResultsValueDto<MimeTypeRDto>>($"{Ticket}/mtypes/{scope}", RestCallTypeEnum.Get,null,GetPagingHeaderAttributes(paging));
  106. return result.Values;
  107. }
  108. #endregion
  109. #region *** Documents ***
  110. public async Task<IEnumerable<DocumentRDto>> GetDocumentsAsync(DocumentsScopeEnums scope, PagingDto paging = null)
  111. {
  112. CheckIsOpen();
  113. var result = await CallRequestAsync<ResultsValueDto<DocumentRDto>>($"{Ticket}/documents/{scope}", RestCallTypeEnum.Get,null,GetPagingHeaderAttributes(paging));
  114. return result.Values;
  115. }
  116. public async Task<IEnumerable<DocumentRDto>> GetWorkspaceDocumentsAsync(DocumentsScopeEnums scope, string workspaceApiKey,string apiKeyPassword,string userNameContext, PagingDto paging = null)
  117. {
  118. CheckIsOpen();
  119. var header = GetUserContextHeaderAttributes(userNameContext, GetApiPasswordHeaderAttributes(apiKeyPassword,GetPagingHeaderAttributes(paging)));
  120. var result = await CallRequestAsync<ResultsValueDto<DocumentRDto>>($"{Ticket}/workspace/{workspaceApiKey}/documents/{scope}", RestCallTypeEnum.Get,null, header);
  121. return result.Values;
  122. }
  123. public async Task<DocumentRDto> GetWorkspaceDocumentByNameAsync(string workspaceApiKey,string apiKeyPassword,string documentName, string userNameContext)
  124. {
  125. CheckIsOpen();
  126. var header = GetUserContextHeaderAttributes(userNameContext, GetApiPasswordHeaderAttributes(apiKeyPassword));
  127. header.Add("documentName", documentName);
  128. var result = await CallRequestAsync<ResultValueDto<DocumentRDto>>($"{Ticket}/workspace/{workspaceApiKey}/document", RestCallTypeEnum.Get,null, header);
  129. // can return null value
  130. return result.HasNoDataException() ? null : result.Value;
  131. }
  132. public async Task<DocumentRDto> GetWorkspaceDocumentAsync(string workspaceApiKey,string apiKeyPassword,long documentId, string userNameContext)
  133. {
  134. CheckIsOpen();
  135. var header = GetUserContextHeaderAttributes(userNameContext, GetApiPasswordHeaderAttributes(apiKeyPassword));
  136. var result = await CallRequestAsync<ResultValueDto<DocumentRDto>>($"{Ticket}/workspace/{workspaceApiKey}/document/{documentId}", RestCallTypeEnum.Get,null, header);
  137. return result.Value;
  138. }
  139. public async Task<Stream> GetWorkspaceDocumentContentAsync(string workspaceApiKey,string apiKeyPassword,long documentId, long artifactId, string userNameContext)
  140. {
  141. CheckIsOpen();
  142. var header = GetUserContextHeaderAttributes(userNameContext, GetApiPasswordHeaderAttributes(apiKeyPassword));
  143. var result = await CallGetStreamAsync($"{Ticket}/workspace/{workspaceApiKey}/document/{documentId}/artifact/{artifactId}/content", header);
  144. return result;
  145. }
  146. public async Task<DocumentRDto> CreateWorkspaceDocumentAsync(string workspaceApiKey,string apiKeyPassword,DocumentCDto data, string userNameContext)
  147. {
  148. CheckIsOpen();
  149. var header = GetUserContextHeaderAttributes(userNameContext, GetApiPasswordHeaderAttributes(apiKeyPassword));
  150. var result = await CallRequestAsync<ResultValueDto<DocumentRDto>>($"{Ticket}/workspace/{workspaceApiKey}/document", RestCallTypeEnum.Post,data, header);
  151. return result.Value;
  152. }
  153. public async Task<ArtifactRDto> AppendWorkspaceDocumentArtifactAsync(string workspaceApiKey,string apiKeyPassword,long documentId, string fileName,string mimeType,bool overwriteIfExists, Stream contentStream, string userNameContext)
  154. {
  155. CheckIsOpen();
  156. var header = GetOverwriteHeaderAttributes(overwriteIfExists,GetUserContextHeaderAttributes(userNameContext, GetApiPasswordHeaderAttributes(apiKeyPassword)));
  157. var result = await CallPostStreamMultipartAsync<ResultValueDto<ArtifactRDto>>($"{Ticket}/workspace/{workspaceApiKey}/document/{documentId}/artifact/input", fileName, contentStream,mimeType, header);
  158. return result.Value;
  159. }
  160. public async Task<IEnumerable<ArtifactRDto>> ClearWorkspaceDocumentArtifactAsync(string workspaceApiKey,string apiKeyPassword,long documentId,bool force, string userNameContext)
  161. {
  162. CheckIsOpen();
  163. var header = GetForceHeaderAttributes(force,GetUserContextHeaderAttributes(userNameContext, GetApiPasswordHeaderAttributes(apiKeyPassword)));
  164. var result = await CallRequestAsync<ResultsValueDto<ArtifactRDto>>($"{Ticket}/workspace/{workspaceApiKey}/document/{documentId}/artifact",RestCallTypeEnum.Delete,null, header);
  165. return result.Values;
  166. }
  167. #endregion
  168. #region *** Workspace ***
  169. public async Task<WorkspaceRDto> CreateWorkspaceAsync(WorkspaceCDto workspace)
  170. {
  171. CheckIsOpen();
  172. var result = await CallRequestAsync<ResultValueDto<WorkspaceRDto>>($"{Ticket}/workspace", RestCallTypeEnum.Post,workspace);
  173. return result.Value;
  174. }
  175. public async Task<WorkspaceRDto> GetWorkspaceAsync(IdentificationTypeEnum identificationType, string identificationValue)
  176. {
  177. CheckIsOpen();
  178. var result = await CallRequestAsync<ResultValueDto<WorkspaceRDto>>($"{Ticket}/workspace/{identificationType}/{identificationValue}", RestCallTypeEnum.Get);
  179. return result.Value;
  180. }
  181. public async Task<IEnumerable<WorkspaceRDto>> GetWorkspacesAsync(WorkspaceScopeEnums scope,string userNameContext, PagingDto paging = null)
  182. {
  183. CheckIsOpen();
  184. var headers = GetUserContextHeaderAttributes(userNameContext, GetPagingHeaderAttributes(paging));
  185. var result = await CallRequestAsync<ResultsValueDto<WorkspaceRDto>>($"{Ticket}/workspaces/{scope}", RestCallTypeEnum.Get,null,headers);
  186. return result.Values;
  187. }
  188. #endregion
  189. }
  190. }