diff --git a/commercetools.Sdk/commercetools.Sdk.ImportApi/DependencyInjectionSetup.cs b/commercetools.Sdk/commercetools.Sdk.ImportApi/DependencyInjectionSetup.cs index 8fced5811af..00c1deace9a 100644 --- a/commercetools.Sdk/commercetools.Sdk.ImportApi/DependencyInjectionSetup.cs +++ b/commercetools.Sdk/commercetools.Sdk.ImportApi/DependencyInjectionSetup.cs @@ -2,6 +2,7 @@ using Microsoft.Extensions.DependencyInjection; using System.Collections.Generic; using System.Linq; +using System.Net.Http; using commercetools.Base.Client; using commercetools.Base.Client.Tokens; using commercetools.Base.Registration; @@ -24,14 +25,16 @@ public static IHttpClientBuilder UseCommercetoolsImportApi(this IServiceCollecti clientName }; services.AddSingleton(c => ImportApiFactory.Create(c.GetService())); - return services.UseCommercetoolsImportApi(configuration, clients, tokenProviderSupplier).Single().Value; + return services.UseCommercetoolsImportApi(configuration, clients, tokenProviderSupplier ?? CreateDefaultTokenProvider).Single().Value; } public static IDictionary UseCommercetoolsImportApi(this IServiceCollection services, IConfiguration configuration, IList clients, Func tokenProviderSupplier) { services.UseCommercetoolsImportApiSerialization(); - return services.UseHttpApi(configuration, clients, serviceProvider => serviceProvider.GetService(), tokenProviderSupplier); + return services.UseHttpApi(configuration, clients, + serviceProvider => serviceProvider.GetService(), + tokenProviderSupplier ?? CreateDefaultTokenProvider); } public static void UseCommercetoolsImportApiSerialization(this IServiceCollection services) @@ -40,5 +43,11 @@ public static void UseCommercetoolsImportApiSerialization(this IServiceCollectio services.UseSerialization(); services.AddSingleton(); } + public static ITokenProvider CreateDefaultTokenProvider(string clientName, IConfiguration configuration, IServiceProvider serviceProvider) + { + var httpClientFactory = serviceProvider.GetService(); + var clientConfiguration = configuration.GetSection(clientName).Get(); + return TokenProviderFactory.CreateClientCredentialsTokenProvider(clientConfiguration, httpClientFactory); + } } } \ No newline at end of file diff --git a/commercetools.Sdk/commercetools.Sdk.MLApi/DependencyInjectionSetup.cs b/commercetools.Sdk/commercetools.Sdk.MLApi/DependencyInjectionSetup.cs index 9e56f8cbd81..27223db074d 100644 --- a/commercetools.Sdk/commercetools.Sdk.MLApi/DependencyInjectionSetup.cs +++ b/commercetools.Sdk/commercetools.Sdk.MLApi/DependencyInjectionSetup.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Net.Http; using commercetools.Base.Client; using commercetools.Base.Client.Tokens; using commercetools.Base.Registration; @@ -24,7 +25,7 @@ public static IHttpClientBuilder UseCommercetoolsMLApi(this IServiceCollection s clientName }; services.AddSingleton(c => MLApiFactory.Create(c.GetService())); - return services.UseCommercetoolsMLApi(configuration, clients, tokenProviderSupplier).Single().Value; + return services.UseCommercetoolsMLApi(configuration, clients, tokenProviderSupplier ?? CreateDefaultTokenProvider).Single().Value; } public static IDictionary UseCommercetoolsMLApi(this IServiceCollection services, @@ -32,7 +33,9 @@ public static IDictionary UseCommercetoolsMLApi(this Func tokenProviderSupplier) { services.UseCommercetoolsMLApiSerialization(); - return services.UseHttpApi(configuration, clients, serviceProvider => serviceProvider.GetService(), tokenProviderSupplier); + return services.UseHttpApi(configuration, clients, + serviceProvider => serviceProvider.GetService(), + tokenProviderSupplier ?? CreateDefaultTokenProvider); } public static void UseCommercetoolsMLApiSerialization(this IServiceCollection services) @@ -41,5 +44,12 @@ public static void UseCommercetoolsMLApiSerialization(this IServiceCollection se services.UseSerialization(); services.AddSingleton(); } + + public static ITokenProvider CreateDefaultTokenProvider(string clientName, IConfiguration configuration, IServiceProvider serviceProvider) + { + var httpClientFactory = serviceProvider.GetService(); + var clientConfiguration = configuration.GetSection(clientName).Get(); + return TokenProviderFactory.CreateClientCredentialsTokenProvider(clientConfiguration, httpClientFactory); + } } } \ No newline at end of file