From b29591981c8329679f366bfe3916599cf9b92463 Mon Sep 17 00:00:00 2001 From: NetherGranite Date: Sat, 20 Apr 2024 16:21:13 -0600 Subject: [PATCH 1/4] Make UriReplacementHandler's constructor option optional and make it respect request options --- src/Middleware/UriReplacementHandler.cs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/Middleware/UriReplacementHandler.cs b/src/Middleware/UriReplacementHandler.cs index daadd3b..c981b02 100644 --- a/src/Middleware/UriReplacementHandler.cs +++ b/src/Middleware/UriReplacementHandler.cs @@ -12,21 +12,29 @@ namespace Microsoft.Kiota.Http.HttpClientLibrary.Middleware; /// A type with the rules used to perform a URI replacement. public class UriReplacementHandler : DelegatingHandler where TUriReplacementHandlerOption : IUriReplacementHandlerOption { - private readonly TUriReplacementHandlerOption uriReplacement; + private readonly TUriReplacementHandlerOption? _uriReplacement; /// /// Creates a new UriReplacementHandler. /// /// An object with the URI replacement rules. - public UriReplacementHandler(TUriReplacementHandlerOption uriReplacement) + public UriReplacementHandler(TUriReplacementHandlerOption? uriReplacement = default) { - this.uriReplacement = uriReplacement; + this._uriReplacement = uriReplacement; } /// protected override async Task SendAsync( HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) { + var uriReplacement = request.GetRequestOption() ?? _uriReplacement; + + // If there is no URI replacement to apply, then just skip this handler. + if(uriReplacement is null) + { + return await base.SendAsync(request, cancellationToken).ConfigureAwait(false); + } + Activity? activity; if(request.GetRequestOption() is { } obsOptions) { From 8a032df0d3f220442451a59b96f672e32eff95b6 Mon Sep 17 00:00:00 2001 From: NetherGranite Date: Sat, 20 Apr 2024 16:22:10 -0600 Subject: [PATCH 2/4] Add a test to ensure UriReplacementHandler respects request options --- .../Middleware/UriReplacementHandlerTests.cs | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/Microsoft.Kiota.Http.HttpClientLibrary.Tests/Middleware/UriReplacementHandlerTests.cs b/Microsoft.Kiota.Http.HttpClientLibrary.Tests/Middleware/UriReplacementHandlerTests.cs index 02df8ce..293edf4 100644 --- a/Microsoft.Kiota.Http.HttpClientLibrary.Tests/Middleware/UriReplacementHandlerTests.cs +++ b/Microsoft.Kiota.Http.HttpClientLibrary.Tests/Middleware/UriReplacementHandlerTests.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Net.Http; using System.Threading.Tasks; +using Microsoft.Kiota.Abstractions; using Microsoft.Kiota.Http.HttpClientLibrary.Middleware; using Microsoft.Kiota.Http.HttpClientLibrary.Middleware.Options; using Moq; @@ -65,4 +66,38 @@ public async Task Calls_Uri_ReplacementAsync() mockReplacement.Verify(static x=> x.Replace(It.IsAny()), Times.Once()); } + + [Fact] + public async Task Calls_Uri_Replacement_From_Request_OptionsAsync() + { + var mockReplacement = new Mock(); + mockReplacement.Setup(static x => x.IsEnabled()).Returns(true); + mockReplacement.Setup(static x => x.Replace(It.IsAny())).Returns(new Uri("http://changed")); + + var handler = new UriReplacementHandler() + { + InnerHandler = new FakeSuccessHandler() + }; + var msg = new HttpRequestMessage(HttpMethod.Get, "http://localhost"); + SetRequestOption(msg, mockReplacement.Object); + var client = new HttpClient(handler); + await client.SendAsync(msg); + + mockReplacement.Verify(static x=> x.Replace(It.IsAny()), Times.Once()); + } + + /// + /// Sets a in . + /// + /// + /// The representation of the request. + /// The request option. + private static void SetRequestOption(HttpRequestMessage httpRequestMessage, T option) where T : IRequestOption + { +#if NET5_0_OR_GREATER + httpRequestMessage.Options.Set(new HttpRequestOptionsKey(typeof(T).FullName!), option); +#else + httpRequestMessage.Properties.Add(typeof(T).FullName!, option); +#endif + } } From 831120ce7fb7256ae76e00d3b797d28a2a6047c0 Mon Sep 17 00:00:00 2001 From: NetherGranite Date: Sat, 20 Apr 2024 16:23:08 -0600 Subject: [PATCH 3/4] Add UriReplacementHandler as a default handler so it works out of the box --- src/KiotaClientFactory.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/KiotaClientFactory.cs b/src/KiotaClientFactory.cs index 5dd9498..6c7d1ec 100644 --- a/src/KiotaClientFactory.cs +++ b/src/KiotaClientFactory.cs @@ -8,6 +8,7 @@ using System.Net.Http; using Microsoft.Kiota.Abstractions.Authentication; using Microsoft.Kiota.Http.HttpClientLibrary.Middleware; +using Microsoft.Kiota.Http.HttpClientLibrary.Middleware.Options; namespace Microsoft.Kiota.Http.HttpClientLibrary { @@ -36,6 +37,7 @@ public static IList CreateDefaultHandlers() return new List { //add the default middlewares as they are ready + new UriReplacementHandler(), new RetryHandler(), new RedirectHandler(), new ParametersNameDecodingHandler(), From 6f83ec0ae6e88a9336c104bf2e78ca8f438f877d Mon Sep 17 00:00:00 2001 From: Andrew Omondi Date: Mon, 22 Apr 2024 09:41:33 +0300 Subject: [PATCH 4/4] Bumps version and release notes --- CHANGELOG.md | 3 +++ src/Microsoft.Kiota.Http.HttpClientLibrary.csproj | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 907525b..d462a63 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [1.3.12] - 2024-04-22 + +- UriReplacementHandler improvements to be added to middleware pipeline by default and respects options set in the HttpRequestMessage (https://github.com/microsoft/kiota-http-dotnet/issues/242) - Adds `ConfigureAwait(false)` calls to async calls (https://github.com/microsoft/kiota-http-dotnet/issues/240). ## [1.3.11] - 2024-04-19 diff --git a/src/Microsoft.Kiota.Http.HttpClientLibrary.csproj b/src/Microsoft.Kiota.Http.HttpClientLibrary.csproj index 99406a3..c36fcbc 100644 --- a/src/Microsoft.Kiota.Http.HttpClientLibrary.csproj +++ b/src/Microsoft.Kiota.Http.HttpClientLibrary.csproj @@ -15,7 +15,7 @@ https://aka.ms/kiota/docs true true - 1.3.11 + 1.3.12 true