Skip to content

Commit 3825fb6

Browse files
committed
refactor HttpClient initialization
1 parent 8ab2009 commit 3825fb6

File tree

3 files changed

+26
-45
lines changed

3 files changed

+26
-45
lines changed

DnsTube.Core/Enums/HttpClientType.cs

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
6-
7-
namespace DnsTube.Core.Enums
1+
namespace DnsTube.Core.Enums
82
{
93
public enum HttpClientName
104
{
115
Cloudflare,
126
GitHub,
13-
IpAddress,
7+
IpAddressV4,
148
IpAddressV6
159
}
1610
}

DnsTube.Core/Services/IpAddressService.cs

+2-10
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,8 @@ public IpAddressService(ISettingsService settingsService, ILogService logService
3030

3131
var settings = await _settingsService.GetAsync();
3232
var url = protocol == IpSupport.IPv4 ? settings.IPv4_API : settings.IPv6_API;
33-
34-
HttpClient httpClient;
35-
36-
if (protocol == IpSupport.IPv4){
37-
httpClient = _httpClientFactory.CreateClient(HttpClientName.IpAddress.ToString());
38-
}
39-
else
40-
{
41-
httpClient = _httpClientFactory.CreateClient(HttpClientName.IpAddressV6.ToString());
42-
}
33+
var httpClientName = protocol == IpSupport.IPv4 ? HttpClientName.IpAddressV4 : HttpClientName.IpAddressV6;
34+
var httpClient = _httpClientFactory.CreateClient(httpClientName.ToString());
4335

4436
for (var attempts = 0; attempts < maxAttempts; attempts++)
4537
{

DnsTube.Service/Program.cs

+22-27
Original file line numberDiff line numberDiff line change
@@ -68,47 +68,42 @@ static async Task ConfigureHttpClientsAsync(WebApplicationBuilder builder, ISett
6868
{
6969
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls13;
7070

71-
var selectedAdapterName = (await settingsService.GetAsync()).NetworkAdapter;
71+
var settings = await settingsService.GetAsync();
72+
var selectedAdapterName = settings.NetworkAdapter;
7273
bool needsCustomHandler = !string.IsNullOrWhiteSpace(selectedAdapterName) && selectedAdapterName != "_DEFAULT_";
7374

74-
IHttpClientBuilder httpClientBuilder;
75-
76-
httpClientBuilder = builder.Services.AddHttpClient(
77-
HttpClientName.Cloudflare.ToString(),
78-
client =>
75+
var clientConfigurations = new Dictionary<string, Action<HttpClient>>
76+
{
77+
[HttpClientName.Cloudflare.ToString()] = client =>
7978
{
8079
client.BaseAddress = new Uri("https://api.cloudflare.com/client/v4/");
8180
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
8281
client.DefaultRequestHeaders.UserAgent.ParseAdd("DnsTube");
83-
});
84-
if (needsCustomHandler)
85-
{
86-
ConfigureHandler(httpClientBuilder, selectedAdapterName!);
87-
}
88-
89-
httpClientBuilder = builder.Services.AddHttpClient(
90-
HttpClientName.GitHub.ToString(),
91-
client =>
82+
},
83+
[HttpClientName.GitHub.ToString()] = client =>
9284
{
9385
client.BaseAddress = new Uri("https://api.github.com/");
9486
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/vnd.github.v3+json"));
9587
client.DefaultRequestHeaders.UserAgent.ParseAdd("DnsTube");
9688
client.DefaultRequestHeaders.Add("Cache-Control", "no-cache");
97-
});
98-
if (needsCustomHandler)
99-
{
100-
ConfigureHandler(httpClientBuilder, selectedAdapterName!);
101-
}
102-
103-
httpClientBuilder = builder.Services.AddHttpClient(
104-
HttpClientName.IpAddress.ToString(),
105-
(client) =>
89+
},
90+
[HttpClientName.IpAddressV4.ToString()] = client =>
91+
{
92+
client.DefaultRequestHeaders.UserAgent.ParseAdd("DnsTube");
93+
},
94+
[HttpClientName.IpAddressV6.ToString()] = client =>
10695
{
10796
client.DefaultRequestHeaders.UserAgent.ParseAdd("DnsTube");
108-
});
109-
if (needsCustomHandler)
97+
}
98+
};
99+
100+
foreach (var config in clientConfigurations)
110101
{
111-
ConfigureHandler(httpClientBuilder, selectedAdapterName!);
102+
var httpClientBuilder = builder.Services.AddHttpClient(config.Key, config.Value);
103+
if (needsCustomHandler)
104+
{
105+
ConfigureHandler(httpClientBuilder, selectedAdapterName!);
106+
}
112107
}
113108
}
114109

0 commit comments

Comments
 (0)