From 22bf7c56b5d79910e663e2a5bb67877ff618cd59 Mon Sep 17 00:00:00 2001 From: Paul Schaeflein Date: Mon, 22 Apr 2024 16:02:00 -0500 Subject: [PATCH 1/7] Add parameter for options to CreateDefaultHandlers --- src/KiotaClientFactory.cs | 41 +++++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/src/KiotaClientFactory.cs b/src/KiotaClientFactory.cs index 6c7d1ec..43c54db 100644 --- a/src/KiotaClientFactory.cs +++ b/src/KiotaClientFactory.cs @@ -2,10 +2,11 @@ // Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information. // ------------------------------------------------------------------------------ -using System.Linq; using System.Collections.Generic; +using System.Linq; 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; @@ -32,17 +33,37 @@ public static HttpClient Create(HttpMessageHandler? finalHandler = null) /// 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 - 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(), }; } /// @@ -66,7 +87,7 @@ public static IList CreateDefaultHandlers() } } if(finalHandler != null) - handlers[handlers.Length-1].InnerHandler = finalHandler; + handlers[handlers.Length - 1].InnerHandler = finalHandler; return handlers[0];//first } /// @@ -76,7 +97,7 @@ public static IList CreateDefaultHandlers() /// The created . public static DelegatingHandler? ChainHandlersCollectionAndGetFirstLink(params DelegatingHandler[] handlers) { - return ChainHandlersCollectionAndGetFirstLink(null,handlers); + return ChainHandlersCollectionAndGetFirstLink(null, handlers); } /// /// Gets a default Http Client handler with the appropriate proxy configurations From 0d449418588975db1e2fdf0f8457788b4aa280be Mon Sep 17 00:00:00 2001 From: Paul Schaeflein Date: Mon, 22 Apr 2024 16:04:32 -0500 Subject: [PATCH 2/7] Fix whitespace --- src/KiotaClientFactory.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/KiotaClientFactory.cs b/src/KiotaClientFactory.cs index 43c54db..8d6cba2 100644 --- a/src/KiotaClientFactory.cs +++ b/src/KiotaClientFactory.cs @@ -2,8 +2,8 @@ // Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information. // ------------------------------------------------------------------------------ -using System.Collections.Generic; using System.Linq; +using System.Collections.Generic; using System.Net; using System.Net.Http; using Microsoft.Kiota.Abstractions; @@ -87,7 +87,7 @@ public static IList CreateDefaultHandlers(IRequestOption[]? o } } if(finalHandler != null) - handlers[handlers.Length - 1].InnerHandler = finalHandler; + handlers[handlers.Length-1].InnerHandler = finalHandler; return handlers[0];//first } /// @@ -97,7 +97,7 @@ public static IList CreateDefaultHandlers(IRequestOption[]? o /// The created . public static DelegatingHandler? ChainHandlersCollectionAndGetFirstLink(params DelegatingHandler[] handlers) { - return ChainHandlersCollectionAndGetFirstLink(null, handlers); + return ChainHandlersCollectionAndGetFirstLink(null,handlers); } /// /// Gets a default Http Client handler with the appropriate proxy configurations From 56cb6c963c460637b115aaf52df540f80eed62b7 Mon Sep 17 00:00:00 2001 From: Paul Schaeflein Date: Tue, 23 Apr 2024 17:15:19 -0500 Subject: [PATCH 3/7] Add handler options to create method; Add tests --- .../KiotaClientFactoryTests.cs | 31 ++++++++++++++++++- src/KiotaClientFactory.cs | 5 +-- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/Microsoft.Kiota.Http.HttpClientLibrary.Tests/KiotaClientFactoryTests.cs b/Microsoft.Kiota.Http.HttpClientLibrary.Tests/KiotaClientFactoryTests.cs index c70b786..0849932 100644 --- a/Microsoft.Kiota.Http.HttpClientLibrary.Tests/KiotaClientFactoryTests.cs +++ b/Microsoft.Kiota.Http.HttpClientLibrary.Tests/KiotaClientFactoryTests.cs @@ -1,5 +1,9 @@ -using System.Net; +using System.Linq; +using System.Net; using System.Net.Http; +using Castle.Components.DictionaryAdapter.Xml; +using Microsoft.Kiota.Http.HttpClientLibrary.Middleware; +using Microsoft.Kiota.Http.HttpClientLibrary.Middleware.Options; using Microsoft.Kiota.Http.HttpClientLibrary.Tests.Mocks; using Xunit; @@ -82,5 +86,30 @@ public void GetDefaultHttpMessageHandlerSetsUpProxy() #endif } + + [Fact] + public void CreateDefaultHandlersWithOptions() + { + // Arrange + var retryHandlerOption = new RetryHandlerOption { MaxRetry = 5, ShouldRetry = (_, _, _) => true }; + + + // Act + var handlers = KiotaClientFactory.CreateDefaultHandlers(new[] { retryHandlerOption }); + + RetryHandler retryHandler = default; + foreach(var handler in handlers) + { + if(handler is RetryHandler retryFromDefault) + { + retryHandler = retryFromDefault; + break; + } + } + + // Assert + Assert.NotNull(retryHandler); + Assert.NotNull(retryHandler.RetryOption); + } } } diff --git a/src/KiotaClientFactory.cs b/src/KiotaClientFactory.cs index 8d6cba2..16928a4 100644 --- a/src/KiotaClientFactory.cs +++ b/src/KiotaClientFactory.cs @@ -22,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(); } From 54fe68aa876cf551378c9d3d608296da99d18191 Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Wed, 24 Apr 2024 08:58:05 -0400 Subject: [PATCH 4/7] - fixes ambiguous call --- .../KiotaClientFactoryTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Microsoft.Kiota.Http.HttpClientLibrary.Tests/KiotaClientFactoryTests.cs b/Microsoft.Kiota.Http.HttpClientLibrary.Tests/KiotaClientFactoryTests.cs index 0d68414..0755d1a 100644 --- a/Microsoft.Kiota.Http.HttpClientLibrary.Tests/KiotaClientFactoryTests.cs +++ b/Microsoft.Kiota.Http.HttpClientLibrary.Tests/KiotaClientFactoryTests.cs @@ -117,7 +117,7 @@ public void CreateDefaultHandlersWithOptions() [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()); From 79c4b4ae69efdd2cfdd0178c928055bed718f3d7 Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Tue, 21 May 2024 15:35:35 -0400 Subject: [PATCH 5/7] - merge fix --- src/KiotaClientFactory.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/KiotaClientFactory.cs b/src/KiotaClientFactory.cs index 244727b..4e4ca05 100644 --- a/src/KiotaClientFactory.cs +++ b/src/KiotaClientFactory.cs @@ -56,7 +56,6 @@ public static IList CreateDefaultHandlers(IRequestOption[]? o return new List { //add the default middlewares as they are ready, and add them to the list below as well - //add the default middlewares as they are ready optionsForHandlers.OfType().FirstOrDefault() is UriReplacementHandlerOption uriReplacementOption ? new UriReplacementHandler(uriReplacementOption) From 097bb96f3c900386ef0455ab6641a1037878ac9a Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Tue, 21 May 2024 15:37:32 -0400 Subject: [PATCH 6/7] - adds changelog entry for option parameter --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) 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 From 9d3fffd26eb57ddea4d0c29e2043de8d9583bc67 Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Tue, 21 May 2024 15:41:58 -0400 Subject: [PATCH 7/7] chore: code linting Signed-off-by: Vincent Biret --- .../KiotaClientFactoryTests.cs | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/Microsoft.Kiota.Http.HttpClientLibrary.Tests/KiotaClientFactoryTests.cs b/Microsoft.Kiota.Http.HttpClientLibrary.Tests/KiotaClientFactoryTests.cs index cead02d..5459348 100644 --- a/Microsoft.Kiota.Http.HttpClientLibrary.Tests/KiotaClientFactoryTests.cs +++ b/Microsoft.Kiota.Http.HttpClientLibrary.Tests/KiotaClientFactoryTests.cs @@ -1,9 +1,7 @@ -using System.Collections; -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; -using Castle.Components.DictionaryAdapter.Xml; using Microsoft.Kiota.Http.HttpClientLibrary.Middleware; using Microsoft.Kiota.Http.HttpClientLibrary.Middleware.Options; using Microsoft.Kiota.Http.HttpClientLibrary.Tests.Mocks; @@ -112,21 +110,12 @@ public void CreateDefaultHandlersWithOptions() // Act - var handlers = KiotaClientFactory.CreateDefaultHandlers(new[] { retryHandlerOption }); - - RetryHandler retryHandler = default; - foreach(var handler in handlers) - { - if(handler is RetryHandler retryFromDefault) - { - retryHandler = retryFromDefault; - break; - } - } + var handlers = KiotaClientFactory.CreateDefaultHandlers([retryHandlerOption]); + var retryHandler = handlers.OfType().FirstOrDefault(); // Assert Assert.NotNull(retryHandler); - Assert.NotNull(retryHandler.RetryOption); + Assert.Equal(retryHandlerOption, retryHandler.RetryOption); } [Fact]