Skip to content

Commit 4aa10e1

Browse files
authored
Merge pull request #566 from Rans4ckeR/fix-565
feat: adds the ability to pass the http version for netstandard/netfx/netcore instead of using a hardcoded value (default http/2) fix: get the http version and version policy from the http client for net5+ instead of using a hardcoded value (default http/2)
1 parent 9879648 commit 4aa10e1

13 files changed

+246
-73
lines changed

src/abstractions/Microsoft.Kiota.Abstractions.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
2-
<Sdk Name="Microsoft.DotNet.PackageValidation" Version="1.0.0-preview.7.21379.12" />
3-
1+
<Project Sdk="Microsoft.NET.Sdk">
42
<PropertyGroup>
53
<Description>Abstractions library for the Kiota generated SDKs in dotnet.</Description>
64
<AssemblyTitle>Kiota Abstractions Library for dotnet</AssemblyTitle>
75
<!-- NET 5 target to be removed on next major version-->
86
<TargetFrameworks>netstandard2.0;netstandard2.1;net5.0;net6.0;net8.0</TargetFrameworks>
97
<GenerateDocumentationFile>true</GenerateDocumentationFile>
8+
<EnablePackageValidation>true</EnablePackageValidation>
9+
<GenerateCompatibilitySuppressionFile>false</GenerateCompatibilitySuppressionFile>
1010
</PropertyGroup>
1111

1212
<ItemGroup>

src/authentication/azure/Microsoft.Kiota.Authentication.Azure.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
2-
<Sdk Name="Microsoft.DotNet.PackageValidation" Version="1.0.0-preview.7.21379.12" />
3-
1+
<Project Sdk="Microsoft.NET.Sdk">
42
<PropertyGroup>
53
<Description>Kiota authentication provider implementation with Azure Identity.</Description>
64
<AssemblyTitle>Kiota Azure Identity Authentication Library for dotnet</AssemblyTitle>
75
<!-- NET 5 target to be removed on next major version-->
86
<TargetFrameworks>netstandard2.0;netstandard2.1;net5.0;net6.0;net8.0</TargetFrameworks>
97
<GenerateDocumentationFile>true</GenerateDocumentationFile>
8+
<EnablePackageValidation>true</EnablePackageValidation>
9+
<GenerateCompatibilitySuppressionFile>false</GenerateCompatibilitySuppressionFile>
1010
</PropertyGroup>
1111

1212
<ItemGroup>

src/bundle/Microsoft.Kiota.Bundle.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
2-
<Sdk Name="Microsoft.DotNet.PackageValidation" Version="1.0.0-preview.7.21379.12" />
3-
1+
<Project Sdk="Microsoft.NET.Sdk">
42
<PropertyGroup>
53
<Description>Kiota Bundle package providing default implementations for client setup.</Description>
64
<AssemblyTitle>Kiota Bundle package for dotnet</AssemblyTitle>
75
<!-- NET 5 target to be removed on next major version-->
86
<TargetFrameworks>netstandard2.0;netstandard2.1;net5.0;net6.0;net8.0</TargetFrameworks>
97
<GenerateDocumentationFile>true</GenerateDocumentationFile>
8+
<EnablePackageValidation>true</EnablePackageValidation>
9+
<GenerateCompatibilitySuppressionFile>false</GenerateCompatibilitySuppressionFile>
1010
</PropertyGroup>
1111

1212
<ItemGroup>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!-- https://learn.microsoft.com/dotnet/fundamentals/package-validation/diagnostic-ids -->
3+
<Suppressions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4+
<Suppression>
5+
<DiagnosticId>CP0002</DiagnosticId>
6+
<Target>M:Microsoft.Kiota.Http.HttpClientLibrary.HttpClientRequestAdapter.#ctor(Microsoft.Kiota.Abstractions.Authentication.IAuthenticationProvider,Microsoft.Kiota.Abstractions.Serialization.IParseNodeFactory,Microsoft.Kiota.Abstractions.Serialization.ISerializationWriterFactory,System.Net.Http.HttpClient,Microsoft.Kiota.Http.HttpClientLibrary.ObservabilityOptions,System.Version)</Target>
7+
<Left>lib/netstandard2.1/Microsoft.Kiota.Http.HttpClientLibrary.dll</Left>
8+
<Right>lib/net5.0/Microsoft.Kiota.Http.HttpClientLibrary.dll</Right>
9+
</Suppression>
10+
</Suppressions>

