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

Commit

Permalink
Merge pull request #71 from microsoft/feature/native-request
Browse files Browse the repository at this point in the history
- adds a method to convert abstract requests to native requests in the request adapter interface.
  • Loading branch information
baywet authored Jan 10, 2023
2 parents 575cad8 + 7845c34 commit 10863ce
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 35 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public HttpRequestMessageExtensionsTests () {
requestAdapter = new HttpClientRequestAdapter(new AnonymousAuthenticationProvider());
}
[Fact]
public void GetRequestOptionCanExtractRequestOptionFromHttpRequestMessage()
public async Task GetRequestOptionCanExtractRequestOptionFromHttpRequestMessage()
{
// Arrange
var requestInfo = new RequestInformation()
Expand All @@ -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<HttpRequestMessage>(requestInfo);
var extractedOption = requestMessage.GetRequestOption<RedirectHandlerOption>();
// Assert
Assert.NotNull(extractedOption);
Expand All @@ -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<HttpRequestMessage>(requestInfo);
HttpRequestMessage clonedRequest = await originalRequest.CloneAsync();

Assert.NotNull(clonedRequest);
Expand All @@ -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<HttpRequestMessage>(requestInfo);
originalRequest.Content = new StringContent("contents");

var clonedRequest = await originalRequest.CloneAsync();
Expand All @@ -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<HttpRequestMessage>(requestInfo);
originalRequest.Content = new StringContent("contents");

var clonedRequest = await originalRequest.CloneAsync();
Expand All @@ -109,37 +109,37 @@ public async Task CloneAsyncWithRequestOption()
}

[Fact]
public void IsBufferedReturnsTrueForGetRequest()
public async Task IsBufferedReturnsTrueForGetRequest()
{
// Arrange
var requestInfo = new RequestInformation
{
HttpMethod = Method.GET,
URI = new Uri("http://localhost")
};
var originalRequest = requestAdapter.GetRequestMessageFromRequestInformation(requestInfo);
var originalRequest = await requestAdapter.ConvertToNativeRequestAsync<HttpRequestMessage>(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
{
HttpMethod = Method.POST,
URI = new Uri("http://localhost")
};
var originalRequest = requestAdapter.GetRequestMessageFromRequestInformation(requestInfo);
var originalRequest = await requestAdapter.ConvertToNativeRequestAsync<HttpRequestMessage>(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 };
Expand All @@ -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<HttpRequestMessage>(requestInfo);
// Act
var response = originalRequest.IsBuffered();
// Assert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<HttpRequestMessage>(requestInfo);

// Act
var response = await _invoker.SendAsync(requestMessage, new CancellationToken());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<HttpRequestMessage>(requestInfo);
Assert.Empty(requestMessage.Headers);

// Act
Expand Down Expand Up @@ -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<HttpRequestMessage>(requestInfo);
Assert.Empty(requestMessage.Headers);

// Act
Expand Down Expand Up @@ -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<HttpRequestMessage>(requestInfo);// get a request message
Assert.Empty(requestMessage.Headers);

// Act
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public async Task DisabledUserAgentHandlerDoesNotChangeRequest()
}
});
// Act and get a request message
var requestMessage = requestAdapter.GetRequestMessageFromRequestInformation(requestInfo);
var requestMessage = await requestAdapter.ConvertToNativeRequestAsync<HttpRequestMessage>(requestInfo);
Assert.Empty(requestMessage.Headers);

// Act
Expand All @@ -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<HttpRequestMessage>(requestInfo);
Assert.Empty(requestMessage.Headers);

// Act
Expand All @@ -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<HttpRequestMessage>(requestInfo);
Assert.Empty(requestMessage.Headers);

// Act
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void EnablesBackingStore()


[Fact]
public void GetRequestMessageFromRequestInformationWithBaseUrlTemplate()
public async Task GetRequestMessageFromRequestInformationWithBaseUrlTemplate()
{
// Arrange
requestAdapter.BaseUrl = "http://localhost";
Expand All @@ -64,7 +64,7 @@ public void GetRequestMessageFromRequestInformationWithBaseUrlTemplate()
};

// Act
var requestMessage = requestAdapter.GetRequestMessageFromRequestInformation(requestInfo);
var requestMessage = await requestAdapter.ConvertToNativeRequestAsync<HttpRequestMessage>(requestInfo);

// Assert
Assert.NotNull(requestMessage.RequestUri);
Expand All @@ -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
Expand All @@ -87,15 +87,15 @@ public void GetRequestMessageFromRequestInformationSetsQueryParametersCorrectlyW
requestInfo.QueryParameters.Add(queryParam, queryParamObject);

// Act
var requestMessage = requestAdapter.GetRequestMessageFromRequestInformation(requestInfo);
var requestMessage = await requestAdapter.ConvertToNativeRequestAsync<HttpRequestMessage>(requestInfo);

// Assert
Assert.NotNull(requestMessage.RequestUri);
Assert.Contains(expectedString, requestMessage.RequestUri.Query);
}

[Fact]
public void GetRequestMessageFromRequestInformationSetsContentHeaders()
public async Task GetRequestMessageFromRequestInformationSetsContentHeaders()
{
// Arrange
var requestInfo = new RequestInformation
Expand All @@ -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<HttpRequestMessage>(requestInfo);

// Assert
Assert.NotNull(requestMessage.Content);
Expand Down
17 changes: 8 additions & 9 deletions src/HttpClientRequestAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ private async Task<HttpResponseMessage> GetHttpResponseMessage(RequestInformatio
var additionalAuthenticationContext = string.IsNullOrEmpty(claims) ? null : new Dictionary<string, object> { { 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)
{
Expand Down Expand Up @@ -449,16 +449,15 @@ private void SetBaseUrlForRequestInformation(RequestInformation requestInfo)
{
requestInfo.PathParameters.TryAdd("baseurl", BaseUrl);
}
/// <summary>
/// Creates a <see cref="HttpRequestMessage"/> instance from a <see cref="RequestInformation"/> instance.
/// </summary>
/// <param name="requestInfo">The <see cref="RequestInformation"/> instance to convert.</param>
/// <returns>A <see cref="HttpRequestMessage"/> instance</returns>
public HttpRequestMessage GetRequestMessageFromRequestInformation(RequestInformation requestInfo)
/// <inheritdoc/>
public async Task<T> ConvertToNativeRequestAsync<T>(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.
Expand Down
4 changes: 2 additions & 2 deletions src/Microsoft.Kiota.Http.HttpClientLibrary.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<Deterministic>true</Deterministic>
<VersionPrefix>1.0.0</VersionPrefix>
<VersionSuffix>rc.2</VersionSuffix>
<VersionSuffix>rc.3</VersionSuffix>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<!-- Enable this line once we go live to prevent breaking changes -->
<!-- <PackageValidationBaselineVersion>1.0.0</PackageValidationBaselineVersion> -->
Expand All @@ -32,7 +32,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
<PackageReference Include="Microsoft.Kiota.Abstractions" Version="1.0.0-rc.2" />
<PackageReference Include="Microsoft.Kiota.Abstractions" Version="1.0.0-rc.3" />
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="7.0.0" />
<PackageReference Include="System.Text.Json" Version="7.0.1" />
</ItemGroup>
Expand Down

0 comments on commit 10863ce

Please sign in to comment.