Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions Poolz.Finance.CSharp.Http.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.13.35828.75
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{02EA681E-C7D8-13C7-8484-4AC65E1B71E8}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{C43445A2-69BB-48F5-8D8B-45F82D29028D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Poolz.Finance.CSharp.Http", "src\Poolz.Finance.CSharp.Http\Poolz.Finance.CSharp.Http.csproj", "{6EAF7CF1-0D92-218F-D1AC-37BD1E926245}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Poolz.Finance.CSharp.Http.Tests", "tests\Poolz.Finance.CSharp.Http.Tests\Poolz.Finance.CSharp.Http.Tests.csproj", "{7B8DC796-734F-C107-EB88-001D0D01E09F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{6EAF7CF1-0D92-218F-D1AC-37BD1E926245}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6EAF7CF1-0D92-218F-D1AC-37BD1E926245}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6EAF7CF1-0D92-218F-D1AC-37BD1E926245}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6EAF7CF1-0D92-218F-D1AC-37BD1E926245}.Release|Any CPU.Build.0 = Release|Any CPU
{7B8DC796-734F-C107-EB88-001D0D01E09F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7B8DC796-734F-C107-EB88-001D0D01E09F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7B8DC796-734F-C107-EB88-001D0D01E09F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7B8DC796-734F-C107-EB88-001D0D01E09F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{6EAF7CF1-0D92-218F-D1AC-37BD1E926245} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
{7B8DC796-734F-C107-EB88-001D0D01E09F} = {C43445A2-69BB-48F5-8D8B-45F82D29028D}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {AF2F8BF3-0CF6-467C-944A-792A7D687E3C}
EndGlobalSection
EndGlobal
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,25 @@
# Poolz.Finance.CSharp.LibTemplate
# Poolz.Finance.CSharp.Http

Poolz.Finance.CSharp.Http is a lightweight .NET 8 library that centralizes the creation of `HttpClient` instances for Poolz Finance services.
It wraps the default `HttpClientHandler` with a custom `FailureOnlyLoggingHandler` that surfaces detailed information when requests fail while keeping successful responses silent.

## Getting started

```csharp
using Poolz.Finance.CSharp.Http;

var factory = new HttpClientFactory();
var client = factory.Create(
url: "https://api.poolz.finance",
configureHeaders: headers =>
{
headers.Add("Authorization", "Bearer <token>");
headers.Add("User-Agent", "Poolz-SDK-Demo/1.0");
});

var response = await client.GetAsync("/v1/pools");
var content = await response.Content.ReadAsStringAsync();
```

If the request fails, `FailureOnlyLoggingHandler` rethrows an exception that includes the HTTP method and URL so that you immediately know which call needs attention.

22 changes: 22 additions & 0 deletions src/Poolz.Finance.CSharp.Http/FailureOnlyLoggingHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace Poolz.Finance.CSharp.Http;

public class FailureOnlyLoggingHandler(HttpMessageHandler innerHandler) : DelegatingHandler(innerHandler)
{
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage req, CancellationToken ct)
{
try
{
var response = await base.SendAsync(req, ct);
response.EnsureSuccessStatusCode();
return response;
}
catch (HttpRequestException exception)
{
throw new HttpRequestException(
$"HTTP request failed. METHOD: {req.Method}. URL: {req.RequestUri}",
exception,
exception.StatusCode
);
}
}
}
17 changes: 17 additions & 0 deletions src/Poolz.Finance.CSharp.Http/HttpClientFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Net.Http.Headers;

namespace Poolz.Finance.CSharp.Http;

public class HttpClientFactory : IHttpClientFactory
{
public HttpClient Create(string url, Action<HttpRequestHeaders>? configureHeaders = null)
{
var client = new HttpClient(new FailureOnlyLoggingHandler(new HttpClientHandler()))
{
BaseAddress = new Uri(url)
};

configureHeaders?.Invoke(client.DefaultRequestHeaders);
return client;
}
}
8 changes: 8 additions & 0 deletions src/Poolz.Finance.CSharp.Http/IHttpClientFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System.Net.Http.Headers;

namespace Poolz.Finance.CSharp.Http;

public interface IHttpClientFactory
{
public HttpClient Create(string url, Action<HttpRequestHeaders>? configure = null);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using Xunit;
using System.Net;

namespace Poolz.Finance.CSharp.Http.Tests;

public class FailureOnlyLoggingHandlerTests
{
[Fact]
public async Task SendAsync_ReturnsResponse_WhenStatusIsSuccessful()
{
using var handler = new FailureOnlyLoggingHandler(new StubHandler((_, _) =>
Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK))));
using var invoker = new HttpMessageInvoker(handler);
using var request = new HttpRequestMessage(HttpMethod.Get, "https://poolz.finance/success");

var response = await invoker.SendAsync(request, CancellationToken.None);

Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}

[Fact]
public async Task SendAsync_ThrowsHttpRequestExceptionWithContext_WhenStatusIsFailure()
{
using var handler = new FailureOnlyLoggingHandler(new StubHandler((_, _) =>
Task.FromResult(new HttpResponseMessage(HttpStatusCode.BadRequest))));
using var invoker = new HttpMessageInvoker(handler);
using var request = new HttpRequestMessage(HttpMethod.Post, "https://poolz.finance/failure");

var exception = await Assert.ThrowsAsync<HttpRequestException>(() => invoker.SendAsync(request, CancellationToken.None));

Assert.Equal(HttpStatusCode.BadRequest, exception.StatusCode);
Assert.NotNull(exception.InnerException);
Assert.Contains("METHOD: POST", exception.Message);
Assert.Contains("URL: https://poolz.finance/failure", exception.Message);
}

private sealed class StubHandler(
Func<HttpRequestMessage, CancellationToken, Task<HttpResponseMessage>> responseFactory)
: HttpMessageHandler
{
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
return responseFactory(request, cancellationToken);
}
}
}
22 changes: 22 additions & 0 deletions tests/Poolz.Finance.CSharp.Http.Tests/HttpClientFactoryTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Xunit;

namespace Poolz.Finance.CSharp.Http.Tests;

public class HttpClientFactoryTests
{
[Fact]
public void Create_ConfiguresBaseAddressAndHeaders()
{
var factory = new HttpClientFactory();
var expectedBaseAddress = new Uri("https://api.poolz.finance/");

using var client = factory.Create(expectedBaseAddress.ToString(), headers =>
{
headers.Add("X-Test", "42");
});

Assert.Equal(expectedBaseAddress, client.BaseAddress);
Assert.True(client.DefaultRequestHeaders.Contains("X-Test"));
Assert.Equal("42", client.DefaultRequestHeaders.GetValues("X-Test").Single());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>Poolz.Finance.CSharp.Http.Tests</RootNamespace>
<AssemblyName>Poolz.Finance.CSharp.Http.Tests</AssemblyName>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.11.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.2.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Poolz.Finance.CSharp.Http\Poolz.Finance.CSharp.Http.csproj" />
</ItemGroup>

</Project>