Skip to content

Commit

Permalink
Support HTTP handler filters
Browse files Browse the repository at this point in the history
  • Loading branch information
kmcclellan committed Aug 11, 2023
1 parent bca2896 commit 22bc76e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@ sealed class ConfigureClientFromNodes
{
readonly IServiceProvider _provider;
readonly IOptionsMonitor<HttpClientFactoryOptions> _httpOptions;
readonly IEnumerable<IHttpMessageHandlerBuilderFilter> _httpFilters;

public ConfigureClientFromNodes(
IServiceProvider provider,
IOptionsFactory<ElasticsearchNodeOptions> nodeOptions,
IOptionsMonitor<HttpClientFactoryOptions> httpOptions)
IOptionsMonitor<HttpClientFactoryOptions> httpOptions,
IEnumerable<IHttpMessageHandlerBuilderFilter> httpFilters)
: base(nodeOptions)
{
_provider = provider;
_httpOptions = httpOptions;
_httpFilters = httpFilters;
}

protected override void Configure(ElasticsearchClientOptions options, ElasticsearchNodeOptions dependency)
Expand All @@ -28,6 +31,6 @@ protected override void Configure(ElasticsearchClientOptions options, Elasticsea

var fallback = options.NodePool;
options.NodePool = () => dependency.CreatePool() ?? fallback();
options.Connection = () => new HttpFactoryClient(_provider, _httpOptions);
options.Connection = () => new HttpFactoryClient(_provider, _httpOptions, _httpFilters);
}
}
27 changes: 22 additions & 5 deletions Elasticstretch.DependencyInjection/Options/HttpFactoryClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,40 @@ sealed class HttpFactoryClient : HttpTransportClient

readonly IServiceProvider _provider;
readonly IOptionsMonitor<HttpClientFactoryOptions> _options;
readonly IEnumerable<IHttpMessageHandlerBuilderFilter> _filters;

public HttpFactoryClient(IServiceProvider provider, IOptionsMonitor<HttpClientFactoryOptions> options)
public HttpFactoryClient(
IServiceProvider provider,
IOptionsMonitor<HttpClientFactoryOptions> options,
IEnumerable<IHttpMessageHandlerBuilderFilter> filters)
{
_provider = provider;
_options = options;
_filters = filters;
}

protected override HttpMessageHandler CreateHttpClientHandler(RequestData requestData)
{
var builder = _provider.GetRequiredService<HttpMessageHandlerBuilder>();
builder.PrimaryHandler = base.CreateHttpClientHandler(requestData);
var clientOptions = _options.Get(ClientName);

var configureHandler = (HttpMessageHandlerBuilder builder) =>
{
foreach (var item in clientOptions.HttpMessageHandlerBuilderActions)
{
item(builder);
}
};

foreach (var item in _options.Get(ClientName).HttpMessageHandlerBuilderActions)
// Not sure why filters are needed in addition to builder actions, but OK...
foreach (var filter in _filters.Reverse())
{
item(builder);
configureHandler = filter.Configure(configureHandler);
}

var builder = _provider.GetRequiredService<HttpMessageHandlerBuilder>();
configureHandler(builder);

builder.PrimaryHandler = base.CreateHttpClientHandler(requestData);
return builder.Build();
}
}

0 comments on commit 22bc76e

Please sign in to comment.