Skip to content

Commit

Permalink
feat: Add overload on AddAtcRestClient with a IContractSerializer
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkallesen committed Jul 14, 2024
1 parent 05afd47 commit 656980d
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions src/Atc.Rest.Client/Options/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ public static IServiceCollection AddAtcRestClient<TOptions>(
this IServiceCollection services,
string clientName,
TOptions options,
Action<IHttpClientBuilder>? httpClientBuilder = default)
Action<IHttpClientBuilder>? httpClientBuilder = default,
IContractSerializer? contractSerializer = null)
where TOptions : AtcRestClientOptions, new()
{
services.AddSingleton(options);
Expand All @@ -23,7 +24,14 @@ public static IServiceCollection AddAtcRestClient<TOptions>(

// Register utilities
services.AddSingleton<IHttpMessageFactory, HttpMessageFactory>();
services.AddSingleton<IContractSerializer, DefaultJsonContractSerializer>();
if (contractSerializer is null)
{
services.AddSingleton<IContractSerializer, DefaultJsonContractSerializer>();
}
else
{
services.AddSingleton(contractSerializer);
}

return services;
}
Expand All @@ -34,9 +42,10 @@ public static IServiceCollection AddAtcRestClient(
string clientName,
Uri baseAddress,
TimeSpan timeout,
Action<IHttpClientBuilder>? httpClientBuilder = default)
Action<IHttpClientBuilder>? httpClientBuilder = default,
IContractSerializer? contractSerializer = null)
{
var clientBuilder = services.AddHttpClient(clientName, (s, c) =>
var clientBuilder = services.AddHttpClient(clientName, (_, c) =>
{
c.BaseAddress = baseAddress;
c.Timeout = timeout;
Expand All @@ -46,7 +55,14 @@ public static IServiceCollection AddAtcRestClient(

// Register utilities
services.AddSingleton<IHttpMessageFactory, HttpMessageFactory>();
services.AddSingleton<IContractSerializer, DefaultJsonContractSerializer>();
if (contractSerializer is null)
{
services.AddSingleton<IContractSerializer, DefaultJsonContractSerializer>();
}
else
{
services.AddSingleton(contractSerializer);
}

return services;
}
Expand Down

0 comments on commit 656980d

Please sign in to comment.