From 14d8e41924bc051e27bb86d411b88e075fab9e8f Mon Sep 17 00:00:00 2001 From: "Collin M. Barrett" Date: Wed, 3 Jul 2019 09:16:13 -0500 Subject: [PATCH] tidy AgentHttpClient registration and extract interface --- FilterLists.sln.DotSettings | 5 ++++- .../Extensions/ServiceCollectionExtensions.cs | 15 ++++++++++----- .../Features/Lists/DownloadRawText.cs | 4 ++-- .../Features/Lists/DownloadSevenZip.cs | 4 ++-- .../Features/Lists/DownloadZip.cs | 4 ++-- .../Features/Urls/ValidateUrls.cs | 4 ++-- .../Infrastructure/Clients/AgentHttpClient.cs | 13 ++++++++----- 7 files changed, 30 insertions(+), 19 deletions(-) diff --git a/FilterLists.sln.DotSettings b/FilterLists.sln.DotSettings index 0790d253f..6b8e71c26 100644 --- a/FilterLists.sln.DotSettings +++ b/FilterLists.sln.DotSettings @@ -1,4 +1,7 @@ - + ExplicitlyExcluded True diff --git a/src/FilterLists.Agent/Extensions/ServiceCollectionExtensions.cs b/src/FilterLists.Agent/Extensions/ServiceCollectionExtensions.cs index 0ee7872aa..d5b22780c 100644 --- a/src/FilterLists.Agent/Extensions/ServiceCollectionExtensions.cs +++ b/src/FilterLists.Agent/Extensions/ServiceCollectionExtensions.cs @@ -18,11 +18,7 @@ public static void RegisterAgentServices(this IServiceCollection services) services.AddConfiguration(); services.AddLoggingCustom(); services.AddMediatR(typeof(Program).Assembly); - services.AddHttpClient().ConfigureHttpMessageHandlerBuilder(b => - { - b.PrimaryHandler = new HttpClientHandler {AllowAutoRedirect = false}; - b.Build(); - }); + services.AddAgentHttpClient(); services.AddSingleton(); services.AddSingleton(); services.AddTransient(); @@ -43,6 +39,15 @@ private static void AddConfiguration(this IServiceCollection services) services.Configure(config.GetSection(nameof(GitHub))); } + private static void AddAgentHttpClient(this IServiceCollection services) + { + services.AddHttpClient().ConfigureHttpMessageHandlerBuilder(b => + { + b.PrimaryHandler = new HttpClientHandler {AllowAutoRedirect = false}; + b.Build(); + }); + } + private static void AddLoggingCustom(this IServiceCollection services) { services.AddLogging(b => diff --git a/src/FilterLists.Agent/Features/Lists/DownloadRawText.cs b/src/FilterLists.Agent/Features/Lists/DownloadRawText.cs index 3a83aeef8..e1deb7c23 100644 --- a/src/FilterLists.Agent/Features/Lists/DownloadRawText.cs +++ b/src/FilterLists.Agent/Features/Lists/DownloadRawText.cs @@ -30,9 +30,9 @@ public class Handler : AsyncRequestHandler private readonly HttpClient _httpClient; private readonly ILogger _logger; - public Handler(AgentHttpClient agentHttpClient, ILogger logger) + public Handler(IAgentHttpClient agentHttpClient, ILogger logger) { - _httpClient = agentHttpClient.Client; + _httpClient = agentHttpClient.HttpClient; _logger = logger; } diff --git a/src/FilterLists.Agent/Features/Lists/DownloadSevenZip.cs b/src/FilterLists.Agent/Features/Lists/DownloadSevenZip.cs index f5358dcde..5432f17cc 100644 --- a/src/FilterLists.Agent/Features/Lists/DownloadSevenZip.cs +++ b/src/FilterLists.Agent/Features/Lists/DownloadSevenZip.cs @@ -30,9 +30,9 @@ public class Handler : AsyncRequestHandler private readonly HttpClient _httpClient; private readonly ILogger _logger; - public Handler(AgentHttpClient agentHttpClient, ILogger logger) + public Handler(IAgentHttpClient agentHttpClient, ILogger logger) { - _httpClient = agentHttpClient.Client; + _httpClient = agentHttpClient.HttpClient; _logger = logger; } diff --git a/src/FilterLists.Agent/Features/Lists/DownloadZip.cs b/src/FilterLists.Agent/Features/Lists/DownloadZip.cs index e9e982818..cac13116e 100644 --- a/src/FilterLists.Agent/Features/Lists/DownloadZip.cs +++ b/src/FilterLists.Agent/Features/Lists/DownloadZip.cs @@ -29,9 +29,9 @@ public class Handler : AsyncRequestHandler private readonly HttpClient _httpClient; private readonly ILogger _logger; - public Handler(AgentHttpClient agentHttpClient, ILogger logger) + public Handler(IAgentHttpClient agentHttpClient, ILogger logger) { - _httpClient = agentHttpClient.Client; + _httpClient = agentHttpClient.HttpClient; _logger = logger; } diff --git a/src/FilterLists.Agent/Features/Urls/ValidateUrls.cs b/src/FilterLists.Agent/Features/Urls/ValidateUrls.cs index af6367059..57a3963dd 100644 --- a/src/FilterLists.Agent/Features/Urls/ValidateUrls.cs +++ b/src/FilterLists.Agent/Features/Urls/ValidateUrls.cs @@ -31,9 +31,9 @@ public class Handler : IRequestHandler> private readonly HttpClient _httpClient; private readonly ILogger _logger; - public Handler(AgentHttpClient agentHttpClient, ILogger logger) + public Handler(IAgentHttpClient agentHttpClient, ILogger logger) { - _httpClient = agentHttpClient.Client; + _httpClient = agentHttpClient.HttpClient; _logger = logger; } diff --git a/src/FilterLists.Agent/Infrastructure/Clients/AgentHttpClient.cs b/src/FilterLists.Agent/Infrastructure/Clients/AgentHttpClient.cs index 12b21a0c3..e66ab3904 100644 --- a/src/FilterLists.Agent/Infrastructure/Clients/AgentHttpClient.cs +++ b/src/FilterLists.Agent/Infrastructure/Clients/AgentHttpClient.cs @@ -1,12 +1,15 @@ using System; using System.Net.Http; using System.Net.Http.Headers; -using JetBrains.Annotations; namespace FilterLists.Agent.Infrastructure.Clients { - [UsedImplicitly] - public class AgentHttpClient + public interface IAgentHttpClient + { + HttpClient HttpClient { get; } + } + + public class AgentHttpClient : IAgentHttpClient { public AgentHttpClient(HttpClient client) { @@ -16,9 +19,9 @@ public AgentHttpClient(HttpClient client) var userAgent = new ProductInfoHeaderValue(header); client.DefaultRequestHeaders.UserAgent.Add(userAgent); - Client = client; + HttpClient = client; } - public HttpClient Client { get; } + public HttpClient HttpClient { get; } } } \ No newline at end of file