MyNihongo / FluentHttp

Fluent wrapper around IHttpClientFactory

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Version Nuget downloads

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. for Timeout.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>();

About

Fluent wrapper around IHttpClientFactory

License:MIT License


Languages

Language:C# 100.0%