private static IDictionary <string, IHttpClientBuilder> UseMultipleClients(this Container services, IConfiguration configuration, IDictionary <string, TokenFlow> clients) { var collection = new ServiceCollection(); services.UseHttpApiDefaults(collection); var builders = new Dictionary <string, IHttpClientBuilder>(); var clientBuilders = new List <Registration>(); foreach (KeyValuePair <string, TokenFlow> client in clients) { string clientName = client.Key; TokenFlow tokenFlow = client.Value; IClientConfiguration clientConfiguration = configuration.GetSection(clientName).Get <ClientConfiguration>(); Validator.ValidateObject(clientConfiguration, new ValidationContext(clientConfiguration), true); builders.Add(clientName, services.SetupClient(collection, clientName, clientConfiguration, tokenFlow)); clientBuilders.Add(Lifestyle.Singleton.CreateRegistration(() => new CtpClient(services.GetService <IHttpClientFactory>(), services.GetService <IHttpApiCommandFactory>(), services.GetService <ISerializerService>(), services.GetService <IUserAgentProvider>()) { Name = clientName }, services)); } services.RegisterCollection <IClient>(clientBuilders); services.Register <IHttpClientFactory>(() => collection.BuildServiceProvider().GetService <IHttpClientFactory>(), Lifestyle.Singleton); return(builders); }
private static void UseMultipleClients(this IServiceCollection services, IConfiguration configuration, IDictionary <string, TokenFlow> clients) { services.UseHttpApiDefaults(); TokenFlowMapper tokenFlowMapper = new TokenFlowMapper(); foreach (KeyValuePair <string, TokenFlow> client in clients) { string clientName = client.Key; TokenFlow tokenFlow = client.Value; var clientConfigurationToRemove = services.FirstOrDefault(descriptor => descriptor.ServiceType == typeof(IClientConfiguration)); services.Remove(clientConfigurationToRemove); var tokenFlowRegisterToRemove = services.FirstOrDefault(descriptor => descriptor.ServiceType == typeof(ITokenFlowRegister)); services.Remove(tokenFlowRegisterToRemove); services.SetupClient(configuration, clientName, tokenFlow); services.AddClient(clientName, tokenFlowMapper); } services.AddSingleton <ITokenFlowMapper>(tokenFlowMapper); }
private static IDictionary <string, IHttpClientBuilder> UseMultipleClients(this IServiceCollection services, IConfiguration configuration, IDictionary <string, TokenFlow> clients) { services.UseHttpApiDefaults(); var builders = new ConcurrentDictionary <string, IHttpClientBuilder>(); foreach (KeyValuePair <string, TokenFlow> client in clients) { string clientName = client.Key; TokenFlow tokenFlow = client.Value; IClientConfiguration clientConfiguration = configuration.GetSection(clientName).Get <ClientConfiguration>(); Validator.ValidateObject(clientConfiguration, new ValidationContext(clientConfiguration), true); builders.TryAdd(clientName, services.SetupClient(clientName, clientConfiguration, tokenFlow)); services.AddSingleton <IClient>(c => new CtpClient(c.GetService <IHttpClientFactory>(), c.GetService <IHttpApiCommandFactory>(), c.GetService <ISerializerService>(), c.GetService <IUserAgentProvider>()) { Name = clientName }); } return(builders); }
/// <summary> /// Adds concrete implementations necessary for running of the application to the service collection for a single client. /// </summary> /// <param name="services">The service collection.</param> /// <param name="configuration">The configuration.</param> /// <param name="clientName">The name of the client.</param> /// <param name="tokenFlow">The token flow.</param> public static IHttpClientBuilder UseCommercetools(this Container services, IConfiguration configuration, string clientName = DefaultClientNames.Api, TokenFlow tokenFlow = TokenFlow.ClientCredentials) { return(services.UseCommercetools(configuration, new Dictionary <string, TokenFlow>() { { clientName, tokenFlow } }).Single().Value); }
private static IHttpClientBuilder SetupClient(this Container services, IServiceCollection collection, string clientName, IClientConfiguration clientConfiguration, TokenFlow tokenFlow) { var httpClientBuilder = collection.AddHttpClient(clientName) .ConfigureHttpClient(client => client.BaseAddress = new Uri(clientConfiguration.ApiBaseAddress + clientConfiguration.ProjectKey + "/")) .AddHttpMessageHandler(c => { var providers = services.GetAllInstances <ITokenProvider>(); var provider = providers.FirstOrDefault(tokenProvider => tokenProvider.TokenFlow == tokenFlow); provider.ClientConfiguration = clientConfiguration; return(new AuthorizationHandler(provider)); }) .AddHttpMessageHandler(c => new CorrelationIdHandler(new DefaultCorrelationIdProvider(clientConfiguration))) .AddHttpMessageHandler(c => new ErrorHandler(new ApiExceptionFactory(clientConfiguration, services.GetService <ISerializerService>()))) .AddHttpMessageHandler(c => new LoggerHandler(services.GetService <ILoggerFactory>())); return(httpClientBuilder); }
private static IDictionary <string, IHttpClientBuilder> UseSingleClient(this Container services, IConfiguration configuration, string clientName, TokenFlow tokenFlow) { var collection = new ServiceCollection(); services.UseHttpApiDefaults(collection); services.Register <IClient>(() => new CtpClient(services.GetService <IHttpClientFactory>(), services.GetService <IHttpApiCommandFactory>(), services.GetService <ISerializerService>(), services.GetService <IUserAgentProvider>()) { Name = clientName }); var configurationSection = configuration.GetSection(clientName); IClientConfiguration clientConfiguration = configurationSection.Get <ClientConfiguration>(); Validator.ValidateObject(clientConfiguration, new ValidationContext(clientConfiguration), true); var builders = new Dictionary <string, IHttpClientBuilder> { { clientName, services.SetupClient(collection, clientName, clientConfiguration, tokenFlow) } }; services.Register <IHttpClientFactory>(() => collection.BuildServiceProvider().GetService <IHttpClientFactory>(), Lifestyle.Singleton); return(builders); }
/// <summary> /// Adds concrete implementations necessary for running of the application to the service collection for a single client. /// </summary> /// <param name="services">The service collection.</param> /// <param name="configuration">The configuration.</param> /// <param name="clientName">The name of the client.</param> /// <param name="tokenFlow">The token flow.</param> public static void UseCommercetools(this IServiceCollection services, IConfiguration configuration, string clientName = DefaultClientNames.Api, TokenFlow tokenFlow = TokenFlow.ClientCredentials) { services.UseCommercetools(configuration, new Dictionary <string, TokenFlow>() { { clientName, tokenFlow } }); }
/// <summary> /// Adds concrete implementations necessary for running of the application to the service collection for a single client. /// </summary> /// <param name="services">The service collection.</param> /// <param name="configuration">The configuration.</param> /// <param name="clientName">The name of the client.</param> /// <param name="tokenFlow">The token flow.</param> /// <param name="serializationConfiguration">The configuration of serialization services</param> public static IHttpClientBuilder UseCommercetools(this IServiceCollection services, IConfiguration configuration, string clientName = DefaultClientNames.Api, TokenFlow tokenFlow = TokenFlow.ClientCredentials, SerializationConfiguration serializationConfiguration = null) { var clients = new ConcurrentDictionary <string, TokenFlow>(); clients.TryAdd(clientName, tokenFlow); return(services.UseCommercetools(configuration, clients, serializationConfiguration).Single().Value); }
private static void SetupClient(this IServiceCollection services, IConfiguration configuration, string clientName, TokenFlow tokenFlow) { IClientConfiguration clientConfiguration = configuration.GetSection(clientName).Get <ClientConfiguration>(); services.AddSingleton(clientConfiguration); ITokenFlowRegister tokenFlowRegister = new InMemoryTokenFlowRegister(); tokenFlowRegister.TokenFlow = tokenFlow; services.AddSingleton(tokenFlowRegister); IHttpClientBuilder httpClientBuilder = services.AddHttpClient(clientName) .AddHttpMessageHandler <AuthorizationHandler>().AddHttpMessageHandler <CorrelationIdHandler>() .AddHttpMessageHandler <LoggerHandler>() .AddHttpMessageHandler <ErrorHandler>(); }
private static void UseSingleClient(this IServiceCollection services, IConfiguration configuration, string clientName, TokenFlow tokenFlow) { services.UseHttpApiDefaults(); services.SetupClient(configuration, clientName, tokenFlow); services.AddSingleton <IClient>(c => new Client(c.GetService <IHttpClientFactory>(), c.GetService <IHttpApiCommandFactory>(), c.GetService <ISerializerService>()) { Name = clientName }); }
private static IHttpClientBuilder SetupClient(this IServiceCollection services, string clientName, IClientConfiguration clientConfiguration, TokenFlow tokenFlow) { var httpClientBuilder = services.AddHttpClient(clientName) .ConfigureHttpClient((provider, client) => { client.BaseAddress = new Uri(clientConfiguration.ApiBaseAddress + clientConfiguration.ProjectKey + "/"); client.DefaultRequestHeaders.AcceptEncoding.ParseAdd("gzip"); client.DefaultRequestHeaders.AcceptEncoding.ParseAdd("deflate"); client.DefaultRequestHeaders.UserAgent.ParseAdd(provider.GetService <IUserAgentProvider>().UserAgent); }) .ConfigureHttpMessageHandlerBuilder(builder => { builder.PrimaryHandler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip }; }) .AddHttpMessageHandler(c => { var providers = c.GetServices <ITokenProvider>(); var provider = providers.FirstOrDefault(tokenProvider => tokenProvider.TokenFlow == tokenFlow); provider.ClientConfiguration = clientConfiguration; return(new AuthorizationHandler(provider)); }) .AddHttpMessageHandler(c => { var correlationIdProvider = c.GetServices <ICorrelationIdProvider>().FirstOrDefault(); correlationIdProvider.ClientConfiguration = clientConfiguration; return(new CorrelationIdHandler(correlationIdProvider)); }) .AddHttpMessageHandler(c => new ErrorHandler(new ApiExceptionFactory(clientConfiguration, c.GetService <ISerializerService>()))) .AddHttpMessageHandler <LoggerHandler>(); return(httpClientBuilder); }
public ITokenProvider GetTokenProviderByFlow(TokenFlow tokenFlow) { return(this.tokenProviders.FirstOrDefault(x => x.TokenFlow == tokenFlow)); }
private static IDictionary <string, IHttpClientBuilder> UseSingleClient(this IServiceCollection services, IConfiguration configuration, string clientName, TokenFlow tokenFlow) { IClientConfiguration clientConfiguration = configuration.GetSection(clientName).Get <ClientConfiguration>(); Validator.ValidateObject(clientConfiguration, new ValidationContext(clientConfiguration), true); services.UseHttpApiDefaults(); services.AddSingleton <IClient>(c => new CtpClient(c.GetService <IHttpClientFactory>(), c.GetService <IHttpApiCommandFactory>(), c.GetService <ISerializerService>(), c.GetService <IUserAgentProvider>()) { Name = clientName }); var builders = new Dictionary <string, IHttpClientBuilder>(); builders.Add(clientName, services.SetupClient(clientName, clientConfiguration, tokenFlow)); return(builders); }