From c9db926fb59d605bc603bfdc4d2662c4bd262969 Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Mon, 9 Jan 2023 10:29:29 -0500 Subject: [PATCH 1/2] - adds a method to convert abstract requests to native requests in the request adapter interface. Signed-off-by: Vincent Biret --- CHANGELOG.md | 6 +++++ .../HttpRequestMessageExtensionsTests.cs | 22 +++++++++---------- .../ParametersNameDecodingHandlerTests.cs | 2 +- .../Middleware/TelemetryHandlerTests.cs | 6 ++--- .../Middleware/UserAgentHandlerTests.cs | 6 ++--- .../RequestAdapterTests.cs | 12 +++++----- src/HttpClientRequestAdapter.cs | 17 +++++++------- ...rosoft.Kiota.Http.HttpClientLibrary.csproj | 2 +- 8 files changed, 39 insertions(+), 34 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ccdbe61..20ae894 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added +## [1.0.0-rc.3] - 2023-01-09 + +### Added + +- Added a method to convert abstract requests to native requests in the request adapter interface. + ## [1.0.0-rc.2] - 2023-01-05 ### Added diff --git a/Microsoft.Kiota.Http.HttpClientLibrary.Tests/Extensions/HttpRequestMessageExtensionsTests.cs b/Microsoft.Kiota.Http.HttpClientLibrary.Tests/Extensions/HttpRequestMessageExtensionsTests.cs index 1e75066..e58ec6e 100644 --- a/Microsoft.Kiota.Http.HttpClientLibrary.Tests/Extensions/HttpRequestMessageExtensionsTests.cs +++ b/Microsoft.Kiota.Http.HttpClientLibrary.Tests/Extensions/HttpRequestMessageExtensionsTests.cs @@ -19,7 +19,7 @@ public HttpRequestMessageExtensionsTests () { requestAdapter = new HttpClientRequestAdapter(new AnonymousAuthenticationProvider()); } [Fact] - public void GetRequestOptionCanExtractRequestOptionFromHttpRequestMessage() + public async Task GetRequestOptionCanExtractRequestOptionFromHttpRequestMessage() { // Arrange var requestInfo = new RequestInformation() @@ -33,7 +33,7 @@ public void GetRequestOptionCanExtractRequestOptionFromHttpRequestMessage() }; requestInfo.AddRequestOptions(new IRequestOption[] { redirectHandlerOption }); // Act and get a request message - var requestMessage = requestAdapter.GetRequestMessageFromRequestInformation(requestInfo); + var requestMessage = await requestAdapter.ConvertToNativeRequestAsync(requestInfo); var extractedOption = requestMessage.GetRequestOption(); // Assert Assert.NotNull(extractedOption); @@ -49,7 +49,7 @@ public async Task CloneAsyncWithEmptyHttpContent() HttpMethod = Method.GET, URI = new Uri("http://localhost") }; - var originalRequest = requestAdapter.GetRequestMessageFromRequestInformation(requestInfo); + var originalRequest = await requestAdapter.ConvertToNativeRequestAsync(requestInfo); HttpRequestMessage clonedRequest = await originalRequest.CloneAsync(); Assert.NotNull(clonedRequest); @@ -67,7 +67,7 @@ public async Task CloneAsyncWithHttpContent() URI = new Uri("http://localhost") }; requestInfo.SetStreamContent(new MemoryStream(Encoding.UTF8.GetBytes("contents"))); - var originalRequest = requestAdapter.GetRequestMessageFromRequestInformation(requestInfo); + var originalRequest = await requestAdapter.ConvertToNativeRequestAsync(requestInfo); originalRequest.Content = new StringContent("contents"); var clonedRequest = await originalRequest.CloneAsync(); @@ -93,7 +93,7 @@ public async Task CloneAsyncWithRequestOption() MaxRedirect = 7 }; requestInfo.AddRequestOptions(new IRequestOption[] { redirectHandlerOption }); - var originalRequest = requestAdapter.GetRequestMessageFromRequestInformation(requestInfo); + var originalRequest = await requestAdapter.ConvertToNativeRequestAsync(requestInfo); originalRequest.Content = new StringContent("contents"); var clonedRequest = await originalRequest.CloneAsync(); @@ -109,7 +109,7 @@ public async Task CloneAsyncWithRequestOption() } [Fact] - public void IsBufferedReturnsTrueForGetRequest() + public async Task IsBufferedReturnsTrueForGetRequest() { // Arrange var requestInfo = new RequestInformation @@ -117,14 +117,14 @@ public void IsBufferedReturnsTrueForGetRequest() HttpMethod = Method.GET, URI = new Uri("http://localhost") }; - var originalRequest = requestAdapter.GetRequestMessageFromRequestInformation(requestInfo); + var originalRequest = await requestAdapter.ConvertToNativeRequestAsync(requestInfo); // Act var response = originalRequest.IsBuffered(); // Assert Assert.True(response, "Unexpected content type"); } [Fact] - public void IsBufferedReturnsTrueForPostWithNoContent() + public async Task IsBufferedReturnsTrueForPostWithNoContent() { // Arrange var requestInfo = new RequestInformation @@ -132,14 +132,14 @@ public void IsBufferedReturnsTrueForPostWithNoContent() HttpMethod = Method.POST, URI = new Uri("http://localhost") }; - var originalRequest = requestAdapter.GetRequestMessageFromRequestInformation(requestInfo); + var originalRequest = await requestAdapter.ConvertToNativeRequestAsync(requestInfo); // Act var response = originalRequest.IsBuffered(); // Assert Assert.True(response, "Unexpected content type"); } [Fact] - public void IsBufferedReturnsTrueForPostWithBufferStringContent() + public async Task IsBufferedReturnsTrueForPostWithBufferStringContent() { // Arrange byte[] data = new byte[] { 1, 2, 3, 4, 5 }; @@ -149,7 +149,7 @@ public void IsBufferedReturnsTrueForPostWithBufferStringContent() URI = new Uri("http://localhost"), Content = new MemoryStream(data) }; - var originalRequest = requestAdapter.GetRequestMessageFromRequestInformation(requestInfo); + var originalRequest = await requestAdapter.ConvertToNativeRequestAsync(requestInfo); // Act var response = originalRequest.IsBuffered(); // Assert diff --git a/Microsoft.Kiota.Http.HttpClientLibrary.Tests/Middleware/ParametersNameDecodingHandlerTests.cs b/Microsoft.Kiota.Http.HttpClientLibrary.Tests/Middleware/ParametersNameDecodingHandlerTests.cs index 9f870d5..9f30849 100644 --- a/Microsoft.Kiota.Http.HttpClientLibrary.Tests/Middleware/ParametersNameDecodingHandlerTests.cs +++ b/Microsoft.Kiota.Http.HttpClientLibrary.Tests/Middleware/ParametersNameDecodingHandlerTests.cs @@ -38,7 +38,7 @@ public async Task DefaultParameterNameDecodingHandlerDecodesNames(string origina URI = new Uri(original) }; // Act and get a request message - var requestMessage = requestAdapter.GetRequestMessageFromRequestInformation(requestInfo); + var requestMessage = await requestAdapter.ConvertToNativeRequestAsync(requestInfo); // Act var response = await _invoker.SendAsync(requestMessage, new CancellationToken()); diff --git a/Microsoft.Kiota.Http.HttpClientLibrary.Tests/Middleware/TelemetryHandlerTests.cs b/Microsoft.Kiota.Http.HttpClientLibrary.Tests/Middleware/TelemetryHandlerTests.cs index 0c10e96..8c9a0bb 100644 --- a/Microsoft.Kiota.Http.HttpClientLibrary.Tests/Middleware/TelemetryHandlerTests.cs +++ b/Microsoft.Kiota.Http.HttpClientLibrary.Tests/Middleware/TelemetryHandlerTests.cs @@ -36,7 +36,7 @@ public async Task DefaultTelemetryHandlerDoesNotChangeRequest() URI = new Uri("http://localhost") }; // Act and get a request message - var requestMessage = requestAdapter.GetRequestMessageFromRequestInformation(requestInfo); + var requestMessage = await requestAdapter.ConvertToNativeRequestAsync(requestInfo); Assert.Empty(requestMessage.Headers); // Act @@ -67,7 +67,7 @@ public async Task TelemetryHandlerSelectivelyEnrichesRequestsBasedOnRequestMiddl // Configures the telemetry at the request level requestInfo.AddRequestOptions(new IRequestOption[] { telemetryHandlerOption }); // Act and get a request message - var requestMessage = requestAdapter.GetRequestMessageFromRequestInformation(requestInfo); + var requestMessage = await requestAdapter.ConvertToNativeRequestAsync(requestInfo); Assert.Empty(requestMessage.Headers); // Act @@ -105,7 +105,7 @@ public async Task TelemetryHandlerGloballyEnrichesRequests() URI = new Uri("http://localhost") }; - var requestMessage = requestAdapter.GetRequestMessageFromRequestInformation(requestInfo);// get a request message + var requestMessage = await requestAdapter.ConvertToNativeRequestAsync(requestInfo);// get a request message Assert.Empty(requestMessage.Headers); // Act diff --git a/Microsoft.Kiota.Http.HttpClientLibrary.Tests/Middleware/UserAgentHandlerTests.cs b/Microsoft.Kiota.Http.HttpClientLibrary.Tests/Middleware/UserAgentHandlerTests.cs index 850d3be..81887f2 100644 --- a/Microsoft.Kiota.Http.HttpClientLibrary.Tests/Middleware/UserAgentHandlerTests.cs +++ b/Microsoft.Kiota.Http.HttpClientLibrary.Tests/Middleware/UserAgentHandlerTests.cs @@ -41,7 +41,7 @@ public async Task DisabledUserAgentHandlerDoesNotChangeRequest() } }); // Act and get a request message - var requestMessage = requestAdapter.GetRequestMessageFromRequestInformation(requestInfo); + var requestMessage = await requestAdapter.ConvertToNativeRequestAsync(requestInfo); Assert.Empty(requestMessage.Headers); // Act @@ -62,7 +62,7 @@ public async Task EnabledUserAgentHandlerAddsHeaderValue() }; var defaultOption = new UserAgentHandlerOption(); // Act and get a request message - var requestMessage = requestAdapter.GetRequestMessageFromRequestInformation(requestInfo); + var requestMessage = await requestAdapter.ConvertToNativeRequestAsync(requestInfo); Assert.Empty(requestMessage.Headers); // Act @@ -88,7 +88,7 @@ public async Task DoesntAddProductTwice() }; var defaultOption = new UserAgentHandlerOption(); // Act and get a request message - var requestMessage = requestAdapter.GetRequestMessageFromRequestInformation(requestInfo); + var requestMessage = await requestAdapter.ConvertToNativeRequestAsync(requestInfo); Assert.Empty(requestMessage.Headers); // Act diff --git a/Microsoft.Kiota.Http.HttpClientLibrary.Tests/RequestAdapterTests.cs b/Microsoft.Kiota.Http.HttpClientLibrary.Tests/RequestAdapterTests.cs index f1f4708..f9e8519 100644 --- a/Microsoft.Kiota.Http.HttpClientLibrary.Tests/RequestAdapterTests.cs +++ b/Microsoft.Kiota.Http.HttpClientLibrary.Tests/RequestAdapterTests.cs @@ -53,7 +53,7 @@ public void EnablesBackingStore() [Fact] - public void GetRequestMessageFromRequestInformationWithBaseUrlTemplate() + public async Task GetRequestMessageFromRequestInformationWithBaseUrlTemplate() { // Arrange requestAdapter.BaseUrl = "http://localhost"; @@ -64,7 +64,7 @@ public void GetRequestMessageFromRequestInformationWithBaseUrlTemplate() }; // Act - var requestMessage = requestAdapter.GetRequestMessageFromRequestInformation(requestInfo); + var requestMessage = await requestAdapter.ConvertToNativeRequestAsync(requestInfo); // Assert Assert.NotNull(requestMessage.RequestUri); @@ -76,7 +76,7 @@ public void GetRequestMessageFromRequestInformationWithBaseUrlTemplate() [InlineData("count", true, "count=true")] [InlineData("skip", 10, "skip=10")] [InlineData("skip", null, "")]// query parameter no placed - public void GetRequestMessageFromRequestInformationSetsQueryParametersCorrectlyWithSelect(string queryParam, object queryParamObject, string expectedString) + public async Task GetRequestMessageFromRequestInformationSetsQueryParametersCorrectlyWithSelect(string queryParam, object queryParamObject, string expectedString) { // Arrange var requestInfo = new RequestInformation @@ -87,7 +87,7 @@ public void GetRequestMessageFromRequestInformationSetsQueryParametersCorrectlyW requestInfo.QueryParameters.Add(queryParam, queryParamObject); // Act - var requestMessage = requestAdapter.GetRequestMessageFromRequestInformation(requestInfo); + var requestMessage = await requestAdapter.ConvertToNativeRequestAsync(requestInfo); // Assert Assert.NotNull(requestMessage.RequestUri); @@ -95,7 +95,7 @@ public void GetRequestMessageFromRequestInformationSetsQueryParametersCorrectlyW } [Fact] - public void GetRequestMessageFromRequestInformationSetsContentHeaders() + public async Task GetRequestMessageFromRequestInformationSetsContentHeaders() { // Arrange var requestInfo = new RequestInformation @@ -108,7 +108,7 @@ public void GetRequestMessageFromRequestInformationSetsContentHeaders() requestInfo.SetStreamContent(new MemoryStream(Encoding.UTF8.GetBytes("contents"))); // Act - var requestMessage = requestAdapter.GetRequestMessageFromRequestInformation(requestInfo); + var requestMessage = await requestAdapter.ConvertToNativeRequestAsync(requestInfo); // Assert Assert.NotNull(requestMessage.Content); diff --git a/src/HttpClientRequestAdapter.cs b/src/HttpClientRequestAdapter.cs index d90fae4..66385e6 100644 --- a/src/HttpClientRequestAdapter.cs +++ b/src/HttpClientRequestAdapter.cs @@ -393,7 +393,7 @@ private async Task GetHttpResponseMessage(RequestInformatio var additionalAuthenticationContext = string.IsNullOrEmpty(claims) ? null : new Dictionary { { ClaimsKey, claims } }; await authProvider.AuthenticateRequestAsync(requestInfo, additionalAuthenticationContext, cancellationToken); - using var message = GetRequestMessageFromRequestInformationInternal(requestInfo, activityForAttributes); + using var message = GetRequestMessageFromRequestInformation(requestInfo, activityForAttributes); var response = await this.client.SendAsync(message,cancellationToken); if(response == null) { @@ -449,16 +449,15 @@ private void SetBaseUrlForRequestInformation(RequestInformation requestInfo) { requestInfo.PathParameters.TryAdd("baseurl", BaseUrl); } - /// - /// Creates a instance from a instance. - /// - /// The instance to convert. - /// A instance - public HttpRequestMessage GetRequestMessageFromRequestInformation(RequestInformation requestInfo) + /// + public async Task ConvertToNativeRequestAsync(RequestInformation requestInfo, CancellationToken cancellationToken = default) { - return GetRequestMessageFromRequestInformationInternal(requestInfo, null); + await authProvider.AuthenticateRequestAsync(requestInfo, null, cancellationToken); + if (GetRequestMessageFromRequestInformation(requestInfo, null) is T result) + return result; + else throw new InvalidOperationException($"Could not convert the request information to a {typeof(T).Name}"); } - private HttpRequestMessage GetRequestMessageFromRequestInformationInternal(RequestInformation requestInfo, Activity activityForAttributes) + private HttpRequestMessage GetRequestMessageFromRequestInformation(RequestInformation requestInfo, Activity activityForAttributes) { using var span = activitySource?.StartActivity(nameof(GetRequestMessageFromRequestInformation)); SetBaseUrlForRequestInformation(requestInfo);// this method can also be called from a different context so ensure the baseUrl is added. diff --git a/src/Microsoft.Kiota.Http.HttpClientLibrary.csproj b/src/Microsoft.Kiota.Http.HttpClientLibrary.csproj index fb6737c..15973f8 100644 --- a/src/Microsoft.Kiota.Http.HttpClientLibrary.csproj +++ b/src/Microsoft.Kiota.Http.HttpClientLibrary.csproj @@ -14,7 +14,7 @@ true true 1.0.0 - rc.2 + rc.3 true From 7845c345c7be1ef8c55dea67b63c7a5bf5c98d50 Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Tue, 10 Jan 2023 07:59:20 -0500 Subject: [PATCH 2/2] - bumps abstractions version --- src/Microsoft.Kiota.Http.HttpClientLibrary.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.Kiota.Http.HttpClientLibrary.csproj b/src/Microsoft.Kiota.Http.HttpClientLibrary.csproj index 15973f8..d32aead 100644 --- a/src/Microsoft.Kiota.Http.HttpClientLibrary.csproj +++ b/src/Microsoft.Kiota.Http.HttpClientLibrary.csproj @@ -32,7 +32,7 @@ - +