From aeaba0e75dd64c8029b888d0f7f5b15f84425f2f Mon Sep 17 00:00:00 2001 From: Jens Schulze Date: Wed, 4 Sep 2024 09:29:57 +0200 Subject: [PATCH] add http version middleware (#351) --- .../ClientFactory.cs | 18 ++++++++++---- .../ClientOptions.cs | 3 +++ .../DependencyInjectionSetup.cs | 6 +++-- .../Middlewares/VersionMiddleware.cs | 24 +++++++++++++++++++ 4 files changed, 45 insertions(+), 6 deletions(-) create mode 100644 commercetools.Sdk/commercetools.Base.Client/Middlewares/VersionMiddleware.cs diff --git a/commercetools.Sdk/commercetools.Base.Client/ClientFactory.cs b/commercetools.Sdk/commercetools.Base.Client/ClientFactory.cs index 89e26431b06..7b52ef32b57 100644 --- a/commercetools.Sdk/commercetools.Base.Client/ClientFactory.cs +++ b/commercetools.Sdk/commercetools.Base.Client/ClientFactory.cs @@ -17,26 +17,27 @@ public static IClient Create( ISerializerService serializerService, ITokenProvider tokenProvider, bool readResponseAsStream = false, - ICorrelationIdProvider correlationIdProvider = null) + ICorrelationIdProvider correlationIdProvider = null, + Version httpVersion = null) { Validator.ValidateObject(configuration, new ValidationContext(configuration), true); if (readResponseAsStream && serializerService is IStreamSerializerService streamSerializer) { return new StreamCtpClient( - CreateMiddlewareStack(clientName, configuration, factory, tokenProvider, true, correlationIdProvider), + CreateMiddlewareStack(clientName, configuration, factory, tokenProvider, true, correlationIdProvider, httpVersion), streamSerializer, clientName ); } return new CtpClient( - CreateMiddlewareStack(clientName, configuration, factory, tokenProvider, false, correlationIdProvider), + CreateMiddlewareStack(clientName, configuration, factory, tokenProvider, false, correlationIdProvider, httpVersion), serializerService, clientName ); } public static Middleware CreateMiddlewareStack(string clientName, IClientConfiguration configuration, - IHttpClientFactory factory, ITokenProvider tokenProvider, bool readResponseAsStream = false, ICorrelationIdProvider correlationIdProvider = null) + IHttpClientFactory factory, ITokenProvider tokenProvider, bool readResponseAsStream = false, ICorrelationIdProvider correlationIdProvider = null, Version httpVersion = null) { var httpClient = factory.CreateClient(clientName); httpClient.BaseAddress = new Uri(configuration.ApiBaseAddress); @@ -48,6 +49,10 @@ public static Middleware CreateMiddlewareStack(string clientName, IClientConfigu correlationIdProvider ?? new DefaultCorrelationIdProvider(configuration) ) }; + if (httpVersion != null) + { + handlers.Add(CreateVersionMiddleware(httpVersion)); + } var httpMiddleware = readResponseAsStream ? (DelegatingMiddleware)new StreamHttpMiddleware(httpClient) : new HttpMiddleware(httpClient); @@ -70,5 +75,10 @@ public static CorrelationIdMiddleware CreateCorrelationIdMiddleware( { return new CorrelationIdMiddleware(correlationIdProvider); } + + public static VersionMiddleware CreateVersionMiddleware(Version httpVersion) + { + return new VersionMiddleware(httpVersion); + } } } \ No newline at end of file diff --git a/commercetools.Sdk/commercetools.Base.Client/ClientOptions.cs b/commercetools.Sdk/commercetools.Base.Client/ClientOptions.cs index adf1914df06..892624aadb5 100644 --- a/commercetools.Sdk/commercetools.Base.Client/ClientOptions.cs +++ b/commercetools.Sdk/commercetools.Base.Client/ClientOptions.cs @@ -1,3 +1,4 @@ +using System; using System.Net; namespace commercetools.Base.Client @@ -8,5 +9,7 @@ public class ClientOptions DecompressionMethods.Deflate | DecompressionMethods.GZip; public bool ReadResponseAsStream { get; set; } = false; + + public Version UseHttpVersion { get; set; } = null; } } \ No newline at end of file diff --git a/commercetools.Sdk/commercetools.Base.Client/DependencyInjectionSetup.cs b/commercetools.Sdk/commercetools.Base.Client/DependencyInjectionSetup.cs index a693437485c..114cb3d91da 100644 --- a/commercetools.Sdk/commercetools.Base.Client/DependencyInjectionSetup.cs +++ b/commercetools.Sdk/commercetools.Base.Client/DependencyInjectionSetup.cs @@ -60,7 +60,8 @@ private static IDictionary UseMultipleClients(this I serializerFactory(serviceProvider), tokenProviderSupplier(clientName, configuration, serviceProvider), options.ReadResponseAsStream, - serviceProvider.GetService()); + serviceProvider.GetService(), + options.UseHttpVersion); client.Name = clientName; return client; }); @@ -86,7 +87,8 @@ private static IDictionary UseSingleClient(this ISer serializerFactory(serviceProvider), tokenProviderSupplier(clientName, configuration, serviceProvider), options.ReadResponseAsStream, - serviceProvider.GetService()); + serviceProvider.GetService(), + options.UseHttpVersion); client.Name = clientName; return client; diff --git a/commercetools.Sdk/commercetools.Base.Client/Middlewares/VersionMiddleware.cs b/commercetools.Sdk/commercetools.Base.Client/Middlewares/VersionMiddleware.cs new file mode 100644 index 00000000000..4d923126248 --- /dev/null +++ b/commercetools.Sdk/commercetools.Base.Client/Middlewares/VersionMiddleware.cs @@ -0,0 +1,24 @@ +using System; +using System.Net.Http; +using System.Threading; +using System.Threading.Tasks; + +namespace commercetools.Base.Client.Middlewares +{ + public class VersionMiddleware: DelegatingMiddleware + { + private readonly Version _httpVersion; + + public VersionMiddleware(Version httpVersion) + { + this._httpVersion = httpVersion; + } + + protected internal override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) + { + request.Version = _httpVersion; + return await base.SendAsync(request, cancellationToken).ConfigureAwait(false); + } + + } +} \ No newline at end of file