Skip to content

Commit

Permalink
Bring you own httpclient (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
s3nthilg0pal authored Dec 23, 2024
1 parent ab8628d commit 090b280
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
3 changes: 2 additions & 1 deletion IpApi.Net.Sample.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ static async Task Main(string[] args)
var host = Host.CreateDefaultBuilder(args)
.ConfigureServices(services =>
{
services.AddIpApiClient();
services.AddHttpClient();
services.AddIpApiClient(useExistingHttpClient:true);
services.AddScoped<IIpService, IpService>();

})
Expand Down
5 changes: 2 additions & 3 deletions IpApi.Net.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.12.35514.174 d17.12
VisualStudioVersion = 17.12.35514.174
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IpApi", "IpApi\IpApi.csproj", "{2DA3FB8F-C9AC-4597-AEFC-C0BD75E87B31}"
EndProject
Expand All @@ -13,8 +13,7 @@ Global
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{2DA3FB8F-C9AC-4597-AEFC-C0BD75E87B31}.Debug|Any CPU.ActiveCfg = Release|Any CPU
{2DA3FB8F-C9AC-4597-AEFC-C0BD75E87B31}.Debug|Any CPU.Build.0 = Release|Any CPU
{2DA3FB8F-C9AC-4597-AEFC-C0BD75E87B31}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2DA3FB8F-C9AC-4597-AEFC-C0BD75E87B31}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2DA3FB8F-C9AC-4597-AEFC-C0BD75E87B31}.Release|Any CPU.Build.0 = Release|Any CPU
{5EB20BE2-F56E-4ABF-9F2F-958FB7AFA579}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
Expand Down
20 changes: 19 additions & 1 deletion IpApi/DependencyInjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,26 @@ namespace IpApi;

public static class DependencyInjection
{
public static IServiceCollection AddIpApiClient(this IServiceCollection serviceCollection)
/// <summary>
/// Adds the <see cref="IHttpClientFactory"/> and IIApiClient to the <see cref="IServiceCollection"/>.
/// Set true to <see cref="useExistingHttpClient"/> if you are bringing you own httpclient.
/// </summary>
/// <param name="serviceCollection">The <see cref="IServiceCollection"/>.</param>
/// <returns>The <see cref="IServiceCollection"/>.</returns>
/// <param name="useExistingHttpClient">Set true if httpclient is already registered</param>
public static IServiceCollection AddIpApiClient(this IServiceCollection serviceCollection, bool useExistingHttpClient = false)
{
if (useExistingHttpClient)
{
serviceCollection.AddScoped<IIApiClient>(sp =>
{
var httpClientFactory = sp.GetRequiredService<IHttpClientFactory>();
return new IpApiClient(httpClientFactory);
});

return serviceCollection;
}

serviceCollection.AddHttpClient();
serviceCollection.AddScoped<IIApiClient, IpApiClient>();

Expand Down
2 changes: 1 addition & 1 deletion IpApi/IpApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ private static async Task HandleErrorAsync(CancellationToken cancellationToken,
using var httpClient = _httpClientFactory.CreateClient();
httpClient.BaseAddress = new Uri(BaseUrl);

var allIps = string.Join(" ", ips);
var allIps = string.Join(",", ips);

var response = await httpClient.GetAsync(allIps, cancellationToken);

Expand Down

0 comments on commit 090b280

Please sign in to comment.