TransferRequest
using Relativity.Transfer.Dto;
using Relativity.Transfer.Resources;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Relativity.Transfer
{
public sealed class TransferRequest : ITransferRequest
{
private readonly List<TransferPath> requestPaths = new List<TransferPath>();
public string Application { get; set; }
public int BatchNumber { get; set; }
public Guid? ClientRequestId { get; set; }
public TransferContext Context { get; set; }
public TransferDirection Direction { get; set; }
public Guid? JobId { get; set; }
public string Name { get; set; }
public IReadOnlyList<TransferPath> Paths => requestPaths;
public bool RemotePathsInUncFormat { get; set; }
public IRetryStrategy RetryStrategy { get; set; }
public IRemotePathResolver SourcePathResolver { get; set; }
public bool SubmitApmMetrics { get; set; }
public object Tag { get; set; }
public string TargetPath { get; set; }
public IRemotePathResolver TargetPathResolver { get; set; }
public int TotalBatchCount { get; set; }
public TransferRequest()
: this(TransferDirection.Upload, null, null, null)
{
}
public TransferRequest(TransferDirection direction, IEnumerable<TransferPath> paths, string targetPath, TransferContext context)
{
Application = null;
BatchNumber = 0;
ClientRequestId = Guid.NewGuid();
Context = context;
Direction = direction;
if (paths != null)
AddPaths(paths);
JobId = null;
Name = "transfer-api";
RemotePathsInUncFormat = true;
RetryStrategy = RetryStrategies.CreateExpBackoffStrategy();
SourcePathResolver = null;
SubmitApmMetrics = true;
Tag = null;
TargetPathResolver = null;
TargetPath = targetPath;
TotalBatchCount = 0;
}
public TransferRequest(TransferRequestConfiguration configuration)
: this(configuration, null)
{
}
public TransferRequest(TransferRequestConfiguration configuration, IEnumerable<TransferPath> paths)
: this(configuration, paths, null)
{
}
public TransferRequest(TransferRequestConfiguration configuration, IEnumerable<TransferPath> paths, TransferContext context)
{
if (configuration == null)
throw new ArgumentNullException("configuration");
Application = configuration.Application;
BatchNumber = 0;
ClientRequestId = (configuration.ClientRequestId ?? Guid.NewGuid());
Context = context;
Direction = configuration.Direction;
JobId = configuration.JobId;
Name = configuration.Name;
RemotePathsInUncFormat = configuration.RemotePathsInUncFormat;
RetryStrategy = ((configuration.RetryWaitTimeSeconds > 0) ? RetryStrategies.CreateFixedTimeStrategy(configuration.RetryWaitTimeSeconds) : RetryStrategies.CreateExpBackoffStrategy());
SourcePathResolver = null;
SubmitApmMetrics = configuration.SubmitApmMetrics;
Tag = null;
TargetPathResolver = null;
TargetPath = configuration.TargetPath;
TotalBatchCount = 0;
if (paths != null)
AddPaths(paths);
}
public static TransferRequest FromConfiguration(TransferRequestConfiguration configuration)
{
return FromConfiguration(configuration, (TransferContext)null);
}
public static TransferRequest FromConfiguration(TransferRequestConfiguration configuration, TransferContext context)
{
return FromConfiguration(configuration, null, context);
}
public static TransferRequest FromConfiguration(TransferRequestConfiguration configuration, IEnumerable<TransferPath> paths)
{
return FromConfiguration(configuration, paths, null);
}
public static TransferRequest FromConfiguration(TransferRequestConfiguration configuration, IEnumerable<TransferPath> paths, TransferContext context)
{
return new TransferRequest(configuration, paths, context);
}
public static TransferRequest ForDownload(TransferPath path)
{
return ForDownload(path, (string)null);
}
public static TransferRequest ForDownload(TransferPath path, string targetPath)
{
return ForDownload(path, targetPath, null);
}
public static TransferRequest ForDownload(TransferPath path, TransferContext context)
{
return ForDownload(path, null, context);
}
public static TransferRequest ForDownload(TransferPath path, string targetPath, TransferContext context)
{
return ForDownload(new TransferPath[1] {
path
}, targetPath, context);
}
public static TransferRequest ForDownload(IEnumerable<TransferPath> paths)
{
return ForDownload(paths, (string)null);
}
public static TransferRequest ForDownload(IEnumerable<TransferPath> paths, string targetPath)
{
return ForDownload(paths, targetPath, null);
}
public static TransferRequest ForDownload(IEnumerable<TransferPath> paths, TransferContext context)
{
return ForDownload(paths, null, context);
}
public static TransferRequest ForDownload(IEnumerable<TransferPath> paths, string targetPath, TransferContext context)
{
return new TransferRequest(TransferDirection.Download, paths, targetPath, context);
}
public static TransferRequest ForDownloadJob()
{
return ForDownloadJob(null, null);
}
public static TransferRequest ForDownloadJob(string targetPath)
{
return ForDownloadJob(targetPath, null);
}
public static TransferRequest ForDownloadJob(TransferContext context)
{
return ForDownloadJob(null, context);
}
public static TransferRequest ForDownloadJob(string targetPath, TransferContext context)
{
return ForDownload(new TransferPath[0], targetPath, context);
}
public static TransferRequest ForUpload(TransferPath path)
{
return ForUpload(path, (string)null);
}
public static TransferRequest ForUpload(TransferPath path, string targetPath)
{
return ForUpload(path, targetPath, null);
}
public static TransferRequest ForUpload(TransferPath path, TransferContext context)
{
return ForUpload(path, null, context);
}
public static TransferRequest ForUpload(TransferPath path, string targetPath, TransferContext context)
{
return ForUpload(new TransferPath[1] {
path
}, targetPath, context);
}
public static TransferRequest ForUpload(IEnumerable<TransferPath> paths)
{
return ForUpload(paths, (string)null);
}
public static TransferRequest ForUpload(IEnumerable<TransferPath> paths, string targetPath)
{
return ForUpload(paths, targetPath, null);
}
public static TransferRequest ForUpload(IEnumerable<TransferPath> paths, TransferContext context)
{
return ForUpload(paths, null, context);
}
public static TransferRequest ForUpload(IEnumerable<TransferPath> paths, string targetPath, TransferContext context)
{
return new TransferRequest(TransferDirection.Upload, paths, targetPath, context);
}
public static TransferRequest ForUploadJob()
{
return ForUploadJob((string)null);
}
public static TransferRequest ForUploadJob(string targetPath)
{
return ForUploadJob(targetPath, null);
}
public static TransferRequest ForUploadJob(TransferContext context)
{
return ForUploadJob(null, context);
}
public static TransferRequest ForUploadJob(string targetPath, TransferContext context)
{
return ForUpload(new TransferPath[0], targetPath, context);
}
public static TransferRequest FromSearchResult(EnumeratedPathsResult searchPathsResult)
{
return FromSearchResult(searchPathsResult, new TransferContext());
}
public static TransferRequest FromSearchResult(EnumeratedPathsResult searchPathsResult, TransferContext context)
{
if (searchPathsResult == null)
throw new ArgumentNullException("searchPathsResult");
return new TransferRequest(searchPathsResult.LocalPaths ? TransferDirection.Upload : TransferDirection.Download, searchPathsResult.Paths, null, context);
}
public static TransferRequest FromSerializedBatch(SerializedBatch batch)
{
return FromSerializedBatch(batch, new TransferContext());
}
public static TransferRequest FromSerializedBatch(SerializedBatch batch, TransferContext context)
{
if (batch == null)
throw new ArgumentNullException("batch");
TransferRequest transferRequest = FromSerializedBatchForJob(batch, context);
transferRequest.AddPaths(batch);
return transferRequest;
}
public static TransferRequest FromSerializedBatchForJob(SerializedBatch batch)
{
return FromSerializedBatchForJob(batch, new TransferContext());
}
public static TransferRequest FromSerializedBatchForJob(SerializedBatch batch, TransferContext context)
{
if (batch == null)
throw new ArgumentNullException("batch");
return new TransferRequest {
Application = null,
BatchNumber = batch.BatchNumber,
ClientRequestId = new Guid?(Guid.NewGuid()),
Context = context,
Direction = (batch.LocalPaths ? TransferDirection.Upload : TransferDirection.Download),
JobId = null,
Name = "transfer-api",
RemotePathsInUncFormat = true,
SubmitApmMetrics = true,
TotalBatchCount = batch.TotalBatchCount
};
}
public void AddPath(TransferPath path)
{
if (path == (TransferPath)null)
throw new ArgumentNullException("path");
if (string.IsNullOrEmpty(path.SourcePath))
throw new ArgumentException(CoreStrings.SourcePathArgumentExceptionMessage, "path");
requestPaths.Add(path);
}
public void AddPaths(IEnumerable<TransferPath> paths)
{
if (paths == null)
throw new ArgumentNullException("paths");
foreach (TransferPath path in paths) {
if (path != (TransferPath)null)
requestPaths.Add(path);
}
}
public void AddPaths(SerializedBatch batch)
{
if (batch == null)
throw new ArgumentNullException("batch");
SerializedPathsBatchDto serializedPathsBatchDto = JsonFileSerializer.Deserialize<SerializedPathsBatchDto>(batch.File);
IEnumerable<TransferPath> paths = serializedPathsBatchDto.Paths.Select(TransferPathDto.ConvertToPath);
AddPaths(paths);
}
public void Clear()
{
Application = null;
BatchNumber = 0;
ClientRequestId = null;
Context = null;
Direction = TransferDirection.None;
JobId = null;
Name = "transfer-api";
requestPaths.Clear();
RemotePathsInUncFormat = false;
RetryStrategy = null;
SourcePathResolver = null;
SubmitApmMetrics = true;
Tag = null;
TargetPath = null;
TargetPathResolver = null;
TotalBatchCount = 0;
}
public void ClearPaths()
{
requestPaths.Clear();
}
public override string ToString()
{
IEnumerable<string> list = CollectLoggableProperties();
return ToPretty(list);
}
private IEnumerable<string> CollectLoggableProperties()
{
object obj;
Guid value;
if (!JobId.HasValue)
obj = "<empty>";
else {
value = JobId.Value;
obj = value.ToString();
}
string str = (string)obj;
string str2 = string.IsNullOrEmpty(TargetPath) ? "<empty>" : TargetPath;
string str3 = string.IsNullOrEmpty(Application) ? "<empty>" : Application;
object obj2;
if (!ClientRequestId.HasValue)
obj2 = "<empty>";
else {
value = ClientRequestId.Value;
obj2 = value.ToString();
}
string str4 = (string)obj2;
int count = Paths.Count;
return new string[9] {
"Application: " + str3,
"JobId: " + str,
"ClientRequestId: " + str4,
string.Format("{0}: {1}", "BatchNumber", BatchNumber),
string.Format("{0}: {1}", "TotalBatchCount", TotalBatchCount),
"TargetPath: " + str2,
$"""{count}",
string.Format("{0}: {1}", "RemotePathsInUncFormat", RemotePathsInUncFormat),
string.Format("{0}: {1}", "SubmitApmMetrics", SubmitApmMetrics)
};
}
private static string ToPretty(IEnumerable<string> list)
{
StringBuilder stringBuilder = new StringBuilder();
string value = "";
foreach (string item in list) {
stringBuilder.Append(value).Append(item);
value = ", ";
}
return stringBuilder.ToString();
}
}
}