Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access to just the types of the handlers #255

Merged
merged 3 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 16 additions & 12 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,22 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [1.4.1] - 2024-05-07

## Changed

- Use `SocketsHttpHandler` with `EnableMultipleHttp2Connections` as default HTTP message handler.

## [1.4.0]

## Added

- KiotaClientFactory `create()` overload that accepts a list of handlers.

### Added

- `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

## Changed

- Use `SocketsHttpHandler` with `EnableMultipleHttp2Connections` as default HTTP message handler.

## [1.4.0]

## Added

- KiotaClientFactory `create()` overload that accepts a list of handlers.

## [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)
Expand Down
55 changes: 37 additions & 18 deletions src/KiotaClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@ public static HttpClient Create(HttpMessageHandler? finalHandler = null)
var handler = ChainHandlersCollectionAndGetFirstLink(finalHandler ?? GetDefaultHttpMessageHandler(), defaultHandlers.ToArray());
return handler != null ? new HttpClient(handler) : new HttpClient();
}

/// <summary>
/// Initializes the <see cref="HttpClient"/> with a custom middleware pipeline.
/// </summary>
/// <param name="handlers">The <see cref="DelegatingHandler"/> instances to create the <see cref="DelegatingHandler"/> from.</param>
/// <param name="finalHandler">The final <see cref="HttpMessageHandler"/> in the http pipeline. Can be configured for proxies, auto-decompression and auto-redirects</param>
/// <returns>The <see cref="HttpClient"/> with the custom handlers.</returns>
public static HttpClient Create(IList<DelegatingHandler> handlers, HttpMessageHandler? finalHandler = null)
{
if(handlers == null || !handlers.Any())
return Create(finalHandler);
var handler = ChainHandlersCollectionAndGetFirstLink(finalHandler ?? GetDefaultHttpMessageHandler(), handlers.ToArray());
return handler != null ? new HttpClient(handler) : new HttpClient();
}

/// <summary>
/// Initializes the <see cref="HttpClient"/> with a custom middleware pipeline.
/// </summary>
/// <param name="handlers">The <see cref="DelegatingHandler"/> instances to create the <see cref="DelegatingHandler"/> from.</param>
/// <param name="finalHandler">The final <see cref="HttpMessageHandler"/> in the http pipeline. Can be configured for proxies, auto-decompression and auto-redirects</param>
/// <returns>The <see cref="HttpClient"/> with the custom handlers.</returns>
public static HttpClient Create(IList<DelegatingHandler> handlers, HttpMessageHandler? finalHandler = null)
{
if(handlers == null || !handlers.Any())
return Create(finalHandler);
var handler = ChainHandlersCollectionAndGetFirstLink(finalHandler ?? GetDefaultHttpMessageHandler(), handlers.ToArray());
return handler != null ? new HttpClient(handler) : new HttpClient();
}
/// <summary>
/// Creates a default set of middleware to be used by the <see cref="HttpClient"/>.
/// </summary>
Expand All @@ -51,7 +51,7 @@ public static IList<DelegatingHandler> CreateDefaultHandlers()
{
return new List<DelegatingHandler>
{
//add the default middlewares as they are ready
//add the default middlewares as they are ready, and add them to the list below as well
new UriReplacementHandler<UriReplacementHandlerOption>(),
new RetryHandler(),
new RedirectHandler(),
Expand All @@ -60,6 +60,25 @@ public static IList<DelegatingHandler> CreateDefaultHandlers()
new HeadersInspectionHandler(),
};
}

/// <summary>
/// Gets the default handler types.
/// </summary>
/// <returns>A list of all the default handlers</returns>
/// <remarks>Order matters</remarks>
public static IList<System.Type> GetDefaultHandlerTypes()
{
return new List<System.Type>
{
typeof(UriReplacementHandler<UriReplacementHandlerOption>),
typeof(RetryHandler),
typeof(RedirectHandler),
typeof(ParametersNameDecodingHandler),
typeof(UserAgentHandler),
typeof(HeadersInspectionHandler),
};
}

/// <summary>
/// Creates a <see cref="DelegatingHandler"/> to use for the <see cref="HttpClient" /> from the provided <see cref="DelegatingHandler"/> instances. Order matters.
/// </summary>
Expand All @@ -81,7 +100,7 @@ public static IList<DelegatingHandler> CreateDefaultHandlers()
}
}
if(finalHandler != null)
handlers[handlers.Length-1].InnerHandler = finalHandler;
handlers[handlers.Length - 1].InnerHandler = finalHandler;
return handlers[0];//first
}
/// <summary>
Expand All @@ -91,7 +110,7 @@ public static IList<DelegatingHandler> CreateDefaultHandlers()
/// <returns>The created <see cref="DelegatingHandler"/>.</returns>
public static DelegatingHandler? ChainHandlersCollectionAndGetFirstLink(params DelegatingHandler[] handlers)
{
return ChainHandlersCollectionAndGetFirstLink(null,handlers);
return ChainHandlersCollectionAndGetFirstLink(null, handlers);
}
/// <summary>
/// Gets a default Http Client handler with the appropriate proxy configurations
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.4.1</VersionPrefix>
<VersionPrefix>1.4.2</VersionPrefix>
<VersionSuffix></VersionSuffix>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<!-- Enable this line once we go live to prevent breaking changes -->
Expand Down
7 changes: 7 additions & 0 deletions src/Middleware/Options/UriReplacementHandlerOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ public UriReplacementHandlerOption(bool isEnabled, IEnumerable<KeyValuePair<stri
this.replacementPairs = replacementPairs;
}

/// <summary>
/// Creates a new instance of UriReplacementOption with no replacements.
/// </summary>
/// <param name="isEnabled">Whether replacement is enabled.</param>
/// <remarks>Replacement is disabled by default.</remarks>
public UriReplacementHandlerOption(bool isEnabled = false) : this(isEnabled, Array.Empty<KeyValuePair<string, string>>()) { }

/// <inheritdoc/>
public bool IsEnabled()
{
Expand Down