diff --git a/CHANGELOG.md b/CHANGELOG.md index 2b5048a..4fee787 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added +- Added an optional parameter to kiota middleware factory so options can be configured directly. [#233](https://github.com/microsoft/kiota-http-dotnet/issues/233) - `GetDefaultHandlerTypes` added to `KiotaClientFactory` if you're creating your own `HttpClient` and still want to use the default handlers. ## [1.4.1] - 2024-05-07 diff --git a/Microsoft.Kiota.Http.HttpClientLibrary.Tests/KiotaClientFactoryTests.cs b/Microsoft.Kiota.Http.HttpClientLibrary.Tests/KiotaClientFactoryTests.cs index 72c6812..5459348 100644 --- a/Microsoft.Kiota.Http.HttpClientLibrary.Tests/KiotaClientFactoryTests.cs +++ b/Microsoft.Kiota.Http.HttpClientLibrary.Tests/KiotaClientFactoryTests.cs @@ -1,9 +1,9 @@ -using System.Collections; -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using Microsoft.Kiota.Http.HttpClientLibrary.Middleware; +using Microsoft.Kiota.Http.HttpClientLibrary.Middleware.Options; using Microsoft.Kiota.Http.HttpClientLibrary.Tests.Mocks; using Xunit; @@ -102,10 +102,26 @@ public void GetDefaultHttpMessageHandlerSetsUpProxy() #endif } + [Fact] + public void CreateDefaultHandlersWithOptions() + { + // Arrange + var retryHandlerOption = new RetryHandlerOption { MaxRetry = 5, ShouldRetry = (_, _, _) => true }; + + + // Act + var handlers = KiotaClientFactory.CreateDefaultHandlers([retryHandlerOption]); + var retryHandler = handlers.OfType().FirstOrDefault(); + + // Assert + Assert.NotNull(retryHandler); + Assert.Equal(retryHandlerOption, retryHandler.RetryOption); + } + [Fact] public void CreateWithNullOrEmptyHandlersReturnsHttpClient() { - var client = KiotaClientFactory.Create(null, null); + var client = KiotaClientFactory.Create(null, (HttpMessageHandler)null); Assert.IsType(client); client = KiotaClientFactory.Create(new List()); diff --git a/src/KiotaClientFactory.cs b/src/KiotaClientFactory.cs index 96c1eb5..4e4ca05 100644 --- a/src/KiotaClientFactory.cs +++ b/src/KiotaClientFactory.cs @@ -6,6 +6,7 @@ using System.Collections.Generic; using System.Net; using System.Net.Http; +using Microsoft.Kiota.Abstractions; using Microsoft.Kiota.Abstractions.Authentication; using Microsoft.Kiota.Http.HttpClientLibrary.Middleware; using Microsoft.Kiota.Http.HttpClientLibrary.Middleware.Options; @@ -21,10 +22,11 @@ public static class KiotaClientFactory /// Initializes the with the default configuration and middlewares including a authentication middleware using the if provided. /// /// The final in the http pipeline. Can be configured for proxies, auto-decompression and auto-redirects + /// A array of objects passed to the default handlers. /// The with the default middlewares. - public static HttpClient Create(HttpMessageHandler? finalHandler = null) + public static HttpClient Create(HttpMessageHandler? finalHandler = null, IRequestOption[]? optionsForHandlers = null) { - var defaultHandlers = CreateDefaultHandlers(); + var defaultHandlers = CreateDefaultHandlers(optionsForHandlers); var handler = ChainHandlersCollectionAndGetFirstLink(finalHandler ?? GetDefaultHttpMessageHandler(), defaultHandlers.ToArray()); return handler != null ? new HttpClient(handler) : new HttpClient(); } @@ -47,17 +49,37 @@ public static HttpClient Create(IList handlers, HttpMessageHa /// Creates a default set of middleware to be used by the . /// /// A list of the default handlers used by the client. - public static IList CreateDefaultHandlers() + public static IList CreateDefaultHandlers(IRequestOption[]? optionsForHandlers = null) { + optionsForHandlers ??= []; + return new List { //add the default middlewares as they are ready, and add them to the list below as well - new UriReplacementHandler(), - new RetryHandler(), - new RedirectHandler(), - new ParametersNameDecodingHandler(), - new UserAgentHandler(), - new HeadersInspectionHandler(), + + optionsForHandlers.OfType().FirstOrDefault() is UriReplacementHandlerOption uriReplacementOption + ? new UriReplacementHandler(uriReplacementOption) + : new UriReplacementHandler(), + + optionsForHandlers.OfType().FirstOrDefault() is RetryHandlerOption retryHandlerOption + ? new RetryHandler(retryHandlerOption) + : new RetryHandler(), + + optionsForHandlers.OfType().FirstOrDefault() is RedirectHandlerOption redirectHandlerOption + ? new RedirectHandler(redirectHandlerOption) + : new RedirectHandler(), + + optionsForHandlers.OfType().FirstOrDefault() is ParametersNameDecodingOption parametersNameDecodingOption + ? new ParametersNameDecodingHandler(parametersNameDecodingOption) + : new ParametersNameDecodingHandler(), + + optionsForHandlers.OfType().FirstOrDefault() is UserAgentHandlerOption userAgentHandlerOption + ? new UserAgentHandler(userAgentHandlerOption) + : new UserAgentHandler(), + + optionsForHandlers.OfType().FirstOrDefault() is HeadersInspectionHandlerOption headersInspectionHandlerOption + ? new HeadersInspectionHandler(headersInspectionHandlerOption) + : new HeadersInspectionHandler(), }; }