Skip to content

Commit 837544b

Browse files
committed
feat: Add IApiToolkitClientFactory interface and implementation
- Added IApiToolkitClientFactory interface to provide an abstraction for creating HttpClient instances with specific ATOptions. - Implemented ApiToolkitClientFactory class, which leverages HttpClientFactory for centralized management of HttpClient instances.
1 parent f27ba9b commit 837544b

File tree

2 files changed

+96
-1
lines changed

2 files changed

+96
-1
lines changed

ApiToolKit.cs

+31-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Newtonsoft.Json.Linq;
1010
using Newtonsoft.Json;
1111
using System.Diagnostics;
12+
using Microsoft.Extensions.DependencyInjection;
1213
using System.Text.Json;
1314
using static System.Web.HttpUtility;
1415

@@ -424,6 +425,31 @@ public class ATError
424425
public string StackTrace { get; set; }
425426
}
426427

428+
public interface IApiToolkitClientFactory
429+
{
430+
HttpClient CreateClient(ATOptions options);
431+
}
432+
433+
public class ApiToolkitClientFactory : IApiToolkitClientFactory
434+
{
435+
private readonly IHttpClientFactory _httpClientFactory;
436+
private readonly IServiceProvider _serviceProvider;
437+
438+
public ApiToolkitClientFactory(IHttpClientFactory httpClientFactory, IServiceProvider serviceProvider)
439+
{
440+
_httpClientFactory = httpClientFactory;
441+
_serviceProvider = serviceProvider;
442+
}
443+
444+
public HttpClient CreateClient(ATOptions options)
445+
{
446+
var client = _httpClientFactory.CreateClient();
447+
448+
var handler = _serviceProvider.GetRequiredService<ObservingHandler>();
449+
handler.SetOptions(options);
450+
return new HttpClient(handler);
451+
}
452+
}
427453

428454
public class ATOptions
429455
{
@@ -436,9 +462,13 @@ public class ObservingHandler : DelegatingHandler
436462
{
437463
private readonly HttpContext _context;
438464
private readonly Func<Payload, Task> _publishMessageAsync;
439-
private readonly ATOptions _options;
465+
private ATOptions _options;
440466
private readonly string _project_id;
441467
private readonly string? _msg_id;
468+
public void SetOptions(ATOptions options)
469+
{
470+
_options = options;
471+
}
442472
public ObservingHandler(Func<Payload, Task> publishMessage, string project_id, HttpContext? httpContext = null, ATOptions? options = null) : base(new HttpClientHandler())
443473
{
444474
_context = httpContext ?? throw new ArgumentNullException(nameof(httpContext));

README.md

+65
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ Next, initialize APItoolkit in your application's entry point (e.g., `Program.cs
3838
using ApiToolkit.Net;
3939

4040
// Initialize the APItoolkit client
41+
builder.Services.AddTransient<ObservingHandler>();
42+
43+
// Register the custom API Toolkit Client Factory
44+
builder.Services.AddSingleton<IApiToolkitClientFactory, ApiToolkitClientFactory>();
45+
4146
var config = new Config
4247
{
4348
ApiKey = "{ENTER_YOUR_API_KEY_HERE}",
@@ -60,6 +65,66 @@ app.Use(async (context, next) =>
6065
# ...
6166
```
6267

68+
## Usage
69+
70+
You can now use the IApiToolKitClientFactory Interface to directly make your Http requests
71+
72+
```csharp
73+
public class MyService
74+
{
75+
private readonly IApiToolkitClientFactory _apiToolkitClientFactory;
76+
77+
public MyService(IApiToolkitClientFactory apiToolkitClientFactory)
78+
{
79+
_apiToolkitClientFactory = apiToolkitClientFactory;
80+
}
81+
82+
public async Task<string> GetPostAsync()
83+
{
84+
var options = new ATOptions
85+
{
86+
PathWildCard = "/posts/{id}",
87+
RedactHeaders = new[] { "User-Agent" },
88+
RedactRequestBody = new[] { "$.user.password" },
89+
RedactResponseBody = new[] { "$.user.data.email" }
90+
};
91+
92+
var client = _apiToolkitClientFactory.CreateClient(options);
93+
var response = await client.GetAsync("https://jsonplaceholder.typicode.com/posts/1");
94+
return await response.Content.ReadAsStringAsync();
95+
}
96+
}
97+
```
98+
99+
Traditional Middleware Setup
100+
If you prefer to set up the middleware traditionally, here's how you can initialize APItoolkit in your application's entry point (e.g., Program.cs):
101+
102+
```csharp
103+
using ApiToolkit.Net;
104+
105+
// Initialize the APItoolkit client
106+
var config = new Config
107+
{
108+
ApiKey = "{ENTER_YOUR_API_KEY_HERE}",
109+
Debug = false,
110+
Tags = new List<string> { "environment: production", "region: us-east-1" },
111+
ServiceVersion: "v2.0",
112+
};
113+
var client = await APIToolkit.NewClientAsync(config);
114+
115+
// Register the middleware to use the initialized client
116+
app.Use(async (context, next) =>
117+
{
118+
var apiToolkit = new APIToolkit(next, client);
119+
await apiToolkit.InvokeAsync(context);
120+
});
121+
122+
// app.UseEndpoint(..)
123+
// other middleware and logic
124+
// ...
125+
```
126+
127+
63128
> [!NOTE]
64129
>
65130
> - Please make sure the APItoolkit middleware is added before `UseEndpoint` and other middleware are initialized.

0 commit comments

Comments
 (0)