All Projects → loresoft → FluentRest

loresoft / FluentRest

Licence: MIT license
Lightweight fluent wrapper over HttpClient to make REST calls easier

Programming Languages

C#
18002 projects

Projects that are alternatives of or similar to FluentRest

Tiny.restclient
Simpliest Fluent REST client for .NET
Stars: ✭ 158 (+192.59%)
Mutual labels:  httpclient, rest-client
Restclient
🦄 Simple HTTP and REST client for Unity based on Promises, also supports Callbacks! 🎮
Stars: ✭ 675 (+1150%)
Mutual labels:  httpclient, rest-client
HttpClientFactory.Azure.Functions
[Archived] Azure Functions and Azure WebJobs binding extensions for HttpClientFactory. This is no longer the best approach: https://www.tpeczek.com/2019/10/alternative-approach-to-httpclient-in.html
Stars: ✭ 14 (-74.07%)
Mutual labels:  httpclient, httpclientfactory
Rest Client
A tool for automated testing REST API, generating exquisite testing report and REST API documentation.
Stars: ✭ 1,181 (+2087.04%)
Mutual labels:  httpclient, rest-client
RestSharpFramework
Framework for testing RESTful Services with RestSharp and C# HTTP Client
Stars: ✭ 18 (-66.67%)
Mutual labels:  httpclient, rest-client
Fluentlyhttpclient
Http Client for .NET Standard with fluent APIs which are intuitive, easy to use and also highly extensible.
Stars: ✭ 73 (+35.19%)
Mutual labels:  httpclient, rest-client
Httplug
HTTPlug, the HTTP client abstraction for PHP
Stars: ✭ 2,295 (+4150%)
Mutual labels:  httpclient
Rump
REST client for Java that allows for easy configuration and default values. Allows for quick request construction and a huge range of modifications by using response/request interceptors, adjusting default values related to HTTP requests and creating custom instances for when you need multiple API connection setups.
Stars: ✭ 55 (+1.85%)
Mutual labels:  rest-client
Httpclientutil
HttpClient工具类,简单轻松的实现get,post,put和delete请求
Stars: ✭ 165 (+205.56%)
Mutual labels:  httpclient
Curlsharp
CurlSharp - .Net binding and object-oriented wrapper for libcurl.
Stars: ✭ 153 (+183.33%)
Mutual labels:  httpclient
simple-http
抽取一个简单 HTTP 的通用接口,底层实现根据具体引入依赖指定。
Stars: ✭ 38 (-29.63%)
Mutual labels:  httpclient
smart-api-framework
smart api automation framework to support web service api automaton test based on testng and httpclient
Stars: ✭ 15 (-72.22%)
Mutual labels:  httpclient
rawhttp
Raw HTTP client in Go for complete request control and customization.
Stars: ✭ 100 (+85.19%)
Mutual labels:  httpclient
Yurunhttp
YurunHttp 是开源的 PHP HTTP 客户端,支持链式操作,简单易用。完美支持Curl、Swoole 协程。QQ群:17916227
Stars: ✭ 197 (+264.81%)
Mutual labels:  httpclient
esa-httpclient
An asynchronous event-driven HTTP client based on netty.
Stars: ✭ 82 (+51.85%)
Mutual labels:  httpclient
Book118 Downloader
基于java的book118文档下载器
Stars: ✭ 187 (+246.3%)
Mutual labels:  httpclient
angular-httpclient
Angular 15 Example HttpClient
Stars: ✭ 21 (-61.11%)
Mutual labels:  httpclient
Kreya
Kreya is a GUI client for gRPC and REST APIs with innovative features for environments, authorizations and more.
Stars: ✭ 217 (+301.85%)
Mutual labels:  rest-client
oanda api
A ruby client for the Oanda REST API.
Stars: ✭ 35 (-35.19%)
Mutual labels:  rest-client
acfs
API client for services
Stars: ✭ 13 (-75.93%)
Mutual labels:  rest-client

FluentRest

Lightweight fluent wrapper over HttpClient to make REST calls easier

Build status

NuGet Version

Coverage Status

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 myget.org 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: http://www.myget.org/F/loresoft/

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] FluentClient changed to a light wrapper for HttpClient
  • [Breaking] Removed FluentClient.BaseUri defaults, use HttpClient.BaseAddress instead
  • [Breaking] Removed FluentClient default headers, use HttpClient instead
  • [Breaking] All fluent builder take HttpRequestMessage instead of FluentRequest
  • [Breaking] Removed FluentRequest and FluentResponse classes
  • [Breaking] Removed FluentRequest.Create fluent builder
  • [Breaking] Moved all Fake Response handlers to FluentRest.Fake Nuget Package
  • [Breaking] Removed interceptor support in favor of HttpClientFactory middleware handlers
  • Add support for HttpClientFactory typed client and middleware handlers
  • Add FluentRequest.Factory to support named FluentClient instances
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].