Skip to content

Commit

Permalink
Add Version and HttpVersionPolicy support for HttpRequestMessage (#1800)
Browse files Browse the repository at this point in the history
* Add Version and HttpVersionPolicy support for HttpRequestMessage

* Add one more #if NET6_0_OR_GREATER

* Fixes a build error because of VS automatic code

* Fixes test verification error

* Try the test again

* Fix API Tests

---------

Co-authored-by: Chris Pulman <chris.pulman@yahoo.com>
  • Loading branch information
infofromca and ChrisPulman committed Sep 15, 2024
1 parent 89e0a84 commit e6f075e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Refit.Tests/API/ApiApprovalTests.Refit.DotNet6_0.verified.txt
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,8 @@ namespace Refit
public System.Collections.Generic.Dictionary<string, object>? HttpRequestMessageOptions { get; set; }
public Refit.IUrlParameterFormatter UrlParameterFormatter { get; set; }
public Refit.IUrlParameterKeyFormatter UrlParameterKeyFormatter { get; set; }
public System.Version Version { get; set; }
public System.Net.Http.HttpVersionPolicy VersionPolicy { get; set; }
}
public static class RequestBuilder
{
Expand Down
2 changes: 2 additions & 0 deletions Refit.Tests/API/ApiApprovalTests.Refit.DotNet8_0.verified.txt
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,8 @@ namespace Refit
public System.Collections.Generic.Dictionary<string, object>? HttpRequestMessageOptions { get; set; }
public Refit.IUrlParameterFormatter UrlParameterFormatter { get; set; }
public Refit.IUrlParameterKeyFormatter UrlParameterKeyFormatter { get; set; }
public System.Version Version { get; set; }
public System.Net.Http.HttpVersionPolicy VersionPolicy { get; set; }
}
public static class RequestBuilder
{
Expand Down
7 changes: 7 additions & 0 deletions Refit/RefitSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Runtime.Serialization;
Expand Down Expand Up @@ -127,6 +128,12 @@ public Func<
/// Optional Key-Value pairs, which are displayed in the property <see cref="HttpRequestMessage.Properties"/>.
/// </summary>
public Dictionary<string, object>? HttpRequestMessageOptions { get; set; }

#if NET6_0_OR_GREATER
public Version Version { get; set; } = HttpVersion.Version11;

public System.Net.Http.HttpVersionPolicy VersionPolicy { get; set; } = HttpVersionPolicy.RequestVersionOrLower;
#endif
}

/// <summary>
Expand Down
12 changes: 11 additions & 1 deletion Refit/RequestBuilderImplementation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,9 @@ bool paramsContainsCancellationToken
AddHeadersToRequest(headersToAdd, ret);
AddPropertiesToRequest(restMethod, ret, paramList);
#if NET6_0_OR_GREATER
AddVersionToRequest(ret);
#endif
// NB: The URI methods in .NET are dumb. Also, we do this
// UriBuilder business so that we preserve any hardcoded query
// parameters as well as add the parameterized ones.
Expand Down Expand Up @@ -1032,6 +1034,14 @@ void AddPropertiesToRequest(RestMethodInfoInternal restMethod, HttpRequestMessag
#endif
}

void AddVersionToRequest(HttpRequestMessage ret)
{
#if NET6_0_OR_GREATER
ret.Version = settings.Version;
ret.VersionPolicy = settings.VersionPolicy;
#endif
}

IEnumerable<KeyValuePair<string, string?>> ParseQueryParameter(
object? param,
ParameterInfo parameterInfo,
Expand Down

0 comments on commit e6f075e

Please sign in to comment.