Skip to content

Commit

Permalink
Add RequestConfigurationDescriptor copy constructor (#136)
Browse files Browse the repository at this point in the history
Add `RequestConfigurationDescriptor` copy constructor.

As well removes `net6` TFMs.
  • Loading branch information
flobernd authored Nov 19, 2024
1 parent 7d3a633 commit af894be
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 20 deletions.
4 changes: 1 addition & 3 deletions src/Elastic.Transport/Configuration/RequestConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
using System.Collections.Specialized;
using System.Security.Cryptography.X509Certificates;

using Elastic.Transport.Extensions;

namespace Elastic.Transport;

/// <inheritdoc cref="IRequestConfiguration"/>
Expand All @@ -31,7 +29,7 @@ public RequestConfiguration()
/// <inheritdoc cref="IRequestConfiguration"/>
public RequestConfiguration(IRequestConfiguration config)
{
#if NET8_0_OR_GREATER
#if NET
ArgumentNullException.ThrowIfNull(config);
#else
if (config is null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Security.Cryptography.X509Certificates;

using Elastic.Transport.Extensions;
Expand All @@ -19,10 +18,14 @@ public class RequestConfigurationDescriptor : IRequestConfiguration
public RequestConfigurationDescriptor() { }

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

_accept = config.Accept;
_allowedStatusCodes= config.AllowedStatusCodes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public TransportConfiguration(
/// Expert usage: Create a new transport configuration based of a previously configured instance
public TransportConfiguration(ITransportConfiguration config)
{
#if NET8_0_OR_GREATER
#if NET
ArgumentNullException.ThrowIfNull(config);
#else
if (config is null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,18 @@ namespace Elastic.Transport;
/// Allows you to control how <see cref="ITransport{TConfiguration}"/> behaves and where/how it connects to Elastic Stack products
/// </summary>
/// <remarks> <inheritdoc cref="TransportConfigurationDescriptor" path="/summary"/></remarks>
/// <param name="nodePool"><inheritdoc cref="NodePool" path="/summary"/></param>
/// <param name="invoker"><inheritdoc cref="IRequestInvoker" path="/summary"/></param>
/// <param name="serializer"><inheritdoc cref="Serializer" path="/summary"/></param>
/// <param name="productRegistration"><inheritdoc cref="ProductRegistration" path="/summary"/></param>
public class TransportConfigurationDescriptor(
NodePool nodePool,
IRequestInvoker? invoker = null,
Serializer? serializer = null,
ProductRegistration? productRegistration = null) : TransportConfigurationDescriptorBase<TransportConfigurationDescriptor>(nodePool, invoker, serializer, productRegistration)
public class TransportConfigurationDescriptor : TransportConfigurationDescriptorBase<TransportConfigurationDescriptor>
{
/// <summary>
/// Creates a new instance of <see cref="TransportConfigurationDescriptor"/>
/// </summary>
/// <param name="nodePool"><inheritdoc cref="NodePool" path="/summary"/></param>
/// <param name="invoker"><inheritdoc cref="IRequestInvoker" path="/summary"/></param>
/// <param name="serializer"><inheritdoc cref="Serializer" path="/summary"/></param>
/// <param name="productRegistration"><inheritdoc cref="ProductRegistration" path="/summary"/></param>
public TransportConfigurationDescriptor(NodePool nodePool, IRequestInvoker? invoker = null, Serializer? serializer = null, ProductRegistration? productRegistration = null)
: base(nodePool, invoker, serializer, productRegistration) { }

/// <summary>
/// Creates a new instance of <see cref="TransportConfigurationDescriptor"/>
/// </summary>
Expand All @@ -51,6 +53,15 @@ public TransportConfigurationDescriptor(string cloudId, BasicAuthentication cred
/// </summary>
public TransportConfigurationDescriptor(string cloudId, Base64ApiKey credentials, ProductRegistration? productRegistration = null)
: this(new CloudNodePool(cloudId, credentials), productRegistration: productRegistration) { }

/// <summary>
/// Creates a new instance of <see cref="TransportConfigurationDescriptor"/>
/// <para>
/// Expert usage: Create a new transport configuration based of a previously configured instance.
/// </para>
/// </summary>
public TransportConfigurationDescriptor(ITransportConfiguration config)
: base(config) { }
}

/// <inheritdoc cref="TransportConfigurationDescriptor"/>>
Expand Down Expand Up @@ -98,6 +109,79 @@ protected TransportConfigurationDescriptorBase(NodePool nodePool, IRequestInvoke
_responseHeadersToParse = new HeadersList(_productRegistration.ResponseHeadersToParse);
}

/// Expert usage: Create a new transport configuration based of a previously configured instance
protected TransportConfigurationDescriptorBase(ITransportConfiguration config)
{
#if NET
ArgumentNullException.ThrowIfNull(config);
#else
if (config is null)
throw new ArgumentNullException(nameof(config));
#endif

// it's important url formatter is repointed to the new instance of ITransportConfiguration
_urlFormatter = new UrlFormatter(this);

_accept = config.Accept;
_allowedStatusCodes = config.AllowedStatusCodes;
_authentication = config.Authentication;
_bootstrapLock = config.BootstrapLock;
_certificateFingerprint = config.CertificateFingerprint;
_clientCertificates = config.ClientCertificates;
_connectionLimit = config.ConnectionLimit;
_contentType = config.ContentType;
_dateTimeProvider = config.DateTimeProvider;
_deadTimeout = config.DeadTimeout;
_disableAuditTrail = config.DisableAuditTrail;
_disableAutomaticProxyDetection = config.DisableAutomaticProxyDetection;
_disableDirectStreaming = config.DisableDirectStreaming;
_disableMetaHeader = config.DisableMetaHeader;
_disablePings = config.DisablePings;
_disableSniff = config.DisableSniff;
_dnsRefreshTimeout = config.DnsRefreshTimeout;
_enableHttpCompression = config.EnableHttpCompression;
_enableTcpStats = config.EnableTcpStats;
_enableThreadPoolStats = config.EnableThreadPoolStats;
_forceNode = config.ForceNode;
_headers = config.Headers;
_httpPipeliningEnabled = config.HttpPipeliningEnabled;
_keepAliveInterval = config.KeepAliveInterval;
_keepAliveTime = config.KeepAliveTime;
_maxDeadTimeout = config.MaxDeadTimeout;
_maxRetries = config.MaxRetries;
_maxRetryTimeout = config.MaxRetryTimeout;
_memoryStreamFactory = config.MemoryStreamFactory;
_nodePool = config.NodePool;
_nodePredicate = config.NodePredicate;
_onRequestCompleted = config.OnRequestCompleted;
_onRequestDataCreated = config.OnRequestDataCreated;
_opaqueId = config.OpaqueId;
_parseAllHeaders = config.ParseAllHeaders;
_pingTimeout = config.PingTimeout;
_pipelineProvider = config.PipelineProvider;
_prettyJson = config.PrettyJson;
_productRegistration = config.ProductRegistration;
_proxyAddress = config.ProxyAddress;
_proxyPassword = config.ProxyPassword;
_proxyUsername = config.ProxyUsername;
_queryStringParameters = config.QueryStringParameters;
_requestInvoker = config.RequestInvoker;
_requestMetaData = config.RequestMetaData;
_requestResponseSerializer = config.RequestResponseSerializer;
_requestTimeout = config.RequestTimeout;
_responseHeadersToParse = config.ResponseHeadersToParse;
_runAs = config.RunAs;
_serverCertificateValidationCallback = config.ServerCertificateValidationCallback;
_skipDeserializationForStatusCodes = config.SkipDeserializationForStatusCodes;
_sniffInformationLifeSpan = config.SniffInformationLifeSpan;
_sniffsOnConnectionFault = config.SniffsOnConnectionFault;
_sniffsOnStartup = config.SniffsOnStartup;
_statusCodeToResponseSuccess = config.StatusCodeToResponseSuccess;
_throwExceptions = config.ThrowExceptions;
_transferEncodingChunked = config.TransferEncodingChunked;
_userAgent = config.UserAgent;
}

private readonly SemaphoreSlim _bootstrapLock;
private readonly NodePool _nodePool;
private readonly ProductRegistration _productRegistration;
Expand Down
4 changes: 2 additions & 2 deletions src/Elastic.Transport/Elastic.Transport.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<Nullable>annotations</Nullable>
<IsPackable>true</IsPackable>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<TargetFrameworks>netstandard2.0;netstandard2.1;net462;net6.0;net8.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;netstandard2.1;net462;net8.0</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
Expand All @@ -29,7 +29,7 @@
</PackageReference>
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' != 'net6.0' And '$(TargetFramework)' != 'net8.0'">
<ItemGroup Condition="'$(TargetFramework)' != 'net8.0'">
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.4" />
<PackageReference Include="System.Buffers" Version="4.5.1" />
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,14 @@ public void CopiesAllProperties()
autoFaker.RuleFor(x => x.ClientCertificates, f => new X509CertificateCollection());

var config = autoFaker.Generate();
var newConfig = new TransportConfiguration(config);

config.Accept.Should().NotBeEmpty();
config.ClientCertificates.Should().NotBeNull();

ITransportConfiguration newConfig = new TransportConfiguration(config);
config.Should().BeEquivalentTo(newConfig);

ITransportConfiguration newDescriptor = new TransportConfigurationDescriptor(config);
config.Should().BeEquivalentTo(newDescriptor);
}
#endif
}

0 comments on commit af894be

Please sign in to comment.