/// <summary> /// Set error in result /// </summary> /// <param name="command">The command</param> /// <param name="cancellationToken">The cancellation token</param> /// <returns>The response</returns> public async Task <DownloadIntegrationExportCommandResult> Handle( DownloadIntegrationExportCommand command, CancellationToken cancellationToken) { DownloadIntegrationExportCommandResult commandResult; try { commandResult = await _handler .Handle(command, cancellationToken) .ConfigureAwait(Await.Default); } #pragma warning disable CA1031 // Do not catch general exception types catch (Exception ex) #pragma warning restore CA1031 // Do not catch general exception types { commandResult = new DownloadIntegrationExportCommandResult { Success = false, Message = "Error executing integration export", Exception = ex, }; } return(commandResult); }
/// <summary> /// Validate download integration export command /// </summary> /// <param name="command">The command</param> /// <param name="cancellationToken">The cancellation token</param> /// <returns>The response</returns> public Task <DownloadIntegrationExportCommandResult> Handle( DownloadIntegrationExportCommand command, CancellationToken cancellationToken) { Argument.NotNull(command, nameof(command)); _validator.ValidateObject(command); return(_handler.Handle(command, cancellationToken)); }
/// <summary> /// Log debug information /// </summary> /// <param name="command">The command</param> /// <param name="cancellationToken">The cancellation token</param> /// <returns>The response</returns> public Task <DownloadIntegrationExportCommandResult> Handle( DownloadIntegrationExportCommand command, CancellationToken cancellationToken) { if (_logger.IsDebugEnabled) { return(HandleWithDebug(command, cancellationToken)); } else { return(_handler.Handle(command, cancellationToken)); } }
/// <summary> /// Reject download integration export /// </summary> /// <param name="command">The command</param> /// <param name="cancellationToken">The cancellation token</param> /// <returns>The response</returns> public Task <DownloadIntegrationExportCommandResult> Handle( DownloadIntegrationExportCommand command, CancellationToken cancellationToken) { Argument.NotNull(command, nameof(command)); if (command.Accept == true) { return(_handler.Handle(command, cancellationToken)); } else { return(Reject(command, cancellationToken)); } }
private IIntegrationConfiguration GetConfiguration( DownloadIntegrationExportCommand command) { IIntegrationConfiguration configuration; configuration = _configuration.Value .IntegrationExports .Where(x => x.IntegrationId == command.IntegrationId) .SingleOrDefault(); if (configuration == null) { configuration = _configuration.Value.Default; } return(configuration); }
/// <summary> /// Report status back to api /// </summary> /// <param name="command">The command</param> /// <param name="cancellationToken">The cancellation token</param> /// <returns>The response</returns> public async Task <DownloadIntegrationExportCommandResult> Handle( DownloadIntegrationExportCommand command, CancellationToken cancellationToken) { Argument.NotNull(command, nameof(command)); var stopwatch = Stopwatch.StartNew(); var commandResult = await _handler .Handle(command, cancellationToken) .ConfigureAwait(Await.Default); stopwatch.Stop(); int duration = Convert(stopwatch.ElapsedMilliseconds); string exception = null; if (commandResult.Exception != null) { exception = commandResult.Exception.ToString(); if (exception.Length > ExceptionLength) { exception = exception.Substring(0, ExceptionLength); } } var request = new ReportIntegrationExportV1Request { IntegrationExportId = command.IntegrationExportId, IntegrationExportLogId = command.IntegrationExportLogId, Result = commandResult.Success == true ? DownloadIntegrationExportV1Result.Success : DownloadIntegrationExportV1Result.Failed, Duration = duration, Message = commandResult.Message, Exception = exception, }; _ = await _client .Execute(request, cancellationToken) .ConfigureAwait(Await.Default); return(commandResult); }
/// <summary> /// Set configuration and accept download /// </summary> /// <param name="command">The command</param> /// <param name="cancellationToken">The cancellation token</param> /// <returns>The response</returns> public Task <DownloadIntegrationExportCommandResult> Handle( DownloadIntegrationExportCommand command, CancellationToken cancellationToken) { Argument.NotNull(command, nameof(command)); var configuration = GetConfiguration(command); if (configuration != null) { command.Configuration = configuration; command.Accept = configuration.Enabled == true; } else { command.Accept = false; } return(_handler.Handle(command, cancellationToken)); }
private async Task <DownloadIntegrationExportCommandResult> HandleWithDebug( DownloadIntegrationExportCommand command, CancellationToken cancellationToken) { const string commandName = nameof(DownloadIntegrationExportCommand); _logger.DebugFormat( "Executing command {0} :\r\n{1}", commandName, _logger.Serialize(command)); var commandResult = await _handler .Handle(command, cancellationToken) .ConfigureAwait(Await.Default); _logger.DebugFormat( "Executed command {0} :\r\n{1}", commandName, _logger.Serialize(commandResult)); return(commandResult); }
private async Task <DownloadIntegrationExportCommandResult> Reject( DownloadIntegrationExportCommand command, CancellationToken cancellationToken) { var request = new RejectIntegrationExportV1Request { IntegrationExportId = command.IntegrationExportId, Message = "Not configured for this integration", }; _ = await _apiClient .Execute(request, cancellationToken) .ThrowIfFailed() .ConfigureAwait(Await.Default); var commandResult = new DownloadIntegrationExportCommandResult { Success = false, Message = request.Message, Exception = null, }; return(commandResult); }
/// <summary> /// Handle command /// </summary> /// <param name="command">The command</param> /// <param name="cancellationToken">The cancellation token</param> /// <returns>The response</returns> public async Task <DownloadIntegrationExportCommandResult> Handle( DownloadIntegrationExportCommand command, CancellationToken cancellationToken) { Argument.NotNull(command, nameof(command)); DownloadIntegrationExportCommandResult commandResult; var request = new AcceptIntegrationExportV1Request { IntegrationExportId = command.IntegrationExportId, }; var response = await _client .Execute(request, cancellationToken) .ConfigureAwait(Await.Default); if (response.IsSuccessful) { command.IntegrationExportLogId = response.Model.IntegrationExportLogId; commandResult = await _handler .Handle(command, cancellationToken) .ConfigureAwait(Await.Default); } else { commandResult = new DownloadIntegrationExportCommandResult { Success = false, Message = "Error accepting integration export", }; } return(commandResult); }
/// <summary> /// Sanitise filename and execute command /// </summary> /// <param name="command">The command</param> /// <param name="cancellationToken">The cancellation token</param> /// <returns>The response</returns> public Task <DownloadIntegrationExportCommandResult> Handle( DownloadIntegrationExportCommand command, CancellationToken cancellationToken) { Argument.NotNull(command, nameof(command)); if (string.IsNullOrEmpty(command.FileName) == false) { var builder = new StringBuilder(command.FileName); var invalidChars = _path.GetInvalidFileNameChars(); foreach (var invalidChar in invalidChars) { builder = builder.Replace(invalidChar, '_'); } string sanitisedFileName = builder.ToString(); command.FileName = sanitisedFileName; } return(_handler.Handle(command, cancellationToken)); }
/// <summary> /// Handles downloading the integration export /// </summary> /// <param name="command">The command</param> /// <param name="cancellationToken">The cancellation token</param> /// <returns>The response</returns> public async Task <DownloadIntegrationExportCommandResult> Handle( DownloadIntegrationExportCommand command, CancellationToken cancellationToken) { Argument.NotNull(command, nameof(command)); var commandResult = new DownloadIntegrationExportCommandResult(); var configuration = command.Configuration; Require.NotNull(configuration, nameof(configuration)); var request = new DownloadIntegrationExportV1Request { IntegrationExportId = command.IntegrationExportId, }; var response = await _client .Execute(request, cancellationToken) .ConfigureAwait(Await.Default); if (response.IsSuccessful) { using (var model = response.Model) { var saveCommand = new SaveIntegrationExportFileCommand { IntegrationExportId = command.IntegrationExportId, File = model.File, FileName = model.FileName, FileSize = model.Size, DestinationFolder = configuration.Folder, BackupFolder = configuration.BackupFolder, Overwrite = configuration.Overwrite, }; var saveCommandResult = await _handler .Handle(saveCommand, cancellationToken) .ConfigureAwait(Await.Default); switch (saveCommandResult.Result) { case SaveIntegrationExportFileCommandResultKind.Success: commandResult.Success = true; // TODO: ?? commandResult.Message = ""; // TODO: add backup file name and size break; case SaveIntegrationExportFileCommandResultKind.Failed: commandResult.Success = false; commandResult.Message = ""; // TODO: break; default: throw UnexpectedEnumValueException.Create(saveCommandResult.Result); } } } else { commandResult.Success = false; commandResult.Message = "Error downloading integration export"; } return(commandResult); }