src/http/httpClient/HttpClientRequestAdapter.cs

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@
55
using System;
66
using System.Collections.Generic;
77
using System.Diagnostics;
8+
#if NET5_0_OR_GREATER
89
using System.Diagnostics.CodeAnalysis;
10+
#endif
911
using System.IO;
1012
using System.Net;
1113
using System.Net.Http;
14+
#if NET5_0_OR_GREATER
1215
using System.Net.Http.Headers;
16+
#endif
1317
using System.Text.RegularExpressions;
1418
using System.Threading;
1519
using System.Threading.Tasks;
@@ -36,6 +40,15 @@ public class HttpClientRequestAdapter : IRequestAdapter, IDisposable
3640
private readonly bool createdClient;
3741
private readonly ObservabilityOptions obsOptions;
3842
private readonly ActivitySource activitySource;
43+
#if !NET5_0_OR_GREATER
44+
private readonly Version httpVersion;
45+
#endif
46+
#if NETSTANDARD2_1_OR_GREATER
47+
private static readonly Version defaultHttpVersion = HttpVersion.Version20;
48+
#elif NETSTANDARD2_0 || NETFRAMEWORK
49+
private static readonly Version defaultHttpVersion = HttpVersion.Version11;
50+
#endif
51+
#if NET5_0_OR_GREATER
3952
/// <summary>
4053
/// Initializes a new instance of the <see cref="HttpClientRequestAdapter"/> class.
4154
/// <param name="authenticationProvider">The authentication provider.</param>
@@ -45,6 +58,18 @@ public class HttpClientRequestAdapter : IRequestAdapter, IDisposable
4558
/// <param name="observabilityOptions">The observability options.</param>
4659
/// </summary>
4760
public HttpClientRequestAdapter(IAuthenticationProvider authenticationProvider, IParseNodeFactory? parseNodeFactory = null, ISerializationWriterFactory? serializationWriterFactory = null, HttpClient? httpClient = null, ObservabilityOptions? observabilityOptions = null)
61+
#else
62+
/// <summary>
63+
/// Initializes a new instance of the <see cref="HttpClientRequestAdapter"/> class.
64+
/// <param name="authenticationProvider">The authentication provider.</param>
65+
/// <param name="parseNodeFactory">The parse node factory.</param>
66+
/// <param name="serializationWriterFactory">The serialization writer factory.</param>
67+
/// <param name="httpClient">The native HTTP client.</param>
68+
/// <param name="observabilityOptions">The observability options.</param>
69+
/// <param name="httpVersion">The HTTP version.</param>
70+
/// </summary>
71+
public HttpClientRequestAdapter(IAuthenticationProvider authenticationProvider, IParseNodeFactory? parseNodeFactory = null, ISerializationWriterFactory? serializationWriterFactory = null, HttpClient? httpClient = null, ObservabilityOptions? observabilityOptions = null, Version? httpVersion = null)
72+
#endif
4873
{
4974
authProvider = authenticationProvider ?? throw new ArgumentNullException(nameof(authenticationProvider));
5075
createdClient = httpClient == null;
@@ -54,6 +79,9 @@ public HttpClientRequestAdapter(IAuthenticationProvider authenticationProvider,
5479
sWriterFactory = serializationWriterFactory ?? SerializationWriterFactoryRegistry.DefaultInstance;
5580
obsOptions = observabilityOptions ?? new ObservabilityOptions();
5681
activitySource = ActivitySourceRegistry.DefaultInstance.GetOrCreateActivitySource(obsOptions.TracerInstrumentationName);
82+
#if !NET5_0_OR_GREATER
83+
this.httpVersion = httpVersion ?? defaultHttpVersion;
84+
#endif
5785
}
5886
/// <summary>Factory to use to get a serializer for payload serialization</summary>
5987
public ISerializationWriterFactory SerializationWriterFactory
@@ -93,7 +121,7 @@ public string? BaseUrl
93121
using var span = startTracingSpan(requestInfo, nameof(SendCollectionAsync));
94122
var response = await GetHttpResponseMessageAsync(requestInfo, cancellationToken, span).ConfigureAwait(false);
95123
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP3_0_OR_GREATER
96-
if (requestInfo.Content is not null)
124+
if(requestInfo.Content is not null)
97125
await requestInfo.Content.DisposeAsync().ConfigureAwait(false);
98126
#else
99127
requestInfo.Content?.Dispose();
@@ -138,7 +166,7 @@ public string? BaseUrl
138166
using var span = startTracingSpan(requestInfo, nameof(SendPrimitiveCollectionAsync));
139167
var response = await GetHttpResponseMessageAsync(requestInfo, cancellationToken, span).ConfigureAwait(false);
140168
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP3_0_OR_GREATER
141-
if (requestInfo.Content is not null)
169+
if(requestInfo.Content is not null)
142170
await requestInfo.Content.DisposeAsync().ConfigureAwait(false);
143171
#else
144172
requestInfo.Content?.Dispose();
@@ -184,7 +212,7 @@ public string? BaseUrl
184212
using var span = startTracingSpan(requestInfo, nameof(SendAsync));
185213
var response = await GetHttpResponseMessageAsync(requestInfo, cancellationToken, span).ConfigureAwait(false);
186214
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP3_0_OR_GREATER
187-
if (requestInfo.Content is not null)
215+
if(requestInfo.Content is not null)
188216
await requestInfo.Content.DisposeAsync().ConfigureAwait(false);
189217
#else
190218
requestInfo.Content?.Dispose();
@@ -235,7 +263,7 @@ public string? BaseUrl
235263
var isStreamResponse = modelType == typeof(Stream);
236264
var response = await GetHttpResponseMessageAsync(requestInfo, cancellationToken, span, isStreamResponse: isStreamResponse).ConfigureAwait(false);
237265
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP3_0_OR_GREATER
238-
if (requestInfo.Content is not null)
266+
if(requestInfo.Content is not null)
239267
await requestInfo.Content.DisposeAsync().ConfigureAwait(false);
240268
#else
241269
requestInfo.Content?.Dispose();
@@ -362,7 +390,7 @@ public async Task SendNoContentAsync(RequestInformation requestInfo, Dictionary<
362390
using var span = startTracingSpan(requestInfo, nameof(SendNoContentAsync));
363391
var response = await GetHttpResponseMessageAsync(requestInfo, cancellationToken, span).ConfigureAwait(false);
364392
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP3_0_OR_GREATER
365-
if (requestInfo.Content is not null)
393+
if(requestInfo.Content is not null)
366394
await requestInfo.Content.DisposeAsync().ConfigureAwait(false);
367395
#else
368396
requestInfo.Content?.Dispose();
@@ -591,13 +619,18 @@ private HttpRequestMessage GetRequestMessageFromRequestInformation(RequestInform
591619
{
592620
Method = new HttpMethod(requestInfo.HttpMethod.ToString().ToUpperInvariant()),
593621
RequestUri = requestUri,
594-
Version = new Version(2, 0)
622+
#if NET5_0_OR_GREATER
623+
Version = client.DefaultRequestVersion,
624+
VersionPolicy = client.DefaultVersionPolicy
625+
#else
626+
Version = httpVersion
627+
#endif
595628
};
596629

597630
if(requestInfo.RequestOptions != null)
598631
#if NET5_0_OR_GREATER
599632
{
600-
foreach (var option in requestInfo.RequestOptions)
633+
foreach(var option in requestInfo.RequestOptions)
601634
message.Options.Set(new HttpRequestOptionsKey<IRequestOption>(option.GetType().FullName!), option);
602635
}
603636
message.Options.Set(new HttpRequestOptionsKey<IRequestOption>(typeof(ObservabilityOptions).FullName!), obsOptions);

src/http/httpClient/KiotaClientFactory.cs

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
using System;
66
using System.Collections.Generic;
7+
#if NET5_0_OR_GREATER
78
using System.Diagnostics.CodeAnalysis;
9+
#endif
810
using System.Net;
911
using System.Net.Http;
1012
using Microsoft.Kiota.Abstractions;
@@ -26,20 +28,7 @@ public static class KiotaClientFactory
2628
/// <param name="optionsForHandlers">A array of <see cref="IRequestOption"/> objects passed to the default handlers.</param>
2729
/// <returns>The <see cref="HttpClient"/> with the default middlewares.</returns>
2830
public static HttpClient Create(HttpMessageHandler? finalHandler = null, IRequestOption[]? optionsForHandlers = null)
29-
{
30-
var defaultHandlersEnumerable = CreateDefaultHandlers(optionsForHandlers);
31-
int count = 0;
32-
foreach(var _ in defaultHandlersEnumerable) count++;
33-
34-
var defaultHandlersArray = new DelegatingHandler[count];
35-
int index = 0;
36-
foreach(var handler2 in defaultHandlersEnumerable)
37-
{
38-
defaultHandlersArray[index++] = handler2;
39-
}
40-
var handler = ChainHandlersCollectionAndGetFirstLink(finalHandler ?? GetDefaultHttpMessageHandler(), defaultHandlersArray);
41-
return handler != null ? new HttpClient(handler) : new HttpClient();
42-
}
31+
=> Create(finalHandler, CreateDefaultHandlers(optionsForHandlers));
4332

4433
/// <summary>
4534
/// Initializes the <see cref="HttpClient"/> with a custom middleware pipeline.
@@ -48,19 +37,9 @@ public static HttpClient Create(HttpMessageHandler? finalHandler = null, IReques
4837
/// <param name="finalHandler">The final <see cref="HttpMessageHandler"/> in the http pipeline. Can be configured for proxies, auto-decompression and auto-redirects</param>
4938
/// <returns>The <see cref="HttpClient"/> with the custom handlers.</returns>
5039
public static HttpClient Create(IList<DelegatingHandler> handlers, HttpMessageHandler? finalHandler = null)
51-
{
52-
if(handlers == null || handlers.Count == 0)
53-
return Create(finalHandler);
54-
55-
DelegatingHandler[] handlersArray = new DelegatingHandler[handlers.Count];
56-
for(int i = 0; i < handlers.Count; i++)
57-
{
58-
handlersArray[i] = handlers[i];
59-
}
60-
61-
var handler = ChainHandlersCollectionAndGetFirstLink(finalHandler ?? GetDefaultHttpMessageHandler(), handlersArray);
62-
return handler != null ? new HttpClient(handler) : new HttpClient();
63-
}
40+
=> handlers?.Count is not > 0
41+
? Create(finalHandler)
42+
: Create(finalHandler, handlers);
6443

6544
/// <summary>
6645
/// Initializes the <see cref="HttpClient"/> with the default configuration and authentication middleware using the <see cref="IAuthenticationProvider"/> if provided.
@@ -168,7 +147,7 @@ public static IList<ActivatableType> GetDefaultHandlerActivatableTypes()
168147
public readonly struct ActivatableType
169148
{
170149
#if NET5_0_OR_GREATER
171-
/// <summary>
150+
/// <summary>
172151
/// Provides DI-safe trim annotations for an underlying type.
173152
/// </summary>
174153
/// <param name="type">The type to be wrapped.</param>
@@ -182,7 +161,7 @@ public ActivatableType([DynamicallyAccessedMembers(DynamicallyAccessedMemberType
182161
/// </summary>
183162
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
184163
public readonly Type Type;
185-
164+
186165
#else
187166
/// <summary>
188167
/// Provides DI-safe trim annotations for an underlying type.
@@ -207,7 +186,6 @@ public ActivatableType(Type type)
207186
public static implicit operator Type(ActivatableType type) => type.Type;
208187
}
209188

210-
211189
/// <summary>
212190
/// Creates a <see cref="DelegatingHandler"/> to use for the <see cref="HttpClient" /> from the provided <see cref="DelegatingHandler"/> instances. Order matters.
213191
/// </summary>
@@ -261,5 +239,15 @@ public static HttpMessageHandler GetDefaultHttpMessageHandler(IWebProxy? proxy =
261239
return new HttpClientHandler { Proxy = proxy, AllowAutoRedirect = false, AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate };
262240
#endif
263241
}
242+
243+
private static HttpClient Create(HttpMessageHandler? finalHandler, IList<DelegatingHandler> handlersArray)
244+
{
245+
var handler = ChainHandlersCollectionAndGetFirstLink(finalHandler ?? GetDefaultHttpMessageHandler(), [.. handlersArray]);
246+
var client = handler != null ? new HttpClient(handler) : new HttpClient();
247+
#if NET5_0_OR_GREATER
248+
client.DefaultRequestVersion = HttpVersion.Version20;
249+
#endif
250+
return client;
251+
}
264252
}
265253
}

src/http/httpClient/Microsoft.Kiota.Http.HttpClientLibrary.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
2-
<Sdk Name="Microsoft.DotNet.PackageValidation" Version="1.0.0-preview.7.21379.12" />
3-
1+
<Project Sdk="Microsoft.NET.Sdk">
42
<PropertyGroup>
53
<Description>Kiota Http provider implementation for dotnet with HttpClient.</Description>
64
<AssemblyTitle>Kiota Http Library for dotnet</AssemblyTitle>
75
<!-- NET 5 target to be removed on next major version-->
86
<TargetFrameworks>netstandard2.0;netstandard2.1;net5.0;net6.0;net8.0;net462;net8.0-browser;</TargetFrameworks>
97
<GenerateDocumentationFile>true</GenerateDocumentationFile>
8+
<EnablePackageValidation>true</EnablePackageValidation>
9+
<GenerateCompatibilitySuppressionFile>false</GenerateCompatibilitySuppressionFile>
1010
</PropertyGroup>
1111

1212
<!-- NET 5 target to be removed on next major version-->

src/serialization/form/Microsoft.Kiota.Serialization.Form.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
2-
<Sdk Name="Microsoft.DotNet.PackageValidation" Version="1.0.0-preview.7.21379.12" />
3-
1+
<Project Sdk="Microsoft.NET.Sdk">
42
<PropertyGroup>
53
<Description>Kiota URI form encoded serialization provider.</Description>
64
<AssemblyTitle>Kiota URI form encoded serialization provider.</AssemblyTitle>
75
<!-- NET 5 target to be removed on next major version-->
86
<TargetFrameworks>netstandard2.0;netstandard2.1;net5.0;net6.0;net8.0</TargetFrameworks>
97
<GenerateDocumentationFile>true</GenerateDocumentationFile>
8+
<EnablePackageValidation>true</EnablePackageValidation>
9+
<GenerateCompatibilitySuppressionFile>false</GenerateCompatibilitySuppressionFile>
1010
</PropertyGroup>
1111

1212
<ItemGroup>

src/serialization/json/Microsoft.Kiota.Serialization.Json.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
2-
<Sdk Name="Microsoft.DotNet.PackageValidation" Version="1.0.0-preview.7.21379.12" />
3-
1+
<Project Sdk="Microsoft.NET.Sdk">
42
<PropertyGroup>
53
<Description>Kiota JSON serialization provider implementation with System.Text.Json.</Description>
64
<AssemblyTitle>Kiota JSON Serialization Library for dotnet using System.Text.Json</AssemblyTitle>
@@ -9,6 +7,8 @@
97
<GenerateDocumentationFile>true</GenerateDocumentationFile>
108
<NoWarn>$(NoWarn);CS1591</NoWarn> <!-- Ignore warning from code generated by source generators
119
from System.Text.Json-->
10+
<EnablePackageValidation>true</EnablePackageValidation>
11+
<GenerateCompatibilitySuppressionFile>false</GenerateCompatibilitySuppressionFile>
1212
</PropertyGroup>
1313

1414
<!-- NET 5 target to be removed on next major version-->

src/serialization/multipart/Microsoft.Kiota.Serialization.Multipart.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
2-
<Sdk Name="Microsoft.DotNet.PackageValidation" Version="1.0.0-preview.7.21379.12" />
3-
1+
<Project Sdk="Microsoft.NET.Sdk">
42
<PropertyGroup>
53
<Description>Kiota Multipart serialization provider implementation.</Description>
64
<AssemblyTitle>Kiota Multipart Serialization Library for dotnet</AssemblyTitle>
75
<!-- NET 5 target to be removed on next major version-->
86
<TargetFrameworks>netstandard2.0;netstandard2.1;net5.0;net6.0;net8.0</TargetFrameworks>
97
<GenerateDocumentationFile>true</GenerateDocumentationFile>
8+
<EnablePackageValidation>true</EnablePackageValidation>
9+
<GenerateCompatibilitySuppressionFile>false</GenerateCompatibilitySuppressionFile>
1010
</PropertyGroup>
1111

1212
<ItemGroup>

0 commit comments

Comments
 (0)