Skip to content

Commit

Permalink
Switch from clientfactory to regular httpclient (#823)
Browse files Browse the repository at this point in the history
* Switch from clientfactory to regular httpclient
* fix: inject `NullLogger`

---------

Co-authored-by: Justin Perez <justinperez@microsoft.com>
  • Loading branch information
Omotola and melotic authored Oct 3, 2023
1 parent b1044c7 commit d4c7815
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

<ItemGroup>
<PackageReference Include="DotNet.Glob" />
<PackageReference Include="Microsoft.Extensions.Http" />
<PackageReference Include="Microsoft.Extensions.Logging" />
<PackageReference Include="morelinq" />
<PackageReference Include="NuGet.ProjectModel" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<SimplePyPiClient> logger;

// Keep telemetry on how the cache is being used for future refinements
private readonly SimplePypiCacheTelemetryRecord cacheTelemetry = new SimplePypiCacheTelemetryRecord();

private readonly HttpClient httpClient;

/// <summary>
/// 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.
Expand All @@ -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<SimplePyPiClient> logger)
public SimplePyPiClient(IEnvironmentVariableService environmentVariableService, ILogger<SimplePyPiClient> logger)
{
this.environmentVariableService = environmentVariableService;
this.logger = logger;
this.httpClient = httpClientFactory.CreateClient();
}

public static HttpClient HttpClient { get; internal set; } = new HttpClient(HttpClientHandler);

/// <inheritdoc />
public async Task<SimplePypiProject> GetSimplePypiProjectAsync(PipDependencySpecification spec)
{
Expand Down Expand Up @@ -269,7 +270,7 @@ private async Task<HttpResponseMessage> 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;
}

Expand All @@ -280,6 +281,6 @@ public void Dispose()
this.cacheTelemetry.Dispose();
this.cachedProjectWheelFiles.Dispose();
this.cachedSimplePyPiProjects.Dispose();
this.httpClient.Dispose();
HttpClient.Dispose();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -141,12 +139,6 @@ public static IServiceCollection AddComponentDetection(this IServiceCollection s
services.AddSingleton<IYarnLockFileFactory, YarnLockFileFactory>();
services.AddSingleton<IComponentDetector, YarnLockComponentDetector>();

// HttpClient
services.AddHttpClient();

// Remove the default logging for http client
services.RemoveAll<IHttpMessageHandlerBuilderFilter>();

return services;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<ItemGroup>
<PackageReference Include="CommandLineParser" />
<PackageReference Include="DotNet.Glob" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" />
<PackageReference Include="Microsoft.Extensions.Http" />
<PackageReference Include="Microsoft.Extensions.Logging" />
<PackageReference Include="Newtonsoft.Json" />
<PackageReference Include="Polly" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
Expand All @@ -8,7 +8,6 @@

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" />
<PackageReference Include="Microsoft.Extensions.Http" />
<PackageReference Include="Microsoft.Extensions.Logging" />
<PackageReference Include="Serilog" />
<PackageReference Include="Serilog.Extensions.Logging" />
Expand Down
1 change: 0 additions & 1 deletion src/Microsoft.ComponentDetection/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<LoggingEnricher>()
.Enrich.FromLogContext()
.WriteTo.Map(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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<IComponentDetector>().ToList();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<ItemGroup Label="Project References">
<ProjectReference Include="..\..\src\Microsoft.ComponentDetection.Orchestrator\Microsoft.ComponentDetection.Orchestrator.csproj" />
Expand All @@ -7,7 +7,6 @@

<ItemGroup Label="Package References">
<PackageReference Include="coverlet.collector" PrivateAssets="all" />
<PackageReference Include="Microsoft.Extensions.Http" />
<PackageReference Include="Microsoft.Extensions.Logging" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="NuGet.Versioning" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ namespace Microsoft.ComponentDetection.Detectors.Tests;
[TestClass]
public class SimplePyPiClientTests
{
private readonly Mock<IHttpClientFactory> mockHttpClientFactory = new Mock<IHttpClientFactory>();

private Mock<HttpMessageHandler> MockHttpMessageHandler(string content, HttpStatusCode statusCode)
{
var handlerMock = new Mock<HttpMessageHandler>();
Expand All @@ -41,9 +39,8 @@ private Mock<HttpMessageHandler> MockHttpMessageHandler(string content, HttpStat

private ISimplePyPiClient CreateSimplePypiClient(HttpMessageHandler messageHandler, IEnvironmentVariableService evs, ILogger<SimplePyPiClient> logger)
{
var httpClient = new HttpClient(messageHandler);
this.mockHttpClientFactory.Setup(x => x.CreateClient(It.IsAny<string>())).Returns(httpClient);
return new SimplePyPiClient(evs, this.mockHttpClientFactory.Object, logger);
SimplePyPiClient.HttpClient = new HttpClient(messageHandler);
return new SimplePyPiClient(evs, logger);
}

[TestMethod]
Expand Down

0 comments on commit d4c7815

Please sign in to comment.