@@ -38,6 +38,11 @@ Next, initialize APItoolkit in your application's entry point (e.g., `Program.cs
38
38
using ApiToolkit .Net ;
39
39
40
40
// 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
+
41
46
var config = new Config
42
47
{
43
48
ApiKey = " {ENTER_YOUR_API_KEY_HERE}" ,
@@ -60,6 +65,66 @@ app.Use(async (context, next) =>
60
65
# ...
61
66
```
62
67
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
+
63
128
> [ !NOTE]
64
129
>
65
130
> - Please make sure the APItoolkit middleware is added before ` UseEndpoint ` and other middleware are initialized.
0 commit comments