Skip to content

Commit

Permalink
Using ClientCredentials as Default TokenProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
MicheleRezk committed Feb 22, 2021
1 parent ffed80f commit 68bffbb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,14 +25,16 @@ public static IHttpClientBuilder UseCommercetoolsImportApi(this IServiceCollecti
clientName
};
services.AddSingleton(c => ImportApiFactory.Create(c.GetService<IClient>()));
return services.UseCommercetoolsImportApi(configuration, clients, tokenProviderSupplier).Single().Value;
return services.UseCommercetoolsImportApi(configuration, clients, tokenProviderSupplier ?? CreateDefaultTokenProvider).Single().Value;
}

public static IDictionary<string, IHttpClientBuilder> UseCommercetoolsImportApi(this IServiceCollection services,
IConfiguration configuration, IList<string> clients, Func<string, IConfiguration , IServiceProvider, ITokenProvider> tokenProviderSupplier)
{
services.UseCommercetoolsImportApiSerialization();
return services.UseHttpApi(configuration, clients, serviceProvider => serviceProvider.GetService<SerializerService>(), tokenProviderSupplier);
return services.UseHttpApi(configuration, clients,
serviceProvider => serviceProvider.GetService<SerializerService>(),
tokenProviderSupplier ?? CreateDefaultTokenProvider);
}

public static void UseCommercetoolsImportApiSerialization(this IServiceCollection services)
Expand All @@ -40,5 +43,11 @@ public static void UseCommercetoolsImportApiSerialization(this IServiceCollectio
services.UseSerialization();
services.AddSingleton<SerializerService>();
}
public static ITokenProvider CreateDefaultTokenProvider(string clientName, IConfiguration configuration, IServiceProvider serviceProvider)
{
var httpClientFactory = serviceProvider.GetService<IHttpClientFactory>();
var clientConfiguration = configuration.GetSection(clientName).Get<ClientConfiguration>();
return TokenProviderFactory.CreateClientCredentialsTokenProvider(clientConfiguration, httpClientFactory);
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -24,15 +25,17 @@ public static IHttpClientBuilder UseCommercetoolsMLApi(this IServiceCollection s
clientName
};
services.AddSingleton(c => MLApiFactory.Create(c.GetService<IClient>()));
return services.UseCommercetoolsMLApi(configuration, clients, tokenProviderSupplier).Single().Value;
return services.UseCommercetoolsMLApi(configuration, clients, tokenProviderSupplier ?? CreateDefaultTokenProvider).Single().Value;
}

public static IDictionary<string, IHttpClientBuilder> UseCommercetoolsMLApi(this IServiceCollection services,
IConfiguration configuration, IList<string> clients,
Func<string, IConfiguration , IServiceProvider, ITokenProvider> tokenProviderSupplier)
{
services.UseCommercetoolsMLApiSerialization();
return services.UseHttpApi(configuration, clients, serviceProvider => serviceProvider.GetService<SerializerService>(), tokenProviderSupplier);
return services.UseHttpApi(configuration, clients,
serviceProvider => serviceProvider.GetService<SerializerService>(),
tokenProviderSupplier ?? CreateDefaultTokenProvider);
}

public static void UseCommercetoolsMLApiSerialization(this IServiceCollection services)
Expand All @@ -41,5 +44,12 @@ public static void UseCommercetoolsMLApiSerialization(this IServiceCollection se
services.UseSerialization();
services.AddSingleton<SerializerService>();
}

public static ITokenProvider CreateDefaultTokenProvider(string clientName, IConfiguration configuration, IServiceProvider serviceProvider)
{
var httpClientFactory = serviceProvider.GetService<IHttpClientFactory>();
var clientConfiguration = configuration.GetSection(clientName).Get<ClientConfiguration>();
return TokenProviderFactory.CreateClientCredentialsTokenProvider(clientConfiguration, httpClientFactory);
}
}
}

0 comments on commit 68bffbb

Please sign in to comment.