/// <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); }
/// <summary> /// Execute example /// </summary> /// <param name="cancellationToken">The cancellation token</param> /// <returns>The task</returns> public async Task Execute(CancellationToken cancellationToken) { var integrationExportId = Guid.Parse(IntegrationExportId); var request = new DownloadIntegrationExportV1Request { IntegrationExportId = integrationExportId, }; var response = await _client .Execute(request, cancellationToken) .ThrowIfFailed() .ConfigureAwait(Await.Default); string directory = Path.GetTempPath(); directory = Path.Combine(directory, nameof(DownloadIntegrationExportV1Example)); _ = Directory.CreateDirectory(directory); string filename = Path.GetRandomFileName() + '_' + response.Model.FileName; string path = Path.Combine(directory, filename); long bytesWritten = 0L; using (response.Model) using (var stream = response.Model.File) using (var fileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None, BufferSize, useAsync: true)) { byte[] buffer = new byte[BufferSize]; int read; #pragma warning disable CA1835 // Prefer the 'Memory'-based overloads for 'ReadAsync' and 'WriteAsync' while ((read = await stream .ReadAsync(buffer, 0, buffer.Length, cancellationToken) #pragma warning restore CA1835 // Prefer the 'Memory'-based overloads for 'ReadAsync' and 'WriteAsync' .ConfigureAwait(Await.Default)) > 0) { #pragma warning disable CA1835 // Prefer the 'Memory'-based overloads for 'ReadAsync' and 'WriteAsync' await fileStream .WriteAsync(buffer, 0, read, cancellationToken) #pragma warning restore CA1835 // Prefer the 'Memory'-based overloads for 'ReadAsync' and 'WriteAsync' .ConfigureAwait(Await.Default); bytesWritten += read; } await fileStream .FlushAsync(cancellationToken) .ConfigureAwait(Await.Default); } #pragma warning disable CA1303 // Do not pass literals as localized parameters Console.WriteLine("Saved export file to {0} ({1:n0} bytes)", path, bytesWritten); #pragma warning restore CA1303 // Do not pass literals as localized parameters if (response.Headers.ContentLength != bytesWritten) { throw new InvalidOperationException("Number of bytes written not equal to content length"); } }