Lightweight fluent wrapper over HttpClient to make REST calls easier
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 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
- 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
Create a form post request
varclient=newHttpClient();client.BaseAddress=newUri("http://httpbin.org/",UriKind.Absolute);varresult=awaitclient.PostAsync<EchoResult>(b =>b.AppendPath("Project").AppendPath("123").FormValue("Test","Value").FormValue("key","value").QueryString("page",10));Custom authorization header
varclient=newHttpClient();client.BaseAddress=newUri("https://api.github.com/",UriKind.Absolute);varresult=awaitclient.GetAsync<Repository>(b =>b.AppendPath("repos").AppendPath("loresoft").AppendPath("FluentRest").Header(h =>h.Authorization("token","7ca...")));Use with HttpClientFactory and Retry handler
varservices=newServiceCollection();services.AddSingleton<IContentSerializer,JsonContentSerializer>();services.AddHttpClient<GithubClient>(c =>{c.BaseAddress=newUri("https://api.github.com/");c.DefaultRequestHeaders.Add("Accept","application/vnd.github.v3+json");c.DefaultRequestHeaders.Add("User-Agent","GitHubClient");}).AddHttpMessageHandler(()=>newRetryHandler());varserviceProvider=services.BuildServiceProvider();varclient=serviceProvider.GetService<GithubClient>();varresult=awaitclient.GetAsync<Repository>(b =>b.AppendPath("repos").AppendPath("loresoft").AppendPath("FluentRest"));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 HTTP responses can be stored in the following message stores. To create your own message store, implement IFakeMessageStore.
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
varserializer=newJsonContentSerializer();// use memory store by defaultvarfakeHttp=newFakeMessageHandler();varhttpClient=newHttpClient(fakeHttp,true);httpClient.BaseAddress=newUri("https://api.github.com/",UriKind.Absolute);varclient=newFluentClient(httpClient,serializer);// make HTTP callvarresult=awaitclient.GetAsync<Repository>(b =>b.AppendPath("repos").AppendPath("loresoft").AppendPath("FluentRest").Header(h =>h.Authorization("token","7ca...")));Use fake handlers with HttpClientFactory
varservices=newServiceCollection();services.AddSingleton<IContentSerializer,JsonContentSerializer>();services.AddSingleton<IFakeMessageStore>(s =>MemoryMessageStore.Current);services.AddHttpClient<EchoClient>(c =>c.BaseAddress=newUri("http://httpbin.org/")).AddHttpMessageHandler(s =>newFakeMessageHandler(s.GetService<IFakeMessageStore>(),FakeResponseMode.Fake));varserviceProvider=services.BuildServiceProvider();// fake response objectvarresponse=newEchoResult();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 responseMemoryMessageStore.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)));varclient=serviceProvider.GetService<EchoClient>();varresult=awaitclient.PostAsync<EchoResult>(b =>b.AppendPath("post").FormValue("Test","Fake").FormValue("key","value").QueryString("page",10)).ConfigureAwait(false);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.
varserializer=newJsonContentSerializer();// use file store to load from diskvarfakeStore=newFileMessageStore();fakeStore.StorePath=@".\GitHub\Responses";varfakeHttp=newFakeMessageHandler(fakeStore,FakeResponseMode.Capture);varhttpClient=newHttpClient(fakeHttp,true);httpClient.BaseAddress=newUri("https://api.github.com/",UriKind.Absolute);varclient=newFluentClient(httpClient,serializer);varresult=awaitclient.GetAsync<Repository>(b =>b.AppendPath("repos").AppendPath("loresoft").AppendPath("FluentRest").Header(h =>h.Authorization("token","7ca...")));Use captured response
varserializer=newJsonContentSerializer();// use file store to load from diskvarfakeStore=newFileMessageStore();fakeStore.StorePath=@".\GitHub\Responses";varfakeHttp=newFakeMessageHandler(fakeStore,FakeResponseMode.Fake);varhttpClient=newHttpClient(fakeHttp,true);httpClient.BaseAddress=newUri("https://api.github.com/",UriKind.Absolute);varclient=newFluentClient(httpClient,serializer);varresult=awaitclient.GetAsync<Repository>(b =>b.AppendPath("repos").AppendPath("loresoft").AppendPath("FluentRest").Header(h =>h.Authorization("token","7ca...")));- [Breaking] Remove netstandard1.3 support
- add overload for generic AppendPath
- update dependence packages
- [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