Skip to content

Commit

Permalink
add http version middleware (#351)
Browse files Browse the repository at this point in the history
  • Loading branch information
jenschude authored Sep 4, 2024
1 parent b3a369b commit aeaba0e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 6 deletions.
18 changes: 14 additions & 4 deletions commercetools.Sdk/commercetools.Base.Client/ClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -70,5 +75,10 @@ public static CorrelationIdMiddleware CreateCorrelationIdMiddleware(
{
return new CorrelationIdMiddleware(correlationIdProvider);
}

public static VersionMiddleware CreateVersionMiddleware(Version httpVersion)
{
return new VersionMiddleware(httpVersion);
}
}
}
3 changes: 3 additions & 0 deletions commercetools.Sdk/commercetools.Base.Client/ClientOptions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Net;

namespace commercetools.Base.Client
Expand All @@ -8,5 +9,7 @@ public class ClientOptions
DecompressionMethods.Deflate | DecompressionMethods.GZip;

public bool ReadResponseAsStream { get; set; } = false;

public Version UseHttpVersion { get; set; } = null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ private static IDictionary<string, IHttpClientBuilder> UseMultipleClients(this I
serializerFactory(serviceProvider),
tokenProviderSupplier(clientName, configuration, serviceProvider),
options.ReadResponseAsStream,
serviceProvider.GetService<ICorrelationIdProvider>());
serviceProvider.GetService<ICorrelationIdProvider>(),
options.UseHttpVersion);
client.Name = clientName;
return client;
});
Expand All @@ -86,7 +87,8 @@ private static IDictionary<string, IHttpClientBuilder> UseSingleClient(this ISer
serializerFactory(serviceProvider),
tokenProviderSupplier(clientName, configuration, serviceProvider),
options.ReadResponseAsStream,
serviceProvider.GetService<ICorrelationIdProvider>());
serviceProvider.GetService<ICorrelationIdProvider>(),
options.UseHttpVersion);

client.Name = clientName;
return client;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
request.Version = _httpVersion;
return await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
}

}
}

0 comments on commit aeaba0e

Please sign in to comment.