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 options-in-factory
Browse files Browse the repository at this point in the history
  • Loading branch information
baywet authored Apr 24, 2024
2 parents 56cb6c9 + 1e2aa48 commit 991bf35
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

# Mono auto generated files
mono_crash.*
.mono/

# Build results
[Dd]ebug/
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [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
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using Castle.Components.DictionaryAdapter.Xml;
Expand Down Expand Up @@ -111,5 +113,24 @@ public void CreateDefaultHandlersWithOptions()
Assert.NotNull(retryHandler);
Assert.NotNull(retryHandler.RetryOption);
}

[Fact]
public void CreateWithNullOrEmptyHandlersReturnsHttpClient()
{
var client = KiotaClientFactory.Create(null, null);

Check failure on line 120 in Microsoft.Kiota.Http.HttpClientLibrary.Tests/KiotaClientFactoryTests.cs

View workflow job for this annotation

GitHub Actions / build-and-test

The call is ambiguous between the following methods or properties: 'KiotaClientFactory.Create(HttpMessageHandler?, IRequestOption[]?)' and 'KiotaClientFactory.Create(IList<DelegatingHandler>, HttpMessageHandler?)'

Check failure on line 120 in Microsoft.Kiota.Http.HttpClientLibrary.Tests/KiotaClientFactoryTests.cs

View workflow job for this annotation

GitHub Actions / build-and-test

The call is ambiguous between the following methods or properties: 'KiotaClientFactory.Create(HttpMessageHandler?, IRequestOption[]?)' and 'KiotaClientFactory.Create(IList<DelegatingHandler>, HttpMessageHandler?)'

Check failure on line 120 in Microsoft.Kiota.Http.HttpClientLibrary.Tests/KiotaClientFactoryTests.cs

View workflow job for this annotation

GitHub Actions / build-and-test

The call is ambiguous between the following methods or properties: 'KiotaClientFactory.Create(HttpMessageHandler?, IRequestOption[]?)' and 'KiotaClientFactory.Create(IList<DelegatingHandler>, HttpMessageHandler?)'

Check failure on line 120 in Microsoft.Kiota.Http.HttpClientLibrary.Tests/KiotaClientFactoryTests.cs

View workflow job for this annotation

GitHub Actions / build-and-test

The call is ambiguous between the following methods or properties: 'KiotaClientFactory.Create(HttpMessageHandler?, IRequestOption[]?)' and 'KiotaClientFactory.Create(IList<DelegatingHandler>, HttpMessageHandler?)'
Assert.IsType<HttpClient>(client);

client = KiotaClientFactory.Create(new List<DelegatingHandler>());
Assert.IsType<HttpClient>(client);
}

[Fact]
public void CreateWithCustomMiddlewarePipelineReturnsHttpClient()
{
var handlers = KiotaClientFactory.CreateDefaultHandlers();
handlers.Add(new CompressionHandler());
var client = KiotaClientFactory.Create(handlers);
Assert.IsType<HttpClient>(client);
}
}
}
15 changes: 15 additions & 0 deletions src/KiotaClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@ public static HttpClient Create(HttpMessageHandler? finalHandler = null, IReques
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>
/// Creates a default set of middleware to be used by the <see cref="HttpClient"/>.
/// </summary>
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.12</VersionPrefix>
<VersionPrefix>1.4.0</VersionPrefix>
<VersionSuffix></VersionSuffix>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<!-- Enable this line once we go live to prevent breaking changes -->
Expand Down

0 comments on commit 991bf35

Please sign in to comment.