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

Commit 0fcb638

Browse files
authored
Merge pull request #246 from pschaeflein/options-in-factory
Add parameter to factory method for setting handler options
2 parents 3464453 + 9d3fffd commit 0fcb638

File tree

3 files changed

+51
-12
lines changed

3 files changed

+51
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111

12+
- Added an optional parameter to kiota middleware factory so options can be configured directly. [#233](https://github.com/microsoft/kiota-http-dotnet/issues/233)
1213
- `GetDefaultHandlerTypes` added to `KiotaClientFactory` if you're creating your own `HttpClient` and still want to use the default handlers.
1314

1415
## [1.4.1] - 2024-05-07

Microsoft.Kiota.Http.HttpClientLibrary.Tests/KiotaClientFactoryTests.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
using System.Collections;
2-
using System.Collections.Generic;
1+
using System.Collections.Generic;
32
using System.Linq;
43
using System.Net;
54
using System.Net.Http;
65
using Microsoft.Kiota.Http.HttpClientLibrary.Middleware;
6+
using Microsoft.Kiota.Http.HttpClientLibrary.Middleware.Options;
77
using Microsoft.Kiota.Http.HttpClientLibrary.Tests.Mocks;
88
using Xunit;
99

@@ -102,10 +102,26 @@ public void GetDefaultHttpMessageHandlerSetsUpProxy()
102102
#endif
103103
}
104104

105+
[Fact]
106+
public void CreateDefaultHandlersWithOptions()
107+
{
108+
// Arrange
109+
var retryHandlerOption = new RetryHandlerOption { MaxRetry = 5, ShouldRetry = (_, _, _) => true };
110+
111+
112+
// Act
113+
var handlers = KiotaClientFactory.CreateDefaultHandlers([retryHandlerOption]);
114+
var retryHandler = handlers.OfType<RetryHandler>().FirstOrDefault();
115+
116+
// Assert
117+
Assert.NotNull(retryHandler);
118+
Assert.Equal(retryHandlerOption, retryHandler.RetryOption);
119+
}
120+
105121
[Fact]
106122
public void CreateWithNullOrEmptyHandlersReturnsHttpClient()
107123
{
108-
var client = KiotaClientFactory.Create(null, null);
124+
var client = KiotaClientFactory.Create(null, (HttpMessageHandler)null);
109125
Assert.IsType<HttpClient>(client);
110126

111127
client = KiotaClientFactory.Create(new List<DelegatingHandler>());

src/KiotaClientFactory.cs

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Collections.Generic;
77
using System.Net;
88
using System.Net.Http;
9+
using Microsoft.Kiota.Abstractions;
910
using Microsoft.Kiota.Abstractions.Authentication;
1011
using Microsoft.Kiota.Http.HttpClientLibrary.Middleware;
1112
using Microsoft.Kiota.Http.HttpClientLibrary.Middleware.Options;
@@ -21,10 +22,11 @@ public static class KiotaClientFactory
2122
/// Initializes the <see cref="HttpClient"/> with the default configuration and middlewares including a authentication middleware using the <see cref="IAuthenticationProvider"/> if provided.
2223
/// </summary>
2324
/// <param name="finalHandler">The final <see cref="HttpMessageHandler"/> in the http pipeline. Can be configured for proxies, auto-decompression and auto-redirects </param>
25+
/// <param name="optionsForHandlers">A array of <see cref="IRequestOption"/> objects passed to the default handlers.</param>
2426
/// <returns>The <see cref="HttpClient"/> with the default middlewares.</returns>
25-
public static HttpClient Create(HttpMessageHandler? finalHandler = null)
27+
public static HttpClient Create(HttpMessageHandler? finalHandler = null, IRequestOption[]? optionsForHandlers = null)
2628
{
27-
var defaultHandlers = CreateDefaultHandlers();
29+
var defaultHandlers = CreateDefaultHandlers(optionsForHandlers);
2830
var handler = ChainHandlersCollectionAndGetFirstLink(finalHandler ?? GetDefaultHttpMessageHandler(), defaultHandlers.ToArray());
2931
return handler != null ? new HttpClient(handler) : new HttpClient();
3032
}
@@ -47,17 +49,37 @@ public static HttpClient Create(IList<DelegatingHandler> handlers, HttpMessageHa
4749
/// Creates a default set of middleware to be used by the <see cref="HttpClient"/>.
4850
/// </summary>
4951
/// <returns>A list of the default handlers used by the client.</returns>
50-
public static IList<DelegatingHandler> CreateDefaultHandlers()
52+
public static IList<DelegatingHandler> CreateDefaultHandlers(IRequestOption[]? optionsForHandlers = null)
5153
{
54+
optionsForHandlers ??= [];
55+
5256
return new List<DelegatingHandler>
5357
{
5458
//add the default middlewares as they are ready, and add them to the list below as well
55-
new UriReplacementHandler<UriReplacementHandlerOption>(),
56-
new RetryHandler(),
57-
new RedirectHandler(),
58-
new ParametersNameDecodingHandler(),
59-
new UserAgentHandler(),
60-
new HeadersInspectionHandler(),
59+
60+
optionsForHandlers.OfType<UriReplacementHandlerOption>().FirstOrDefault() is UriReplacementHandlerOption uriReplacementOption
61+
? new UriReplacementHandler<UriReplacementHandlerOption>(uriReplacementOption)
62+
: new UriReplacementHandler<UriReplacementHandlerOption>(),
63+
64+
optionsForHandlers.OfType<RetryHandlerOption>().FirstOrDefault() is RetryHandlerOption retryHandlerOption
65+
? new RetryHandler(retryHandlerOption)
66+
: new RetryHandler(),
67+
68+
optionsForHandlers.OfType<RedirectHandlerOption>().FirstOrDefault() is RedirectHandlerOption redirectHandlerOption
69+
? new RedirectHandler(redirectHandlerOption)
70+
: new RedirectHandler(),
71+
72+
optionsForHandlers.OfType<ParametersNameDecodingOption>().FirstOrDefault() is ParametersNameDecodingOption parametersNameDecodingOption
73+
? new ParametersNameDecodingHandler(parametersNameDecodingOption)
74+
: new ParametersNameDecodingHandler(),
75+
76+
optionsForHandlers.OfType<UserAgentHandlerOption>().FirstOrDefault() is UserAgentHandlerOption userAgentHandlerOption
77+
? new UserAgentHandler(userAgentHandlerOption)
78+
: new UserAgentHandler(),
79+
80+
optionsForHandlers.OfType<HeadersInspectionHandlerOption>().FirstOrDefault() is HeadersInspectionHandlerOption headersInspectionHandlerOption
81+
? new HeadersInspectionHandler(headersInspectionHandlerOption)
82+
: new HeadersInspectionHandler(),
6183
};
6284
}
6385

0 commit comments

Comments
 (0)