Skip to content
This repository has been archived by the owner on Jul 9, 2024. It is now read-only.

UriReplacementHandler improvements #243

Merged
merged 5 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -65,4 +66,38 @@ public async Task Calls_Uri_ReplacementAsync()

mockReplacement.Verify(static x=> x.Replace(It.IsAny<Uri>()), Times.Once());
}

[Fact]
public async Task Calls_Uri_Replacement_From_Request_OptionsAsync()
{
var mockReplacement = new Mock<IUriReplacementHandlerOption>();
mockReplacement.Setup(static x => x.IsEnabled()).Returns(true);
mockReplacement.Setup(static x => x.Replace(It.IsAny<Uri>())).Returns(new Uri("http://changed"));

var handler = new UriReplacementHandler<IUriReplacementHandlerOption>()
{
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<Uri>()), Times.Once());
}

/// <summary>
/// Sets a <see cref="IRequestOption"/> in <see cref="HttpRequestMessage"/>.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="httpRequestMessage">The <see cref="HttpRequestMessage"/> representation of the request.</param>
/// <param name="option">The request option.</param>
private static void SetRequestOption<T>(HttpRequestMessage httpRequestMessage, T option) where T : IRequestOption
{
#if NET5_0_OR_GREATER
httpRequestMessage.Options.Set(new HttpRequestOptionsKey<T>(typeof(T).FullName!), option);
#else
httpRequestMessage.Properties.Add(typeof(T).FullName!, option);
#endif
}
}
2 changes: 2 additions & 0 deletions src/KiotaClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -36,6 +37,7 @@ public static IList<DelegatingHandler> CreateDefaultHandlers()
return new List<DelegatingHandler>
{
//add the default middlewares as they are ready
new UriReplacementHandler<UriReplacementHandlerOption>(),
new RetryHandler(),
new RedirectHandler(),
new ParametersNameDecodingHandler(),
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.Kiota.Http.HttpClientLibrary.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<PackageProjectUrl>https://aka.ms/kiota/docs</PackageProjectUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<Deterministic>true</Deterministic>
<VersionPrefix>1.3.11</VersionPrefix>
<VersionPrefix>1.3.12</VersionPrefix>
<VersionSuffix></VersionSuffix>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<!-- Enable this line once we go live to prevent breaking changes -->
Expand Down
14 changes: 11 additions & 3 deletions src/Middleware/UriReplacementHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,29 @@ namespace Microsoft.Kiota.Http.HttpClientLibrary.Middleware;
/// <typeparam name="TUriReplacementHandlerOption">A type with the rules used to perform a URI replacement.</typeparam>
public class UriReplacementHandler<TUriReplacementHandlerOption> : DelegatingHandler where TUriReplacementHandlerOption : IUriReplacementHandlerOption
{
private readonly TUriReplacementHandlerOption uriReplacement;
private readonly TUriReplacementHandlerOption? _uriReplacement;

/// <summary>
/// Creates a new UriReplacementHandler.
/// </summary>
/// <param name="uriReplacement">An object with the URI replacement rules.</param>
public UriReplacementHandler(TUriReplacementHandlerOption uriReplacement)
public UriReplacementHandler(TUriReplacementHandlerOption? uriReplacement = default)
{
this.uriReplacement = uriReplacement;
this._uriReplacement = uriReplacement;
}

/// <inheritdoc/>
protected override async Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
{
var uriReplacement = request.GetRequestOption<TUriReplacementHandlerOption>() ?? _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<ObservabilityOptions>() is { } obsOptions)
{
Expand Down
Loading