Skip to content

Commit 8566939

Browse files
feat(http client builder): configurable http version/policy via WithVersion, WithVersionPolicy (#83)
* feat(http client builder): configurable http version/policy via `WithVersion`, `WithVersionPolicy` * up readme with dotnet support
1 parent 1f59b8d commit 8566939

File tree

10 files changed

+81
-14
lines changed

10 files changed

+81
-14
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22

33
[_vNext_](https://github.com/sketch7/FluentlyHttpClient/compare/3.8.1...3.9.0) (2020-X-X)
44

5+
## [4.0.0](https://github.com/sketch7/FluentlyHttpClient/compare/3.9.6...4.0.0) (2024-07-23)
6+
7+
### Features
8+
- **http client builder:** configurable http version/policy via `WithVersion`, `WithVersionPolicy`
9+
- **http client builder:** defaults to http version http2.0
10+
11+
### BREAKING CHANGES
12+
13+
- **deps:** now target .net8
14+
515
## [3.9.6](https://github.com/sketch7/FluentlyHttpClient/compare/3.9.5...3.9.6) (2024-06-11)
616

717
### Features

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,6 @@
3838
</PropertyGroup>
3939

4040
<PropertyGroup>
41-
<TargetFramework>netstandard2.0</TargetFramework>
41+
<TargetFramework>net8.0</TargetFramework>
4242
</PropertyGroup>
4343
</Project>

Directory.Packages.props

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
<!-- system -->
77
<PackageVersion Include="System.Data.HashFunction.xxHash" Version="2.0.0" />
88
<!-- microsoft packages -->
9-
<PackageVersion Include="Microsoft.Extensions.Caching.Memory" Version="2.2.0" />
10-
<PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="2.2.0" />
11-
<PackageVersion Include="Microsoft.Extensions.Http" Version="2.2.0" />
12-
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="2.2.0" />
9+
<PackageVersion Include="Microsoft.Extensions.Caching.Memory" Version="8.0.0" />
10+
<PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" />
11+
<PackageVersion Include="Microsoft.Extensions.Http" Version="8.0.0" />
12+
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
1313

1414
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
1515
<PackageVersion Include="Microsoft.Extensions.Logging" Version="8.0.0" />

README.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ Available for [.NET Standard 2.0+](https://docs.microsoft.com/en-gb/dotnet/stand
3030

3131
NOTE: 1.x depends on .NET Standard 1.4+, use that if you need older .NET standard.
3232

33+
## dotnet support
34+
35+
| Version | .NET | Status |
36+
| ------- | ------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
37+
| 1.x | .NET Standard 1.4+ | |
38+
| 2.x | .NET Standard 2 | |
39+
| 3.x | .NET Standard 2 | [![CI](https://github.com/sketch7/FluentlyHttpClient/actions/workflows/dotnet-publish.yml/badge.svg?branch=3.x)](https://github.com/sketch7/FluentlyHttpClient/actions/workflows/dotnet-publish.yml) |
40+
| 4.x | net8 | [![CI](https://github.com/sketch7/FluentlyHttpClient/actions/workflows/dotnet-publish.yml/badge.svg?branch=4.x)](https://github.com/sketch7/FluentlyHttpClient/actions/workflows/dotnet-publish.yml) |
41+
3342
### NuGet
3443
```
3544
PM> Install-Package FluentlyHttpClient
@@ -135,7 +144,7 @@ Fluent request API (request builder) allows to create more complex requests and
135144
var httpClient = fluentHttpClientFactory.Get(identifier: "platform");
136145

137146
// HTTP GET + return response and deserialize result (fluent API)
138-
FluentHttpResponse<Hero> response =
147+
FluentHttpResponse<Hero> response =
139148
await httpClient.CreateRequest("/api/heroes/azmodan")
140149
.ReturnAsResponse<Hero>(); // return with response
141150
@@ -410,7 +419,7 @@ fluentHttpClientFactory.CreateBuilder("platform")
410419
WarnThreshold = TimeSpan.Zero
411420
}) // register a middleware using extension method
412421
```
413-
As a best practice, it's best to provide an extension method for usage such as `UseTimer`
422+
As a best practice, it's best to provide an extension method for usage such as `UseTimer`
414423
especially when it has any arguments (options), as it won't be convenient to use.
415424

416425

@@ -455,7 +464,7 @@ public static class FluentHttpRequestBuilderExtensions
455464
```
456465

457466
#### Extending Request Builder/Client Builder headers
458-
In order to extend headers for both `FluentHttpClientBuilder` and `FluentHttpRequestBuilder`, the best approach would be to extend on
467+
In order to extend headers for both `FluentHttpClientBuilder` and `FluentHttpRequestBuilder`, the best approach would be to extend on
459468
`IFluentHttpHeaderBuilder<T>`, this way it will be available for both. See example below.
460469

461470
```cs

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@sketch7/fluently-http-client",
3-
"version": "3.9.6",
3+
"version": "4.0.0",
44
"versionSuffix": "",
55
"scripts": {
66
"pack": "bash ./tools/pack.sh",

src/FluentlyHttpClient.Entity/FluentlyHttpClient.Entity.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netstandard2.0</TargetFramework>
4+
<!-- <TargetFramework>netstandard2.0</TargetFramework> -->
55
<Description>Fluent Http Client with a fluent APIs which are intuitive, easy to use and also highly extensible.</Description>
66
<PackageTags>$(PackageTags) ef entityframework</PackageTags>
77
</PropertyGroup>

src/FluentlyHttpClient/FluentHttpRequestBuilder.cs

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.Extensions.Primitives;
2+
using System.Net;
23
using System.Text.RegularExpressions;
34

45
namespace FluentlyHttpClient;
@@ -8,6 +9,18 @@ namespace FluentlyHttpClient;
89
/// </summary>
910
public class FluentHttpRequestBuilder : IFluentHttpHeaderBuilder<FluentHttpRequestBuilder>, IFluentHttpMessageItems
1011
{
12+
internal static Version _defaultVersion = HttpVersion.Version20;
13+
14+
/// <summary>
15+
/// Gets the HTTP Version for the Http Request.
16+
/// </summary>
17+
public Version Version { get; private set; } = _defaultVersion;
18+
19+
/// <summary>
20+
/// Gets the HTTP Version for the Http Request.
21+
/// </summary>
22+
public HttpVersionPolicy? VersionPolicy { get; private set; }
23+
1124
/// <summary>
1225
/// Gets the HTTP Method for the Http Request.
1326
/// </summary>
@@ -78,6 +91,26 @@ public FluentHttpRequestBuilder WithMethod(HttpMethod method)
7891
return this;
7992
}
8093

94+
/// <summary>
95+
/// Set request HTTP version with specified version.
96+
/// </summary>
97+
/// <returns>Returns request builder for chaining.</returns>
98+
public FluentHttpRequestBuilder WithVersion(Version version)
99+
{
100+
Version = version;
101+
return this;
102+
}
103+
104+
/// <summary>
105+
/// Set request HTTP version policy with specified policy.
106+
/// </summary>
107+
/// <returns>Returns request builder for chaining.</returns>
108+
public FluentHttpRequestBuilder WithVersionPolicy(HttpVersionPolicy policy)
109+
{
110+
VersionPolicy = policy;
111+
return this;
112+
}
113+
81114
/// <inheritdoc />
82115
public FluentHttpRequestBuilder WithHeader(string key, string value)
83116
{
@@ -300,7 +333,13 @@ public FluentHttpRequest Build()
300333

301334
Uri ??= string.Empty;
302335
var uri = BuildUri(Uri, _queryParams, _queryStringOptions);
303-
var httpRequest = new HttpRequestMessage(HttpMethod, uri);
336+
var httpRequest = new HttpRequestMessage(HttpMethod, uri)
337+
{
338+
Version = Version,
339+
};
340+
if (VersionPolicy.HasValue)
341+
httpRequest.VersionPolicy = VersionPolicy.Value;
342+
304343
if (_httpBody != null)
305344
httpRequest.Content = _httpBody;
306345

@@ -358,7 +397,7 @@ private static string BuildUri(string uri, object? queryParams, QueryStringOptio
358397
if (string.IsNullOrEmpty(queryString))
359398
return uri;
360399

361-
if (uri.Contains("?"))
400+
if (uri.Contains('?'))
362401
uri += $"&{queryString}";
363402
else
364403
uri += $"?{queryString}";

src/FluentlyHttpClient/FluentHttpResponse.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ public class FluentHttpResponse : IFluentHttpMessageState
4444
/// </summary>
4545
public HttpResponseMessage Message { get; }
4646

47+
/// <summary>
48+
/// Gets or sets the version of the HTTP response.
49+
/// </summary>
50+
public Version Version
51+
{
52+
get => Message.Version;
53+
set => Message.Version = value;
54+
}
55+
4756
/// <summary>
4857
/// Gets or sets the status code of the HTTP response.
4958
/// </summary>

src/FluentlyHttpClient/FluentlyHttpClient.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netstandard2.0</TargetFramework>
4+
<!-- <TargetFramework>netstandard2.0</TargetFramework> -->
55
<Description>Fluent Http Client with a fluent APIs which are intuitive, easy to use and also highly extensible.</Description>
66
<PackageTags>$(PackageTags)</PackageTags>
77
</PropertyGroup>

test/FluentlyHttpClient.Test.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<PackageReference Include="MessagePack" />
1818
<PackageReference Include="Microsoft.Extensions.DependencyInjection" />
1919
<PackageReference Include="Microsoft.Extensions.Logging" />
20-
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" VersionOverride="8.0.0" />
20+
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" />
2121
<PackageReference Include="MimeTypesMap" />
2222
<PackageReference Include="Serilog.Extensions.Logging" />
2323
<PackageReference Include="Serilog.Sinks.Console" />

0 commit comments

Comments
 (0)