Skip to content

Commit

Permalink
refactor HttpClient initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
drittich committed Mar 26, 2024
1 parent 8ab2009 commit 3825fb6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 45 deletions.
10 changes: 2 additions & 8 deletions DnsTube.Core/Enums/HttpClientType.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DnsTube.Core.Enums
namespace DnsTube.Core.Enums
{
public enum HttpClientName
{
Cloudflare,
GitHub,
IpAddress,
IpAddressV4,
IpAddressV6
}
}
12 changes: 2 additions & 10 deletions DnsTube.Core/Services/IpAddressService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,8 @@ public IpAddressService(ISettingsService settingsService, ILogService logService

var settings = await _settingsService.GetAsync();
var url = protocol == IpSupport.IPv4 ? settings.IPv4_API : settings.IPv6_API;

HttpClient httpClient;

if (protocol == IpSupport.IPv4){
httpClient = _httpClientFactory.CreateClient(HttpClientName.IpAddress.ToString());
}
else
{
httpClient = _httpClientFactory.CreateClient(HttpClientName.IpAddressV6.ToString());
}
var httpClientName = protocol == IpSupport.IPv4 ? HttpClientName.IpAddressV4 : HttpClientName.IpAddressV6;
var httpClient = _httpClientFactory.CreateClient(httpClientName.ToString());

for (var attempts = 0; attempts < maxAttempts; attempts++)
{
Expand Down
49 changes: 22 additions & 27 deletions DnsTube.Service/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,47 +68,42 @@ static async Task ConfigureHttpClientsAsync(WebApplicationBuilder builder, ISett
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls13;

var selectedAdapterName = (await settingsService.GetAsync()).NetworkAdapter;
var settings = await settingsService.GetAsync();
var selectedAdapterName = settings.NetworkAdapter;
bool needsCustomHandler = !string.IsNullOrWhiteSpace(selectedAdapterName) && selectedAdapterName != "_DEFAULT_";

IHttpClientBuilder httpClientBuilder;

httpClientBuilder = builder.Services.AddHttpClient(
HttpClientName.Cloudflare.ToString(),
client =>
var clientConfigurations = new Dictionary<string, Action<HttpClient>>
{
[HttpClientName.Cloudflare.ToString()] = client =>
{
client.BaseAddress = new Uri("https://api.cloudflare.com/client/v4/");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.UserAgent.ParseAdd("DnsTube");
});
if (needsCustomHandler)
{
ConfigureHandler(httpClientBuilder, selectedAdapterName!);
}

httpClientBuilder = builder.Services.AddHttpClient(
HttpClientName.GitHub.ToString(),
client =>
},
[HttpClientName.GitHub.ToString()] = client =>
{
client.BaseAddress = new Uri("https://api.github.com/");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/vnd.github.v3+json"));
client.DefaultRequestHeaders.UserAgent.ParseAdd("DnsTube");
client.DefaultRequestHeaders.Add("Cache-Control", "no-cache");
});
if (needsCustomHandler)
{
ConfigureHandler(httpClientBuilder, selectedAdapterName!);
}

httpClientBuilder = builder.Services.AddHttpClient(
HttpClientName.IpAddress.ToString(),
(client) =>
},
[HttpClientName.IpAddressV4.ToString()] = client =>
{
client.DefaultRequestHeaders.UserAgent.ParseAdd("DnsTube");
},
[HttpClientName.IpAddressV6.ToString()] = client =>
{
client.DefaultRequestHeaders.UserAgent.ParseAdd("DnsTube");
});
if (needsCustomHandler)
}
};

foreach (var config in clientConfigurations)
{
ConfigureHandler(httpClientBuilder, selectedAdapterName!);
var httpClientBuilder = builder.Services.AddHttpClient(config.Key, config.Value);
if (needsCustomHandler)
{
ConfigureHandler(httpClientBuilder, selectedAdapterName!);
}
}
}

Expand Down

0 comments on commit 3825fb6

Please sign in to comment.