Skip to content

Commit

Permalink
Switch from clientfactory to regular httpclient
Browse files Browse the repository at this point in the history
  • Loading branch information
Omotola committed Sep 30, 2023
1 parent 56b3100 commit ed06a11
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 20 deletions.
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;
}
}
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 @@ -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 ed06a11

Please sign in to comment.