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

Add parameter to factory method for setting handler options #246

Merged
merged 9 commits into from
May 21, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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<RetryHandler>().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<HttpClient>(client);

client = KiotaClientFactory.Create(new List<DelegatingHandler>());
Expand Down
40 changes: 31 additions & 9 deletions src/KiotaClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,10 +22,11 @@ public static class KiotaClientFactory
/// Initializes the <see cref="HttpClient"/> with the default configuration and middlewares including a authentication middleware using the <see cref="IAuthenticationProvider"/> if provided.
/// </summary>
/// <param name="finalHandler">The final <see cref="HttpMessageHandler"/> in the http pipeline. Can be configured for proxies, auto-decompression and auto-redirects </param>
/// <param name="optionsForHandlers">A array of <see cref="IRequestOption"/> objects passed to the default handlers.</param>
/// <returns>The <see cref="HttpClient"/> with the default middlewares.</returns>
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();
}
Expand All @@ -47,17 +49,37 @@ public static HttpClient Create(IList<DelegatingHandler> handlers, HttpMessageHa
/// Creates a default set of middleware to be used by the <see cref="HttpClient"/>.
/// </summary>
/// <returns>A list of the default handlers used by the client.</returns>
public static IList<DelegatingHandler> CreateDefaultHandlers()
public static IList<DelegatingHandler> CreateDefaultHandlers(IRequestOption[]? optionsForHandlers = null)
baywet marked this conversation as resolved.
Show resolved Hide resolved
{
optionsForHandlers ??= [];

return new List<DelegatingHandler>
{
//add the default middlewares as they are ready, and add them to the list below as well
new UriReplacementHandler<UriReplacementHandlerOption>(),
new RetryHandler(),
new RedirectHandler(),
new ParametersNameDecodingHandler(),
new UserAgentHandler(),
new HeadersInspectionHandler(),

optionsForHandlers.OfType<UriReplacementHandlerOption>().FirstOrDefault() is UriReplacementHandlerOption uriReplacementOption
? new UriReplacementHandler<UriReplacementHandlerOption>(uriReplacementOption)
: new UriReplacementHandler<UriReplacementHandlerOption>(),

optionsForHandlers.OfType<RetryHandlerOption>().FirstOrDefault() is RetryHandlerOption retryHandlerOption
? new RetryHandler(retryHandlerOption)
: new RetryHandler(),

optionsForHandlers.OfType<RedirectHandlerOption>().FirstOrDefault() is RedirectHandlerOption redirectHandlerOption
? new RedirectHandler(redirectHandlerOption)
: new RedirectHandler(),

optionsForHandlers.OfType<ParametersNameDecodingOption>().FirstOrDefault() is ParametersNameDecodingOption parametersNameDecodingOption
? new ParametersNameDecodingHandler(parametersNameDecodingOption)
: new ParametersNameDecodingHandler(),

optionsForHandlers.OfType<UserAgentHandlerOption>().FirstOrDefault() is UserAgentHandlerOption userAgentHandlerOption
? new UserAgentHandler(userAgentHandlerOption)
: new UserAgentHandler(),

optionsForHandlers.OfType<HeadersInspectionHandlerOption>().FirstOrDefault() is HeadersInspectionHandlerOption headersInspectionHandlerOption
? new HeadersInspectionHandler(headersInspectionHandlerOption)
: new HeadersInspectionHandler(),
};
}

Expand Down
Loading