From d4c78153509b905b821441912b14205f2090c77f Mon Sep 17 00:00:00 2001 From: Omotola Date: Tue, 3 Oct 2023 14:27:22 -0700 Subject: [PATCH] Switch from clientfactory to regular httpclient (#823) * Switch from clientfactory to regular httpclient * fix: inject `NullLogger` --------- Co-authored-by: Justin Perez --- .../Microsoft.ComponentDetection.Detectors.csproj | 1 - .../pip/SimplePypiClient.cs | 13 +++++++------ .../Extensions/ServiceCollectionExtensions.cs | 8 -------- ...Microsoft.ComponentDetection.Orchestrator.csproj | 3 +-- .../Microsoft.ComponentDetection.csproj | 3 +-- src/Microsoft.ComponentDetection/Program.cs | 1 - .../ComponentDetectorTests.cs | 7 ++++++- ...rosoft.ComponentDetection.Detectors.Tests.csproj | 3 +-- .../SimplePypiClientTests.cs | 7 ++----- 9 files changed, 18 insertions(+), 28 deletions(-) diff --git a/src/Microsoft.ComponentDetection.Detectors/Microsoft.ComponentDetection.Detectors.csproj b/src/Microsoft.ComponentDetection.Detectors/Microsoft.ComponentDetection.Detectors.csproj index 88d335607..aaf297b83 100644 --- a/src/Microsoft.ComponentDetection.Detectors/Microsoft.ComponentDetection.Detectors.csproj +++ b/src/Microsoft.ComponentDetection.Detectors/Microsoft.ComponentDetection.Detectors.csproj @@ -2,7 +2,6 @@ - diff --git a/src/Microsoft.ComponentDetection.Detectors/pip/SimplePypiClient.cs b/src/Microsoft.ComponentDetection.Detectors/pip/SimplePypiClient.cs index bade8fcff..72b9bb0e7 100644 --- a/src/Microsoft.ComponentDetection.Detectors/pip/SimplePypiClient.cs +++ b/src/Microsoft.ComponentDetection.Detectors/pip/SimplePypiClient.cs @@ -33,14 +33,14 @@ public sealed class SimplePyPiClient : ISimplePyPiClient, IDisposable // time to wait before retrying a failed call to pypi.org private static readonly TimeSpan RETRYDELAY = TimeSpan.FromSeconds(1); + private static readonly HttpClientHandler HttpClientHandler = new HttpClientHandler() { CheckCertificateRevocationList = true }; + private readonly IEnvironmentVariableService environmentVariableService; private readonly ILogger logger; // Keep telemetry on how the cache is being used for future refinements private readonly SimplePypiCacheTelemetryRecord cacheTelemetry = new SimplePypiCacheTelemetryRecord(); - private readonly HttpClient httpClient; - /// /// A thread safe cache implementation which contains a mapping of URI -> SimpleProject for simplepypi api projects /// and has a limited number of entries which will expire after the cache fills or a specified interval. @@ -58,13 +58,14 @@ public sealed class SimplePyPiClient : ISimplePyPiClient, IDisposable // retries used so far for calls to pypi.org private long retries; - public SimplePyPiClient(IEnvironmentVariableService environmentVariableService, IHttpClientFactory httpClientFactory, ILogger logger) + public SimplePyPiClient(IEnvironmentVariableService environmentVariableService, ILogger logger) { this.environmentVariableService = environmentVariableService; this.logger = logger; - this.httpClient = httpClientFactory.CreateClient(); } + public static HttpClient HttpClient { get; internal set; } = new HttpClient(HttpClientHandler); + /// public async Task GetSimplePypiProjectAsync(PipDependencySpecification spec) { @@ -269,7 +270,7 @@ private async Task GetPypiResponseAsync(Uri uri) request.Headers.UserAgent.Add(ProductValue); request.Headers.UserAgent.Add(CommentValue); request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/vnd.pypi.simple.v1+json")); - var response = await this.httpClient.SendAsync(request); + var response = await HttpClient.SendAsync(request); return response; } @@ -280,6 +281,6 @@ public void Dispose() this.cacheTelemetry.Dispose(); this.cachedProjectWheelFiles.Dispose(); this.cachedSimplePyPiProjects.Dispose(); - this.httpClient.Dispose(); + HttpClient.Dispose(); } } diff --git a/src/Microsoft.ComponentDetection.Orchestrator/Extensions/ServiceCollectionExtensions.cs b/src/Microsoft.ComponentDetection.Orchestrator/Extensions/ServiceCollectionExtensions.cs index 8c726ea07..efbdb0a3c 100644 --- a/src/Microsoft.ComponentDetection.Orchestrator/Extensions/ServiceCollectionExtensions.cs +++ b/src/Microsoft.ComponentDetection.Orchestrator/Extensions/ServiceCollectionExtensions.cs @@ -27,8 +27,6 @@ namespace Microsoft.ComponentDetection.Orchestrator.Extensions; using Microsoft.ComponentDetection.Orchestrator.Services; using Microsoft.ComponentDetection.Orchestrator.Services.GraphTranslation; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.DependencyInjection.Extensions; -using Microsoft.Extensions.Http; public static class ServiceCollectionExtensions { @@ -141,12 +139,6 @@ public static IServiceCollection AddComponentDetection(this IServiceCollection s services.AddSingleton(); services.AddSingleton(); - // HttpClient - services.AddHttpClient(); - - // Remove the default logging for http client - services.RemoveAll(); - return services; } } diff --git a/src/Microsoft.ComponentDetection.Orchestrator/Microsoft.ComponentDetection.Orchestrator.csproj b/src/Microsoft.ComponentDetection.Orchestrator/Microsoft.ComponentDetection.Orchestrator.csproj index 2309c7b72..81d898362 100644 --- a/src/Microsoft.ComponentDetection.Orchestrator/Microsoft.ComponentDetection.Orchestrator.csproj +++ b/src/Microsoft.ComponentDetection.Orchestrator/Microsoft.ComponentDetection.Orchestrator.csproj @@ -1,10 +1,9 @@ - + - diff --git a/src/Microsoft.ComponentDetection/Microsoft.ComponentDetection.csproj b/src/Microsoft.ComponentDetection/Microsoft.ComponentDetection.csproj index 087560fd2..3540ab1b9 100644 --- a/src/Microsoft.ComponentDetection/Microsoft.ComponentDetection.csproj +++ b/src/Microsoft.ComponentDetection/Microsoft.ComponentDetection.csproj @@ -1,4 +1,4 @@ - + Exe @@ -8,7 +8,6 @@ - diff --git a/src/Microsoft.ComponentDetection/Program.cs b/src/Microsoft.ComponentDetection/Program.cs index 60fa9d013..2c9134aec 100644 --- a/src/Microsoft.ComponentDetection/Program.cs +++ b/src/Microsoft.ComponentDetection/Program.cs @@ -25,7 +25,6 @@ .AddComponentDetection() .AddLogging(l => l.AddSerilog(new LoggerConfiguration() .MinimumLevel.ControlledBy(Interceptor.LogLevel) - .MinimumLevel.Override("Microsoft.Extensions.Http.DefaultHttpClientFactory", LogEventLevel.Information) .Enrich.With() .Enrich.FromLogContext() .WriteTo.Map( diff --git a/test/Microsoft.ComponentDetection.Detectors.Tests/ComponentDetectorTests.cs b/test/Microsoft.ComponentDetection.Detectors.Tests/ComponentDetectorTests.cs index 7c1e6d1b1..ffe405ba8 100644 --- a/test/Microsoft.ComponentDetection.Detectors.Tests/ComponentDetectorTests.cs +++ b/test/Microsoft.ComponentDetection.Detectors.Tests/ComponentDetectorTests.cs @@ -6,6 +6,8 @@ namespace Microsoft.ComponentDetection.Detectors.Tests; using Microsoft.ComponentDetection.Contracts; using Microsoft.ComponentDetection.Orchestrator.Extensions; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; using Microsoft.VisualStudio.TestTools.UnitTesting; [TestClass] @@ -18,7 +20,10 @@ public class ComponentDetectorTests [TestInitialize] public void Initialize() { - var serviceProvider = new ServiceCollection().AddComponentDetection().BuildServiceProvider(); + var serviceProvider = new ServiceCollection() + .AddComponentDetection() + .AddSingleton(typeof(ILogger<>), typeof(NullLogger<>)) + .BuildServiceProvider(); this.detectors = serviceProvider.GetServices().ToList(); } diff --git a/test/Microsoft.ComponentDetection.Detectors.Tests/Microsoft.ComponentDetection.Detectors.Tests.csproj b/test/Microsoft.ComponentDetection.Detectors.Tests/Microsoft.ComponentDetection.Detectors.Tests.csproj index 7be9fd172..dcf4f2fd0 100644 --- a/test/Microsoft.ComponentDetection.Detectors.Tests/Microsoft.ComponentDetection.Detectors.Tests.csproj +++ b/test/Microsoft.ComponentDetection.Detectors.Tests/Microsoft.ComponentDetection.Detectors.Tests.csproj @@ -1,4 +1,4 @@ - + @@ -7,7 +7,6 @@ - diff --git a/test/Microsoft.ComponentDetection.Detectors.Tests/SimplePypiClientTests.cs b/test/Microsoft.ComponentDetection.Detectors.Tests/SimplePypiClientTests.cs index fa72c898b..e1b91127c 100644 --- a/test/Microsoft.ComponentDetection.Detectors.Tests/SimplePypiClientTests.cs +++ b/test/Microsoft.ComponentDetection.Detectors.Tests/SimplePypiClientTests.cs @@ -20,8 +20,6 @@ namespace Microsoft.ComponentDetection.Detectors.Tests; [TestClass] public class SimplePyPiClientTests { - private readonly Mock mockHttpClientFactory = new Mock(); - private Mock MockHttpMessageHandler(string content, HttpStatusCode statusCode) { var handlerMock = new Mock(); @@ -41,9 +39,8 @@ private Mock MockHttpMessageHandler(string content, HttpStat private ISimplePyPiClient CreateSimplePypiClient(HttpMessageHandler messageHandler, IEnvironmentVariableService evs, ILogger logger) { - var httpClient = new HttpClient(messageHandler); - this.mockHttpClientFactory.Setup(x => x.CreateClient(It.IsAny())).Returns(httpClient); - return new SimplePyPiClient(evs, this.mockHttpClientFactory.Object, logger); + SimplePyPiClient.HttpClient = new HttpClient(messageHandler); + return new SimplePyPiClient(evs, logger); } [TestMethod]