Skip to content

Commit cb17583

Browse files
Jabenclaude
andcommitted
Add optional configuration action to AddGotenbergSharpClient extension methods
- Added Action<GotenbergSharpClientOptions>? parameter to both overloads - Made GetOptions nullable, returns null if options not configured in DI - Falls back to empty GotenbergSharpClientOptions if not in DI - Allows callers to configure options programmatically after retrieval 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent b94741a commit cb17583

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

src/Gotenberg.Sharp.Api.Client/Extensions/TypedClientServiceCollectionExtensions.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public static class TypedClientServiceCollectionExtensions
3333
/// Configure options via appsettings.json or by calling services.Configure&lt;GotenbergSharpClientOptions&gt;().
3434
/// </summary>
3535
/// <param name="services">The service collection.</param>
36+
/// <param name="configureClientOptions">Optional function to configure client options after they are retrieved.</param>
3637
/// <returns>An IHttpClientBuilder for further configuration.</returns>
3738
/// <exception cref="ArgumentNullException">Thrown when services is null.</exception>
3839
/// <remarks>
@@ -41,7 +42,8 @@ public static class TypedClientServiceCollectionExtensions
4142
/// Options should be configured in the "GotenbergSharpClient" section of appsettings.json or programmatically.
4243
/// </remarks>
4344
public static IHttpClientBuilder AddGotenbergSharpClient(
44-
this IServiceCollection services)
45+
this IServiceCollection services,
46+
Action<GotenbergSharpClientOptions>? configureClientOptions = null)
4547
{
4648
if (services == null)
4749
{
@@ -50,7 +52,10 @@ public static IHttpClientBuilder AddGotenbergSharpClient(
5052

5153
return services.AddGotenbergSharpClient((sp, client) =>
5254
{
53-
var ops = GetOptions(sp);
55+
var ops = GetOptions(sp) ?? new GotenbergSharpClientOptions();
56+
57+
configureClientOptions?.Invoke(ops);
58+
5459
client.Timeout = ops.TimeOut;
5560
client.BaseAddress = ops.ServiceUrl;
5661
});
@@ -61,6 +66,7 @@ public static IHttpClientBuilder AddGotenbergSharpClient(
6166
/// </summary>
6267
/// <param name="services">The service collection.</param>
6368
/// <param name="configureClient">Action to configure the HttpClient instance.</param>
69+
/// <param name="configureClientOptions">Optional function to configure client options after they are retrieved.</param>
6470
/// <returns>An IHttpClientBuilder for further configuration.</returns>
6571
/// <exception cref="ArgumentNullException">Thrown when configureClient is null.</exception>
6672
/// <remarks>
@@ -69,7 +75,8 @@ public static IHttpClientBuilder AddGotenbergSharpClient(
6975
/// </remarks>
7076
public static IHttpClientBuilder AddGotenbergSharpClient(
7177
this IServiceCollection services,
72-
Action<IServiceProvider, HttpClient> configureClient)
78+
Action<IServiceProvider, HttpClient> configureClient,
79+
Action<GotenbergSharpClientOptions>? configureClientOptions = null)
7380
{
7481
if (configureClient == null)
7582
{
@@ -87,7 +94,9 @@ public static IHttpClientBuilder AddGotenbergSharpClient(
8794
}))
8895
.AddHttpMessageHandler(sp =>
8996
{
90-
var ops = GetOptions(sp);
97+
var ops = GetOptions(sp) ?? new GotenbergSharpClientOptions();
98+
99+
configureClientOptions?.Invoke(ops);
91100

92101
var hasUsername = !string.IsNullOrWhiteSpace(ops.BasicAuthUsername);
93102
var hasPassword = !string.IsNullOrWhiteSpace(ops.BasicAuthPassword);
@@ -114,8 +123,8 @@ public static IHttpClientBuilder AddGotenbergSharpClient(
114123
return builder;
115124
}
116125

117-
private static GotenbergSharpClientOptions GetOptions(IServiceProvider sp)
126+
private static GotenbergSharpClientOptions? GetOptions(IServiceProvider sp)
118127
{
119-
return sp.GetRequiredService<IOptions<GotenbergSharpClientOptions>>().Value;
128+
return sp.GetService<IOptions<GotenbergSharpClientOptions>>()?.Value;
120129
}
121130
}

0 commit comments

Comments
 (0)