Skip to content

Commit c15e518

Browse files
committed
Ensure RequestInvokers do not need ITransportConfiguration.
Information is read at a later time from RequestData which is now likely a static instance.
1 parent 5cf92e1 commit c15e518

File tree

9 files changed

+34
-43
lines changed

9 files changed

+34
-43
lines changed

src/Elastic.Transport/Components/Pipeline/RequestData.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Collections.Specialized;
88
using System.Security.Cryptography.X509Certificates;
99
using Elastic.Transport.Extensions;
10+
using Elastic.Transport.Products;
1011

1112
namespace Elastic.Transport;
1213

@@ -40,6 +41,9 @@ public RequestData(ITransportConfiguration global, IRequestConfiguration? local
4041
ProxyPassword = global.ProxyPassword;
4142
DisableAutomaticProxyDetection = global.DisableAutomaticProxyDetection;
4243
UserAgent = global.UserAgent;
44+
ResponseBuilders = global.ResponseBuilders;
45+
ProductResponseBuilders = global.ProductRegistration.ResponseBuilders;
46+
4347
KeepAliveInterval = (int)(global.KeepAliveInterval?.TotalMilliseconds ?? 2000);
4448
KeepAliveTime = (int)(global.KeepAliveTime?.TotalMilliseconds ?? 2000);
4549
RunAs = local?.RunAs ?? global.RunAs;
@@ -88,6 +92,12 @@ public RequestData(ITransportConfiguration global, IRequestConfiguration? local
8892
}
8993
}
9094

95+
/// <inheritdoc cref="ITransportConfiguration.ResponseBuilders"/>
96+
public IReadOnlyCollection<IResponseBuilder> ProductResponseBuilders { get; }
97+
98+
/// <inheritdoc cref="ITransportConfiguration.ResponseBuilders"/>
99+
public IReadOnlyCollection<IResponseBuilder> ResponseBuilders { get; }
100+
91101
/// <inheritdoc cref="ITransportConfiguration.MemoryStreamFactory"/>
92102
public MemoryStreamFactory MemoryStreamFactory { get; }
93103
/// <inheritdoc cref="ITransportConfiguration.MetaHeaderProvider"/>

src/Elastic.Transport/Components/TransportClient/HttpRequestInvoker-FullFramework.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,7 @@ public class HttpRequestInvoker : HttpWebRequestInvoker
1313
/// <summary>
1414
/// Create a new instance of the <see cref="HttpRequestInvoker"/>.
1515
/// </summary>
16-
public HttpRequestInvoker() : base() { }
17-
18-
/// <summary>
19-
/// Create a new instance of the <see cref="HttpRequestInvoker"/>.
20-
/// </summary>
21-
/// <param name="transportConfiguration">The <see cref="ITransportConfiguration"/> from which response builders can be loaded.</param>
22-
public HttpRequestInvoker(ITransportConfiguration transportConfiguration) : base(transportConfiguration) { }
16+
public HttpRequestInvoker() { }
2317

2418
/// <summary> The default TransportClient implementation. Uses <see cref="HttpWebRequest" /> on the current .NET desktop framework.</summary>
2519
internal HttpRequestInvoker(ResponseFactory responseFactory) : base(responseFactory) { }

src/Elastic.Transport/Components/TransportClient/HttpRequestInvoker.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,7 @@ public class HttpRequestInvoker : IRequestInvoker
3737
/// <summary>
3838
/// Create a new instance of the <see cref="HttpRequestInvoker"/>.
3939
/// </summary>
40-
public HttpRequestInvoker() : this(new TransportConfiguration()) { }
41-
42-
/// <summary>
43-
/// Create a new instance of the <see cref="HttpRequestInvoker"/>.
44-
/// </summary>
45-
/// <param name="transportConfiguration">The <see cref="ITransportConfiguration"/> from which response builders can be loaded.</param>
46-
public HttpRequestInvoker(ITransportConfiguration transportConfiguration) :
47-
this(new DefaultResponseFactory(transportConfiguration)) { }
40+
public HttpRequestInvoker() : this(new DefaultResponseFactory()) { }
4841

