Skip to content

Commit

Permalink
Add copy constructor to RequestConfiguration{Descriptor}
Browse files Browse the repository at this point in the history
  • Loading branch information
flobernd committed Nov 8, 2024
1 parent 363f7a1 commit e817fce
Showing 1 changed file with 81 additions and 7 deletions.
88 changes: 81 additions & 7 deletions src/Elastic.Transport/Configuration/RequestConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Security.Cryptography.X509Certificates;

using Elastic.Transport.Extensions;

namespace Elastic.Transport;
Expand Down Expand Up @@ -108,7 +109,6 @@ public interface IRequestConfiguration
/// </summary>
TimeSpan? RequestTimeout { get; }


/// <summary> Specifies the headers from the response that should be parsed. </summary>
HeadersList? ResponseHeadersToParse { get; }

Expand Down Expand Up @@ -163,6 +163,48 @@ public record RequestConfiguration : IRequestConfiguration
/// <summary> The default ping timeout when the connection is over HTTPS. Defaults to 5 seconds </summary>
public static readonly TimeSpan DefaultPingTimeoutOnSsl = TimeSpan.FromSeconds(5);

/// <inheritdoc cref="IRequestConfiguration"/>
public RequestConfiguration()
{
}

/// <inheritdoc cref="IRequestConfiguration"/>
public RequestConfiguration(IRequestConfiguration config)
{
#if NET8_0_OR_GREATER
ArgumentNullException.ThrowIfNull(config);
#else
if (config is null)
throw new ArgumentNullException(nameof(config));
#endif

Accept = config.Accept;
AllowedStatusCodes = config.AllowedStatusCodes;
Authentication = config.Authentication;
ClientCertificates = config.ClientCertificates;
ContentType = config.ContentType;
DisableDirectStreaming = config.DisableDirectStreaming;
DisableAuditTrail = config.DisableAuditTrail;
DisablePings = config.DisablePings;
DisableSniff = config.DisableSniff;
HttpPipeliningEnabled = config.HttpPipeliningEnabled;
EnableHttpCompression = config.EnableHttpCompression;
ForceNode = config.ForceNode;
MaxRetries = config.MaxRetries;
MaxRetryTimeout = config.MaxRetryTimeout;
OpaqueId = config.OpaqueId;
PingTimeout = config.PingTimeout;
RequestTimeout = config.RequestTimeout;
RunAs = config.RunAs;
ThrowExceptions = config.ThrowExceptions;
TransferEncodingChunked = config.TransferEncodingChunked;
Headers = (config.Headers is null) ? null : new NameValueCollection(config.Headers);
EnableTcpStats = config.EnableTcpStats;
EnableThreadPoolStats = config.EnableThreadPoolStats;
ResponseHeadersToParse = (config.ResponseHeadersToParse is null) ? null : new HeadersList(config.ResponseHeadersToParse);
ParseAllHeaders = config.ParseAllHeaders;
RequestMetaData = config.RequestMetaData;
}

/// <inheritdoc />
public string? Accept { get; init; }
Expand All @@ -189,7 +231,7 @@ public record RequestConfiguration : IRequestConfiguration
public bool? DisablePings { get; init; }

/// <inheritdoc />
public bool? DisablePing { get; init; }
public bool? DisablePing { get; init; } // TODO: ?

/// <inheritdoc />
public bool? DisableSniff { get; init; }
Expand All @@ -198,7 +240,7 @@ public record RequestConfiguration : IRequestConfiguration
public bool? HttpPipeliningEnabled { get; init; }

/// <inheritdoc />
public bool? EnableHttpPipelining { get; init; } = true;
public bool? EnableHttpPipelining { get; init; } = true; // TODO: ?

/// <inheritdoc />
public bool? EnableHttpCompression { get; init; }
Expand Down Expand Up @@ -247,7 +289,6 @@ public record RequestConfiguration : IRequestConfiguration

/// <inheritdoc />
public RequestMetaData? RequestMetaData { get; init; }

}

/// <inheritdoc cref="IRequestConfiguration"/>
Expand All @@ -256,6 +297,40 @@ public class RequestConfigurationDescriptor : IRequestConfiguration
/// <inheritdoc cref="IRequestConfiguration"/>
public RequestConfigurationDescriptor() { }

/// <inheritdoc cref="IRequestConfiguration"/>
public RequestConfigurationDescriptor(IRequestConfiguration? config)
{
if (config is null)
return;

_accept = config.Accept;
_allowedStatusCodes= config.AllowedStatusCodes;
_authentication = config.Authentication;
_clientCertificates = config.ClientCertificates;
_contentType = config.ContentType;
_disableDirectStreaming = config.DisableDirectStreaming;
_disableAuditTrail = config.DisableAuditTrail;
_disablePings = config.DisablePings;
_disableSniff = config.DisableSniff;
_httpPipeliningEnabled = config.HttpPipeliningEnabled;
_enableHttpCompression = config.EnableHttpCompression;
_forceNode = config.ForceNode;
_maxRetries = config.MaxRetries;
_maxRetryTimeout = config.MaxRetryTimeout;
_opaqueId = config.OpaqueId;
_pingTimeout = config.PingTimeout;
_requestTimeout = config.RequestTimeout;
_runAs = config.RunAs;
_throwExceptions = config.ThrowExceptions;
_transferEncodingChunked = config.TransferEncodingChunked;
_headers = (config.Headers is null) ? null : new NameValueCollection(config.Headers);
_enableTcpStats = config.EnableTcpStats;
_enableThreadPoolStats = config.EnableThreadPoolStats;
_responseHeadersToParse = (config.ResponseHeadersToParse is null) ? null : new HeadersList(config.ResponseHeadersToParse);
_parseAllHeaders = config.ParseAllHeaders;
_requestMetaData = config.RequestMetaData;
}

private string? _accept;
private IReadOnlyCollection<int>? _allowedStatusCodes;
private AuthorizationHeader? _authentication;
Expand All @@ -282,11 +357,10 @@ public RequestConfigurationDescriptor() { }
private RequestMetaData? _requestMetaData;

#pragma warning disable CS0649 // Field is never assigned to, and will always have its default value
private bool? _enableHttpCompression;
private TimeSpan? _maxRetryTimeout;
private bool? _enableHttpCompression; // TODO: ?
private TimeSpan? _maxRetryTimeout; // TODO: ?
#pragma warning restore CS0649 // Field is never assigned to, and will always have its default value


/// <inheritdoc cref="IRequestConfiguration.RunAs"/>
public RequestConfigurationDescriptor RunAs(string username)
{
Expand Down

0 comments on commit e817fce

Please sign in to comment.