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

Commit

Permalink
Merge branch 'main' into bugfix/hard-version
Browse files Browse the repository at this point in the history
  • Loading branch information
baywet authored May 21, 2024
2 parents ed9f4c1 + 3464453 commit c907639
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 18 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [1.4.2] - 2024-05-21

### Added

- `GetDefaultHandlerTypes` added to `KiotaClientFactory` if you're creating your own `HttpClient` and still want to use the default handlers.

### Changed

- Fixed an issue where fixed versions of abstractions would result in restore failures. [#256](https://github.com/microsoft/kiota-http-dotnet/issues/256)
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
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

0 comments on commit c907639

Please sign in to comment.