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 #44 from microsoft/responseHandlerChanges
Browse files Browse the repository at this point in the history
Changes the responseHandler to be a requestOption
  • Loading branch information
andrueastman authored Oct 17, 2022
2 parents 9117a45 + d780784 commit a0790e8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

## [1.0.0-preview.11] - 2022-10-17

### Changed

- Changes the ResponeHandler parameter in IRequestAdapter to be a RequestOption

## [1.0.0-preview.10] - 2022-09-19

### Added
Expand Down
32 changes: 18 additions & 14 deletions src/HttpClientRequestAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ private Activity startTracingSpan(RequestInformation requestInfo, string methodN
/// </summary>
/// <param name="requestInfo">The <see cref="RequestInformation"/> instance to send</param>
/// <param name="factory">The factory of the response model to deserialize the response into.</param>
/// <param name="responseHandler">The <see cref="IResponseHandler"/> to use with the response</param>
/// <param name="errorMapping">The error factories mapping to use in case of a failed request.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to use for cancelling the request.</param>
public async Task<IEnumerable<ModelType>> SendCollectionAsync<ModelType>(RequestInformation requestInfo, ParsableFactory<ModelType> factory, IResponseHandler responseHandler = default, Dictionary<string, ParsableFactory<IParsable>> errorMapping = default, CancellationToken cancellationToken = default) where ModelType : IParsable
public async Task<IEnumerable<ModelType>> SendCollectionAsync<ModelType>(RequestInformation requestInfo, ParsableFactory<ModelType> factory, Dictionary<string, ParsableFactory<IParsable>> errorMapping = default, CancellationToken cancellationToken = default) where ModelType : IParsable
{
using var span = startTracingSpan(requestInfo, nameof(SendCollectionAsync));
var response = await GetHttpResponseMessage(requestInfo, cancellationToken, span);
requestInfo.Content?.Dispose();
var responseHandler = GetResponseHandler(requestInfo);
if(responseHandler == null)
{
try {
Expand All @@ -109,14 +109,14 @@ public async Task<IEnumerable<ModelType>> SendCollectionAsync<ModelType>(Request
/// Executes the HTTP request specified by the given RequestInformation and returns the deserialized primitive response model collection.
/// </summary>
/// <param name="requestInfo">The RequestInformation object to use for the HTTP request.</param>
/// <param name="responseHandler">The response handler to use for the HTTP request instead of the default handler.</param>
/// <param name="errorMapping">The error factories mapping to use in case of a failed request.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to use for cancelling the request.</param>
/// <returns>The deserialized primitive response model collection.</returns>
public async Task<IEnumerable<ModelType>> SendPrimitiveCollectionAsync<ModelType>(RequestInformation requestInfo, IResponseHandler responseHandler = default, Dictionary<string, ParsableFactory<IParsable>> errorMapping = default, CancellationToken cancellationToken = default) {
public async Task<IEnumerable<ModelType>> SendPrimitiveCollectionAsync<ModelType>(RequestInformation requestInfo, Dictionary<string, ParsableFactory<IParsable>> errorMapping = default, CancellationToken cancellationToken = default) {
using var span = startTracingSpan(requestInfo, nameof(SendPrimitiveCollectionAsync));
var response = await GetHttpResponseMessage(requestInfo, cancellationToken, span);
requestInfo.Content?.Dispose();
var responseHandler = GetResponseHandler(requestInfo);
if(responseHandler == null)
{
try {
Expand Down Expand Up @@ -145,15 +145,15 @@ public async Task<IEnumerable<ModelType>> SendPrimitiveCollectionAsync<ModelType
/// </summary>
/// <param name="requestInfo">The <see cref="RequestInformation"/> instance to send</param>
/// <param name="factory">The factory of the response model to deserialize the response into.</param>
/// <param name="responseHandler">The <see cref="IResponseHandler"/> to use with the response</param>
/// <param name="errorMapping">The error factories mapping to use in case of a failed request.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to use for cancelling the request.</param>
/// <returns>The deserialized response model.</returns>
public async Task<ModelType> SendAsync<ModelType>(RequestInformation requestInfo, ParsableFactory<ModelType> factory, IResponseHandler responseHandler = null, Dictionary<string, ParsableFactory<IParsable>> errorMapping = default, CancellationToken cancellationToken = default) where ModelType : IParsable
public async Task<ModelType> SendAsync<ModelType>(RequestInformation requestInfo, ParsableFactory<ModelType> factory, Dictionary<string, ParsableFactory<IParsable>> errorMapping = default, CancellationToken cancellationToken = default) where ModelType : IParsable
{
using var span = startTracingSpan(requestInfo, nameof(SendAsync));
var response = await GetHttpResponseMessage(requestInfo, cancellationToken, span);
requestInfo.Content?.Dispose();
var responseHandler = GetResponseHandler(requestInfo);
if(responseHandler == null)
{
try {
Expand Down Expand Up @@ -181,15 +181,15 @@ public async Task<ModelType> SendAsync<ModelType>(RequestInformation requestInfo
/// Send a <see cref="RequestInformation"/> instance with a primitive instance of <typeparam name="ModelType"></typeparam>
/// </summary>
/// <param name="requestInfo">The <see cref="RequestInformation"/> instance to send</param>
/// <param name="responseHandler">The <see cref="IResponseHandler"/> to use with the response</param>
/// <param name="errorMapping">The error factories mapping to use in case of a failed request.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to use for cancelling the request.</param>
/// <returns>The deserialized primitive response model.</returns>
public async Task<ModelType> SendPrimitiveAsync<ModelType>(RequestInformation requestInfo, IResponseHandler responseHandler = default, Dictionary<string, ParsableFactory<IParsable>> errorMapping = default, CancellationToken cancellationToken = default)
public async Task<ModelType> SendPrimitiveAsync<ModelType>(RequestInformation requestInfo, Dictionary<string, ParsableFactory<IParsable>> errorMapping = default, CancellationToken cancellationToken = default)
{
using var span = startTracingSpan(requestInfo, nameof(SendPrimitiveAsync));
var response = await GetHttpResponseMessage(requestInfo, cancellationToken, span);
requestInfo.Content?.Dispose();
var responseHandler = GetResponseHandler(requestInfo);
if(responseHandler == null)
{
try {
Expand Down Expand Up @@ -286,15 +286,15 @@ public async Task<ModelType> SendPrimitiveAsync<ModelType>(RequestInformation re
/// Send a <see cref="RequestInformation"/> instance with an empty request body
/// </summary>
/// <param name="requestInfo">The <see cref="RequestInformation"/> instance to send</param>
/// <param name="responseHandler">The <see cref="IResponseHandler"/> to use with the response</param>
/// <param name="errorMapping">The error factories mapping to use in case of a failed request.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to use for cancelling the request.</param>
/// <returns></returns>
public async Task SendNoContentAsync(RequestInformation requestInfo, IResponseHandler responseHandler = null, Dictionary<string, ParsableFactory<IParsable>> errorMapping = default, CancellationToken cancellationToken = default)
public async Task SendNoContentAsync(RequestInformation requestInfo, Dictionary<string, ParsableFactory<IParsable>> errorMapping = default, CancellationToken cancellationToken = default)
{
using var span = startTracingSpan(requestInfo, nameof(SendNoContentAsync));
var response = await GetHttpResponseMessage(requestInfo, cancellationToken, span);
requestInfo.Content?.Dispose();
var responseHandler = GetResponseHandler(requestInfo);
if(responseHandler == null)
{
try {
Expand Down Expand Up @@ -348,10 +348,10 @@ private async Task ThrowFailedResponse(HttpResponseMessage response, Dictionary<
!errorMapping.TryGetValue(statusCodeAsString, out errorFactory) &&
!(statusCodeAsInt >= 400 && statusCodeAsInt < 500 && errorMapping.TryGetValue("4XX", out errorFactory)) &&
!(statusCodeAsInt >= 500 && statusCodeAsInt < 600 && errorMapping.TryGetValue("5XX", out errorFactory)))
{
activityForAttributes?.SetTag(ErrorMappingFoundAttributeName, false);
throw new ApiException($"The server returned an unexpected status code and no error factory is registered for this code: {statusCodeAsString}");
}
{
activityForAttributes?.SetTag(ErrorMappingFoundAttributeName, false);
throw new ApiException($"The server returned an unexpected status code and no error factory is registered for this code: {statusCodeAsString}");
}
activityForAttributes?.SetTag(ErrorMappingFoundAttributeName, true);

var rootNode = await GetRootParseNode(response);
Expand All @@ -365,6 +365,10 @@ private async Task ThrowFailedResponse(HttpResponseMessage response, Dictionary<

throw ex;
}
private static IResponseHandler GetResponseHandler(RequestInformation requestInfo)
{
return requestInfo.GetRequestOption<ResponseHandlerOption>()?.ResponseHandler;
}
private async Task<IParseNode> GetRootParseNode(HttpResponseMessage response)
{
using var span = activitySource?.StartActivity(nameof(GetRootParseNode));
Expand Down
6 changes: 3 additions & 3 deletions src/Microsoft.Kiota.Http.HttpClientLibrary.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<Deterministic>true</Deterministic>
<VersionPrefix>1.0.0</VersionPrefix>
<VersionSuffix>preview.10</VersionSuffix>
<VersionSuffix>preview.11</VersionSuffix>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<!-- Enable this line once we go live to prevent breaking changes -->
<!-- <PackageValidationBaselineVersion>1.0.0</PackageValidationBaselineVersion> -->
<SignAssembly>false</SignAssembly>
<DelaySign>false</DelaySign>
<AssemblyOriginatorKeyFile>35MSSharedLib1024.snk</AssemblyOriginatorKeyFile>
<PackageReleaseNotes>
- Added tracing support through OpenTelemetry.
- Changes the ResponeHandler parameter in IRequestAdapter to be a RequestOption
</PackageReleaseNotes>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
Expand All @@ -31,7 +31,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
<PackageReference Include="Microsoft.Kiota.Abstractions" Version="1.0.0-preview.13" />
<PackageReference Include="Microsoft.Kiota.Abstractions" Version="1.0.0-preview.14" />
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="6.0.0" />
<PackageReference Include="System.Text.Json" Version="6.0.6" />
</ItemGroup>
Expand Down

0 comments on commit a0790e8

Please sign in to comment.