FluentRest
Lightweight fluent wrapper over HttpClient to make REST calls easier
Install / Use
/learn @loresoft/FluentRestREADME
FluentRest
Lightweight fluent wrapper over HttpClient to make REST calls easier
Download
The FluentRest library is available on nuget.org via package name FluentRest.
To install FluentRest, run the following command in the Package Manager Console
PM> Install-Package FluentRest
More information about NuGet package available at https://nuget.org/packages/FluentRest
Development Builds
Development builds are available on the feedz.io feed. A development build is promoted to the main NuGet feed when it's determined to be stable.
In your Package Manager settings add the following package source for development builds: https://f.feedz.io/loresoft/open/nuget/index.json
Features
- Fluent request building
- Fluent form data building
- Automatic deserialization of response content
- Plugin different serialization
- Fake HTTP responses for testing
- Support HttpClientFactory typed client and middleware handlers
Fluent Request
Create a form post request
var client = new HttpClient();
client.BaseAddress = new Uri("http://httpbin.org/", UriKind.Absolute);
var result = await client.PostAsync<EchoResult>(b => b
.AppendPath("Project")
.AppendPath("123")
.FormValue("Test", "Value")
.FormValue("key", "value")
.QueryString("page", 10)
);
Custom authorization header
var client = new HttpClient();
client.BaseAddress = new Uri("https://api.github.com/", UriKind.Absolute);
var result = await client.GetAsync<Repository>(b => b
.AppendPath("repos")
.AppendPath("loresoft")
.AppendPath("FluentRest")
.Header(h => h.Authorization("token", "7ca..."))
);
Use with HttpClientFactory and Retry handler
var services = new ServiceCollection();
services.AddSingleton<IContentSerializer, JsonContentSerializer>();
services.AddHttpClient<GithubClient>(c =>
{
c.BaseAddress = new Uri("https://api.github.com/");
c.DefaultRequestHeaders.Add("Accept", "application/vnd.github.v3+json");
c.DefaultRequestHeaders.Add("User-Agent", "GitHubClient");
})
.AddHttpMessageHandler(() => new RetryHandler());
var serviceProvider = services.BuildServiceProvider();
var client = serviceProvider.GetService<GithubClient>();
var result = await client.GetAsync<Repository>(b => b
.AppendPath("repos")
.AppendPath("loresoft")
.AppendPath("FluentRest")
);
Fake Response
FluentRest.Fake package adds the ability to fake an HTTP responses by using a custom HttpClientHandler. Faking the HTTP response allows creating unit tests without having to make the actual HTTP call.
To install FluentRest.Fake, run the following command in the Package Manager Console
PM> Install-Package FluentRest.Fake
Fake Response Stores
Fake HTTP responses can be stored in the following message stores. To create your own message store, implement IFakeMessageStore.
MemoryMessageStore
The memory message store allows composing a JSON response in the unit test. Register the responses on the start of the unit test.
Register a fake response by URL.
MemoryMessageStore.Current.Register(b => b
.Url("https://api.github.com/repos/loresoft/FluentRest")
.StatusCode(HttpStatusCode.OK)
.ReasonPhrase("OK")
.Content(c => c
.Header("Content-Type", "application/json; charset=utf-8")
.Data(responseObject) // object to be JSON serialized
)
);
Use the fake response in a unit test
var serializer = new JsonContentSerializer();
// use memory store by default
var fakeHttp = new FakeMessageHandler();
var httpClient = new HttpClient(fakeHttp, true);
httpClient.BaseAddress = new Uri("https://api.github.com/", UriKind.Absolute);
var client = new FluentClient(httpClient, serializer);
// make HTTP call
var result = await client.GetAsync<Repository>(b => b
.AppendPath("repos")
.AppendPath("loresoft")
.AppendPath("FluentRest")
.Header(h => h.Authorization("token", "7ca..."))
);
Use fake handlers with HttpClientFactory
var services = new ServiceCollection();
services.AddSingleton<IContentSerializer, JsonContentSerializer>();
services.AddSingleton<IFakeMessageStore>(s => MemoryMessageStore.Current);
services
.AddHttpClient<EchoClient>(c => c.BaseAddress = new Uri("http://httpbin.org/"))
.AddHttpMessageHandler(s => new FakeMessageHandler(s.GetService<IFakeMessageStore>(), FakeResponseMode.Fake));
var serviceProvider = services.BuildServiceProvider();
// fake response object
var response = new EchoResult();
response.Url = "http://httpbin.org/post?page=10";
response.Headers["Accept"] = "application/json";
response.QueryString["page"] = "10";
response.Form["Test"] = "Fake";
response.Form["key"] = "value";
// setup fake response
MemoryMessageStore.Current.Register(b => b
.Url("http://httpbin.org/post?page=10")
.StatusCode(HttpStatusCode.OK)
.ReasonPhrase("OK")
.Content(c => c
.Header("Content-Type", "application/json; charset=utf-8")
.Data(response)
)
);
var client = serviceProvider.GetService<EchoClient>();
var result = await client.PostAsync<EchoResult>(b => b
.AppendPath("post")
.FormValue("Test", "Fake")
.FormValue("key", "value")
.QueryString("page", 10)
).ConfigureAwait(false);
FileMessageStore
The file message store allows saving an HTTP call response on the first use. You can then use that saved response for all future unit test runs.
Configure the FluentRest to capture response.
var serializer = new JsonContentSerializer();
// use file store to load from disk
var fakeStore = new FileMessageStore();
fakeStore.StorePath = @".\GitHub\Responses";
var fakeHttp = new FakeMessageHandler(fakeStore, FakeResponseMode.Capture);
var httpClient = new HttpClient(fakeHttp, true);
httpClient.BaseAddress = new Uri("https://api.github.com/", UriKind.Absolute);
var client = new FluentClient(httpClient, serializer);
var result = await client.GetAsync<Repository>(b => b
.AppendPath("repos")
.AppendPath("loresoft")
.AppendPath("FluentRest")
.Header(h => h.Authorization("token", "7ca..."))
);
Use captured response
var serializer = new JsonContentSerializer();
// use file store to load from disk
var fakeStore = new FileMessageStore();
fakeStore.StorePath = @".\GitHub\Responses";
var fakeHttp = new FakeMessageHandler(fakeStore, FakeResponseMode.Fake);
var httpClient = new HttpClient(fakeHttp, true);
httpClient.BaseAddress = new Uri("https://api.github.com/", UriKind.Absolute);
var client = new FluentClient(httpClient, serializer);
var result = await client.GetAsync<Repository>(b => b
.AppendPath("repos")
.AppendPath("loresoft")
.AppendPath("FluentRest")
.Header(h => h.Authorization("token", "7ca..."))
);
Change Log
Version 6.0
- [Breaking] Remove netstandard1.3 support
- add overload for generic AppendPath
- update dependence packages
Version 5.0
- [Breaking] Major refactor to support HttpClientFactory
- [Breaking]
FluentClientchanged to a light wrapper forHttpClient - [Breaking] Removed
FluentClient.BaseUridefaults, useHttpClient.BaseAddressinstead - [Breaking] Removed
FluentClientdefault headers, useHttpClientinstead - [Breaking] All fluent builder take
HttpRequestMessageinstead ofFluentRequest - [Breaking] Removed
FluentRequestandFluentResponseclasses - [Breaking] Removed
FluentRequest.Createfluent builder - [Breaking] Moved all Fake Response handlers to
FluentRest.FakeNuget Package - [Breaking] Removed interceptor support in favor of HttpClientFactory middleware handlers
- Add support for HttpClientFactory typed client and middleware handlers
- Add
FluentRequest.Factoryto support named FluentClient instances
Related Skills
node-connect
353.3kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
111.7kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
353.3kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
353.3kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