4942
internal HttpRequestInvoker(ResponseFactory responseFactory)
5043
{
@@ -56,13 +49,13 @@ internal HttpRequestInvoker(ResponseFactory responseFactory)
5649
/// Allows consumers to inject their own HttpMessageHandler, and optionally call our default implementation.
5750
/// </summary>
5851
public HttpRequestInvoker(Func<HttpMessageHandler, RequestData, HttpMessageHandler> wrappingHandler) :
59-
this(wrappingHandler, new DefaultResponseFactory(new TransportConfiguration())) { }
52+
this(wrappingHandler, new DefaultResponseFactory()) { }
6053

6154
/// <summary>
6255
/// Allows consumers to inject their own HttpMessageHandler, and optionally call our default implementation.
6356
/// </summary>
6457
public HttpRequestInvoker(Func<HttpMessageHandler, RequestData, HttpMessageHandler> wrappingHandler, ITransportConfiguration transportConfiguration) :
65-
this(wrappingHandler, new DefaultResponseFactory(transportConfiguration))
58+
this(wrappingHandler, new DefaultResponseFactory())
6659
{ }
6760

6861
internal HttpRequestInvoker(Func<HttpMessageHandler, RequestData, HttpMessageHandler> wrappingHandler, ResponseFactory responseFactory)

src/Elastic.Transport/Components/TransportClient/HttpWebRequestInvoker.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,7 @@ static HttpWebRequestInvoker()
4545
/// <summary>
4646
/// Create a new instance of the <see cref="HttpWebRequestInvoker"/>.
4747
/// </summary>
48-
public HttpWebRequestInvoker() : this(new TransportConfiguration()) { }
49-
50-
/// <summary>
51-
/// Create a new instance of the <see cref="HttpWebRequestInvoker"/>.
52-
/// </summary>
53-
/// <param name="transportConfiguration">The <see cref="ITransportConfiguration"/> from which response builders can be loaded.</param>
54-
public HttpWebRequestInvoker(ITransportConfiguration transportConfiguration) :
55-
this(new DefaultResponseFactory(transportConfiguration))
56-
{ }
48+
public HttpWebRequestInvoker() : this(new DefaultResponseFactory()) { }
5749

5850
internal HttpWebRequestInvoker(ResponseFactory responseFactory) => ResponseFactory = responseFactory;
5951

src/Elastic.Transport/Components/TransportClient/InMemoryRequestInvoker.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,11 @@ public class InMemoryRequestInvoker : IRequestInvoker
2929
/// Every request will succeed with this overload, note that it won't actually return mocked responses
3030
/// so using this overload might fail if you are using it to test high level bits that need to deserialize the response.
3131
/// </summary>
32-
public InMemoryRequestInvoker() : this(null) { }
33-
34-
/// <inheritdoc cref="InMemoryRequestInvoker()"/>
35-
public InMemoryRequestInvoker(ProductRegistration? productRegistration)
32+
public InMemoryRequestInvoker()
3633
{
3734
_statusCode = 200;
3835

39-
productRegistration ??= DefaultProductRegistration.Default;
40-
ResponseFactory = new DefaultResponseFactory(new TransportConfiguration(null, productRegistration));
36+
ResponseFactory = new DefaultResponseFactory();
4137
}
4238

4339
/// <inheritdoc cref="InMemoryRequestInvoker"/>
@@ -49,7 +45,7 @@ public InMemoryRequestInvoker(byte[] responseBody, int statusCode = 200, Excepti
4945
_contentType = contentType;
5046
_headers = headers;
5147

52-
ResponseFactory = new DefaultResponseFactory(new TransportConfiguration(null, DefaultProductRegistration.Default));
48+
ResponseFactory = new DefaultResponseFactory();
5349
}
5450

5551
/// <inheritdoc />

src/Elastic.Transport/Configuration/TransportConfiguration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ internal TransportConfiguration(
8585
{
8686
//non init properties
8787
NodePool = nodePool;
88-
RequestInvoker = requestInvoker ?? new HttpRequestInvoker(this);
88+
RequestInvoker = requestInvoker ?? new HttpRequestInvoker();
8989
ProductRegistration = productRegistration ?? DefaultProductRegistration.Default;
9090
RequestInvoker = requestInvoker ?? new HttpRequestInvoker();
9191
RequestResponseSerializer = serializer ?? new LowLevelRequestResponseSerializer();

src/Elastic.Transport/Configuration/TransportConfigurationDescriptor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public abstract class TransportConfigurationDescriptorBase<T> : ITransportConfig
6969
protected TransportConfigurationDescriptorBase(NodePool nodePool, IRequestInvoker? requestInvoker, Serializer? requestResponseSerializer, ProductRegistration? productRegistration)
7070
{
7171
_nodePool = nodePool;
72-
_requestInvoker = requestInvoker ?? new HttpRequestInvoker(this);
72+
_requestInvoker = requestInvoker ?? new HttpRequestInvoker();
7373
_productRegistration = productRegistration ?? DefaultProductRegistration.Default;
7474
_requestInvoker = requestInvoker ?? new HttpRequestInvoker();
7575
_requestResponseSerializer = requestResponseSerializer ?? new LowLevelRequestResponseSerializer();

src/Elastic.Transport/Responses/DefaultResponseFactory.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using System.Threading.Tasks;
1212
using Elastic.Transport.Diagnostics;
1313
using Elastic.Transport.Extensions;
14+
using Elastic.Transport.Products;
1415

1516
namespace Elastic.Transport;
1617

@@ -20,7 +21,7 @@ namespace Elastic.Transport;
2021
/// <remarks>
2122
/// Create an instance of the factory using the provided configuration.
2223
/// </remarks>
23-
internal sealed class DefaultResponseFactory(ITransportConfiguration transportConfiguration) : ResponseFactory
24+
internal sealed class DefaultResponseFactory : ResponseFactory
2425
{
2526
private readonly ConcurrentDictionary<Type, IResponseBuilder> _resolvedBuilders = new()
2627
{
@@ -31,7 +32,7 @@ internal sealed class DefaultResponseFactory(ITransportConfiguration transportCo
3132
[typeof(VoidResponse)] = new VoidResponseBuilder()
3233
};
3334

34-
private readonly ITransportConfiguration? _transportConfiguration = transportConfiguration;
35+
//private readonly ITransportConfiguration? _transportConfiguration = transportConfiguration;
3536

3637
/// <inheritdoc/>
3738
public override TResponse Create<TResponse>(
@@ -87,7 +88,8 @@ private async ValueTask<TResponse> CreateCoreAsync<TResponse>(
8788

8889
TResponse? response = null;
8990

90-
if (MayHaveBody(statusCode, endpoint.Method, contentLength) && TryResolveBuilder<TResponse>(out var builder))
91+
if (MayHaveBody(statusCode, endpoint.Method, contentLength)
92+
&& TryResolveBuilder<TResponse>(out var builder, requestData.ProductResponseBuilders, requestData.ResponseBuilders))
9193
{
9294
var ownsStream = false;
9395

@@ -122,17 +124,21 @@ private async ValueTask<TResponse> CreateCoreAsync<TResponse>(
122124
response ??= new TResponse();
123125
response.ApiCallDetails = details;
124126
return response;
125-
}
127+
}
126128

127-
private bool TryResolveBuilder<TResponse>(out IResponseBuilder builder) where TResponse : TransportResponse, new()
129+
private bool TryResolveBuilder<TResponse>(
130+
out IResponseBuilder builder,
131+
IReadOnlyCollection<IResponseBuilder> productResponseBuilders,
132+
IReadOnlyCollection<IResponseBuilder> responseBuilders
133+
) where TResponse : TransportResponse, new()
128134
{
129135
if (_resolvedBuilders.TryGetValue(typeof(TResponse), out builder))
130136
return true;
131137

132-
if (TryFindResponseBuilder(_transportConfiguration.ResponseBuilders, _resolvedBuilders, ref builder))
138+
if (TryFindResponseBuilder(responseBuilders, _resolvedBuilders, ref builder))
133139
return true;
134140

135-
return TryFindResponseBuilder(_transportConfiguration.ProductRegistration.ResponseBuilders, _resolvedBuilders, ref builder);
141+
return TryFindResponseBuilder(productResponseBuilders, _resolvedBuilders, ref builder);
136142

137143
static bool TryFindResponseBuilder(IEnumerable<IResponseBuilder> responseBuilders, ConcurrentDictionary<Type, IResponseBuilder> resolvedBuilders, ref IResponseBuilder builder)
138144
{

tests/Elastic.Transport.Tests/Plumbing/InMemoryConnectionFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public static class InMemoryConnectionFactory
1111
{
1212
public static TransportConfiguration Create(ProductRegistration productRegistration = null)
1313
{
14-
var invoker = new InMemoryRequestInvoker(productRegistration);
14+
var invoker = new InMemoryRequestInvoker();
1515
var pool = new SingleNodePool(new Uri("http://localhost:9200"));
1616
var settings = new TransportConfiguration(pool, invoker, productRegistration: productRegistration);
1717
return settings;

0 commit comments

Comments
 (0)