Skip to content

Commit 858db53

Browse files
authored
Fix httpClient for Apitally Hub (#2)
* Fix httpClient injection * Use named client
1 parent cdb7d25 commit 858db53

File tree

4 files changed

+30
-15
lines changed

4 files changed

+30
-15
lines changed

src/Apitally/ApitallyClient.cs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class ApitallyClient(
1818
IOptions<ApitallyOptions> options,
1919
RequestLogger requestLogger,
2020
ILogger<ApitallyClient> logger,
21-
HttpClient? httpClient = null
21+
IHttpClientFactory httpClientFactory
2222
) : BackgroundService, IDisposable
2323
{
2424
public enum HubRequestStatus
@@ -34,12 +34,9 @@ public enum HubRequestStatus
3434
private const int InitialSyncIntervalSeconds = 10;
3535
private const int InitialPeriodSeconds = 3600;
3636
private const int MaxQueueTimeSeconds = 3600;
37-
private const int RequestTimeoutSeconds = 10;
38-
private static readonly string HubBaseUrl =
39-
Environment.GetEnvironmentVariable("APITALLY_HUB_BASE_URL") ?? "https://hub.apitally.io";
4037

4138
private readonly Guid _instanceUuid = Guid.NewGuid();
42-
private readonly HttpClient _httpClient = httpClient ?? CreateHttpClient();
39+
private readonly HttpClient _httpClient = httpClientFactory.CreateClient("Apitally");
4340
private readonly IAsyncPolicy<HttpResponseMessage> _retryPolicy = CreateRetryPolicy();
4441
private readonly ConcurrentQueue<SyncData> _syncDataQueue = new();
4542
private readonly Random _random = new();
@@ -58,15 +55,6 @@ public enum HubRequestStatus
5855
public readonly ConsumerRegistry ConsumerRegistry = new();
5956
public readonly RequestLogger RequestLogger = requestLogger;
6057

61-
private static HttpClient CreateHttpClient()
62-
{
63-
return new HttpClient
64-
{
65-
BaseAddress = new Uri(HubBaseUrl),
66-
Timeout = TimeSpan.FromSeconds(RequestTimeoutSeconds),
67-
};
68-
}
69-
7058
private static IAsyncPolicy<HttpResponseMessage> CreateRetryPolicy()
7159
{
7260
return HttpPolicyExtensions

src/Apitally/ApitallyExtensions.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,19 @@ public static IServiceCollection AddApitally(
3535
services.PostConfigure(configureOptions);
3636
}
3737

38+
// Register IHttpClientFactory which is required by ApitallyClient
39+
services.AddHttpClient(
40+
"Apitally",
41+
client =>
42+
{
43+
client.BaseAddress = new Uri(
44+
Environment.GetEnvironmentVariable("APITALLY_HUB_BASE_URL")
45+
?? "https://hub.apitally.io"
46+
);
47+
client.Timeout = TimeSpan.FromSeconds(10);
48+
}
49+
);
50+
3851
// Ensure MVC services are registered and add global filter
3952
services.AddSingleton<ValidationErrorFilter>();
4053
services.AddControllers(options =>

tests/Apitally.TestApp/ApitallyTestExtensions.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ public static IServiceCollection AddApitallyWithoutBackgroundServices(
1414
{
1515
services.PostConfigure(configureOptions);
1616
}
17+
18+
services.AddHttpClient(
19+
"Apitally",
20+
client =>
21+
{
22+
client.BaseAddress = new Uri("http://test");
23+
client.Timeout = TimeSpan.FromSeconds(1);
24+
}
25+
);
1726
services.AddSingleton<ValidationErrorFilter>();
1827
services.AddControllers(options =>
1928
{

tests/Apitally.Tests/ApitallyClientTests.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,17 @@ private static ApitallyClient CreateClient(HttpMessageHandler httpHandler)
154154
);
155155
var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole());
156156
var requestLogger = new RequestLogger(options, loggerFactory.CreateLogger<RequestLogger>());
157+
158+
// Create a mock IHttpClientFactory that returns a client with the specified handler
157159
var httpClient = new HttpClient(httpHandler) { BaseAddress = new Uri("http://test") };
160+
var mockHttpClientFactory = new Mock<IHttpClientFactory>();
161+
mockHttpClientFactory.Setup(f => f.CreateClient("Apitally")).Returns(httpClient);
162+
158163
var client = new ApitallyClient(
159164
options,
160165
requestLogger,
161166
loggerFactory.CreateLogger<ApitallyClient>(),
162-
httpClient
167+
mockHttpClientFactory.Object
163168
);
164169
return client;
165170
}

0 commit comments

Comments
 (0)