MyNihongo.FluentHttp
1.1.2
Prefix Reserved
dotnet add package MyNihongo.FluentHttp --version 1.1.2
NuGet\Install-Package MyNihongo.FluentHttp -Version 1.1.2
<PackageReference Include="MyNihongo.FluentHttp" Version="1.1.2" />
paket add MyNihongo.FluentHttp --version 1.1.2
#r "nuget: MyNihongo.FluentHttp, 1.1.2"
// Install MyNihongo.FluentHttp as a Cake Addin #addin nuget:?package=MyNihongo.FluentHttp&version=1.1.2 // Install MyNihongo.FluentHttp as a Cake Tool #tool nuget:?package=MyNihongo.FluentHttp&version=1.1.2
FluentHttp
Fluent wrapper around IHttpClientFactory
Install a NuGet package MyNihongo.FluentHttp
.
Configuration
Add a section to IConfiguration
{
"FluentHttp": {
"BaseAddress": "https://jsonplaceholder.typicode.com",
"NtlmEnabled": false,
"Timeout": 100
}
}
Timeout
in seconds. forTimeout.Infinite
pass -1.
Register a service
using MyNihongo.FluentHttp;
services.AddFluentHttp();
// Or optionally configure the HTTP client
services.AddFluentHttp((services, httpClient) =>
{
// configure
});
HTTP methods
To optimize JSON serialization JsonTypeInfo<T>
can be supplied for all methods. More info about these types here.
In further examples a variable IFluentHttp fluentHttp
will be used.
GetJsonAsync
Gets a JSON stream.
[JsonSerializable(typeof(RecordContext[]))]
internal partial class RecordContext : JsonSerializerContext {}
public sealed record RecordContext
{
public int Id { get; set; }
}
// Get the model
var models = await fluentHttp
.AppendPathSegment("example")
.GetJsonAsync(RecordContext.Default.RecordArray, ct);
// When an API may return non-JSON values (e.g. `""`) this method can be used in order to ignore parsing exceptions
fluentHttp.GetJsonOrDefaultAsync(RecordContext.Default.RecordArray, ct);
PostJsonAsync
Posts a JSON model and gets the JSON response.
[JsonSerializable(typeof(Request))]
internal partial class RequestContext : JsonSerializerContext {}
[JsonSerializable(typeof(Response))]
internal partial class ResponseContext : JsonSerializerContext {}
var req = new Request
{
Data = "example"
};
var response = await fluentHttp
.AppendPathSegment("example")
.PostJsonAsync(req, RequestContext.Default.Request, ResponseContext.Default.Response, ct);
// When an API may return non-JSON values (e.g. `""`) this method can be used in order to ignore parsing exceptions
fluentHttp.PostJsonOrDefaultAsync(req, RequestContext.Default.Request, ResponseContext.Default.Response, ct);
Fluent extensions
Fluent extensions supply additional parameters for the main HTTP methods.
AppendPathSegment
Appends a new section to the request URI.
// get from https://jsonplaceholder.typicode.com/posts
var result = await fluentHttp
.AppendPathSegment("posts")
.GetJsonAsync<PostRecord>();
AppendPathSegments
Appends multiple sections to the request URI.
// get from https://jsonplaceholder.typicode.com/posts/1/comments
var result = await fluentHttp
.AppendPathSegment("posts", "1", "comments")
.GetJsonAsync<PostCommentRecord>();
AppendParameter
Appends a URL parameter
// get from https://jsonplaceholder.typicode.com/posts?value=123
var result = await fluentHttp
.AppendPathSegment("posts")
.AppendParameter("value", "123")
.GetJsonAsync<PostCommentRecord>();
WithHeader
Appends a header to the request.
var result = await fluentHttp
.AppendPathSegment("posts")
.WithHeader("my-header", "value")
.GetJsonAsync<PostRecord>();
Basic authentication
Append the authentication header for the basic authentication
var result = await fluentHttp
.AppendPathSegment("posts")
.WithBasicAuth("username", "strong password")
.GetJsonAsync<PostRecord>();
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- Microsoft.Extensions.Configuration.Abstractions (>= 7.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0.0)
- Microsoft.Extensions.Http (>= 7.0.0)
- Microsoft.Extensions.ObjectPool (>= 7.0.2)
- System.Text.Json (>= 7.0.1)
-
net6.0
- Microsoft.Extensions.Configuration.Abstractions (>= 7.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0.0)
- Microsoft.Extensions.Http (>= 7.0.0)
- Microsoft.Extensions.ObjectPool (>= 7.0.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.