using System; using System.Collections.Generic; using System.IO; using System.Threading.Tasks; using BO.AppServer.Metadata.Dto; using BO.AppServer.Metadata.Enums; using BO.AppServer.Metadata.Extensions; using Quadarax.Foundation.Core.Data.Interface; using Quadarax.Foundation.Core.Data.Interface.Entity.Dto; using Quadarax.Foundation.Core.Logging; namespace BO.Connector.Console { public class Connection : AbstractConnection { #region *** Private fields *** protected override string ApiName => "Console"; #endregion #region *** Constructors *** public Connection(Uri uriApiBase, TimeSpan timeout, ILogHandler log = null, bool isDumpContentEnabled = false) : base(uriApiBase, timeout, log, isDumpContentEnabled) { } #endregion #region *** CRUD User *** public async Task GetUserAsync(IdentificationTypeEnum identificationType, string identificationValue) { CheckIsOpen(); var result = await CallRequestAsync>($"{Ticket}/user/{identificationType}/{identificationValue}", RestCallTypeEnum.Get); return result.Value; } public async Task> GetUsersAsync(UserScopeEnums scope, PagingDto paging = null) { CheckIsOpen(); var result = await CallRequestAsync>($"{Ticket}/users/{scope}", RestCallTypeEnum.Get,null, GetPagingHeaderAttributes(paging)); return result.Values; } public async Task CreateUserAsync(UserCDto newUser) { CheckIsOpen(); var result = await CallRequestAsync>($"{Ticket}/user", RestCallTypeEnum.Post, newUser); return result.Value; } public async Task UpdateUserAsnc(IdentificationTypeEnum identificationType, string identificationValue, UserUDto updateUser) { CheckIsOpen(); var result = await CallRequestAsync>($"{Ticket}/user/{identificationType}/{identificationValue}", RestCallTypeEnum.Put, updateUser); return result.Value; } public async Task DeleteUserAsync(IdentificationTypeEnum identificationType, string identificationValue) { CheckIsOpen(); var result = await CallRequestAsync>($"{Ticket}/user/{identificationType}/{identificationValue}", RestCallTypeEnum.Delete); return result.Value; } #endregion #region *** Statistics *** public async Task> GetStatisticsAsync(StatisticsScopeEnums scope, PagingDto paging = null) { CheckIsOpen(); var result = await CallRequestAsync>($"{Ticket}/statistics/{scope}", RestCallTypeEnum.Get,null,GetPagingHeaderAttributes(paging)); return result.Values; } public async Task> GetVersionsAsync() { CheckIsOpen(); var result = await CallRequestAsync>($"{Ticket}/versions", RestCallTypeEnum.Get); return result.Values; } #endregion #region *** CRUD BillingPlan *** public async Task CreateBillingPlanAsync(BillingPlanCDto newBillingPlan) { CheckIsOpen(); var result = await CallRequestAsync>($"{Ticket}/bplan", RestCallTypeEnum.Post,newBillingPlan); return result.Value; } public async Task UpdateBillingPlanAsync(BillingPlanUDto updBillingPlan) { CheckIsOpen(); var result = await CallRequestAsync>($"{Ticket}/bplan", RestCallTypeEnum.Put,updBillingPlan); return result.Value; } public async Task> GetBillingPlansAsync(BPlanScopeEnums scope, PagingDto paging = null) { CheckIsOpen(); var result = await CallRequestAsync>($"{Ticket}/bplans/{scope}", RestCallTypeEnum.Get,null,GetPagingHeaderAttributes(paging)); return result.Values; } #endregion #region *** CRUD MIME Type *** public async Task CreateMimeTypeAsync(MimeTypeCDto newMimeType) { CheckIsOpen(); var result = await CallRequestAsync>($"{Ticket}/mtype", RestCallTypeEnum.Post,newMimeType); return result.Value; } public async Task UpdateMimeTypeAsync(MimeTypeUDto updMimeType) { CheckIsOpen(); var result = await CallRequestAsync>($"{Ticket}/mtype", RestCallTypeEnum.Put,updMimeType); return result.Value; } public async Task> GetMimeTypesAsync(MimeTypeScopeEnums scope, PagingDto paging = null) { CheckIsOpen(); var result = await CallRequestAsync>($"{Ticket}/mtypes/{scope}", RestCallTypeEnum.Get,null,GetPagingHeaderAttributes(paging)); return result.Values; } #endregion #region *** Documents *** public async Task> GetDocumentsAsync(DocumentsScopeEnums scope, PagingDto paging = null) { CheckIsOpen(); var result = await CallRequestAsync>($"{Ticket}/documents/{scope}", RestCallTypeEnum.Get,null,GetPagingHeaderAttributes(paging)); return result.Values; } public async Task> GetWorkspaceDocumentsAsync(DocumentsScopeEnums scope, string workspaceApiKey,string apiKeyPassword,string userNameContext, PagingDto paging = null) { CheckIsOpen(); var header = GetUserContextHeaderAttributes(userNameContext, GetApiPasswordHeaderAttributes(apiKeyPassword,GetPagingHeaderAttributes(paging))); var result = await CallRequestAsync>($"{Ticket}/workspace/{workspaceApiKey}/documents/{scope}", RestCallTypeEnum.Get,null, header); return result.Values; } public async Task GetWorkspaceDocumentByNameAsync(string workspaceApiKey,string apiKeyPassword,string documentName, string userNameContext) { CheckIsOpen(); var header = GetUserContextHeaderAttributes(userNameContext, GetApiPasswordHeaderAttributes(apiKeyPassword)); header.Add("documentName", documentName); var result = await CallRequestAsync>($"{Ticket}/workspace/{workspaceApiKey}/document", RestCallTypeEnum.Get,null, header); // can return null value return result.HasNoDataException() ? null : result.Value; } public async Task GetWorkspaceDocumentAsync(string workspaceApiKey,string apiKeyPassword,IdentificationTypeEnum identificationType, string identificationValue, string userNameContext) { CheckIsOpen(); var header = GetUserContextHeaderAttributes(userNameContext, GetApiPasswordHeaderAttributes(apiKeyPassword)); var result = await CallRequestAsync>($"{Ticket}/workspace/{workspaceApiKey}/document/{identificationType}/{identificationValue}", RestCallTypeEnum.Get,null, header); return result.Value; } public async Task GetWorkspaceDocumentContentAsync(string workspaceApiKey,string apiKeyPassword,long documentId, long artifactId, string userNameContext) { CheckIsOpen(); var header = GetUserContextHeaderAttributes(userNameContext, GetApiPasswordHeaderAttributes(apiKeyPassword)); var result = await CallGetStreamAsync($"{Ticket}/workspace/{workspaceApiKey}/document/{documentId}/artifact/{artifactId}/content", header); return result; } public async Task CreateWorkspaceDocumentAsync(string workspaceApiKey,string apiKeyPassword,DocumentCDto data, string userNameContext) { CheckIsOpen(); var header = GetUserContextHeaderAttributes(userNameContext, GetApiPasswordHeaderAttributes(apiKeyPassword)); var result = await CallRequestAsync>($"{Ticket}/workspace/{workspaceApiKey}/document", RestCallTypeEnum.Post,data, header); return result.Value; } public async Task AppendWorkspaceDocumentArtifactAsync(string workspaceApiKey,string apiKeyPassword,long documentId, string fileName,string mimeType,bool overwriteIfExists, Stream contentStream, string userNameContext) { CheckIsOpen(); var header = GetOverwriteHeaderAttributes(overwriteIfExists,GetUserContextHeaderAttributes(userNameContext, GetApiPasswordHeaderAttributes(apiKeyPassword))); var result = await CallPostStreamMultipartAsync>($"{Ticket}/workspace/{workspaceApiKey}/document/{documentId}/artifact/input", fileName, contentStream,mimeType, header); return result.Value; } public async Task> ClearWorkspaceDocumentArtifactAsync(string workspaceApiKey,string apiKeyPassword,long documentId,bool force, string userNameContext) { CheckIsOpen(); var header = GetForceHeaderAttributes(force,GetUserContextHeaderAttributes(userNameContext, GetApiPasswordHeaderAttributes(apiKeyPassword))); var result = await CallRequestAsync>($"{Ticket}/workspace/{workspaceApiKey}/document/{documentId}/artifact",RestCallTypeEnum.Delete,null, header); return result.Values; } #endregion #region *** Workspace *** public async Task CreateWorkspaceAsync(WorkspaceCDto workspace) { CheckIsOpen(); var result = await CallRequestAsync>($"{Ticket}/workspace", RestCallTypeEnum.Post,workspace); return result.Value; } public async Task GetWorkspaceAsync(IdentificationTypeEnum identificationType, string identificationValue) { CheckIsOpen(); var result = await CallRequestAsync>($"{Ticket}/workspace/{identificationType}/{identificationValue}", RestCallTypeEnum.Get); return result.Value; } public async Task> GetWorkspacesAsync(WorkspaceScopeEnums scope,string userNameContext, PagingDto paging = null) { CheckIsOpen(); var headers = GetUserContextHeaderAttributes(userNameContext, GetPagingHeaderAttributes(paging)); var result = await CallRequestAsync>($"{Ticket}/workspaces/{scope}", RestCallTypeEnum.Get,null,headers); return result.Values; } #endregion } }