https://github.com/lecaillon/restack
Restack is an easy to declare and inject, autogenerated proxy for an HTTP service.
https://github.com/lecaillon/restack
Last synced: 8 months ago
JSON representation
Restack is an easy to declare and inject, autogenerated proxy for an HTTP service.
- Host: GitHub
- URL: https://github.com/lecaillon/restack
- Owner: lecaillon
- Created: 2017-09-16T21:19:02.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2019-10-13T21:08:52.000Z (over 6 years ago)
- Last Synced: 2025-05-30T23:15:16.105Z (about 1 year ago)
- Language: C#
- Size: 548 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## Restack
Restack is an easy to declare and inject, autogenerated proxy for an HTTP service. The idea is to make a full featured resilient HTTP client. It's inspired by the David Fowler's project [RestSample](https://github.com/davidfowl/RestSample) and forked from Ryan Nowak work on [Kickr](https://github.com/glennc/Kickr).
Its implementation is based on:
- [refit](https://github.com/paulcbetts/refit)
- [Polly](https://github.com/App-vNext/Polly)
- [consul](https://github.com/hashicorp/consul)
### Example of the refit / Polly support
1. Define an interface for your rest service.
````csharp
public interface IGeoApi
{
[Get("/regions")]
Task> GetRegionsAsync();
}
````
1. Configure the URL for your service and add some http request headers and policies.
```csharp
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddRestack() // configure Restack HttpClientFactory
.AddPolly(); // configure Polly
services.AddRestackGlobalHeaders(o => o.Headers.Add("user-agent", "myagent"));
services.AddRestClient("https://geo.api.gouv.fr")
.AddRestackHeaders(o => o.Headers.Add("api-key", "xxxxx-xxx-xxxxxxxx"))
.AddRestackPolicy(b => b.RetryAsync())
.AddRestackPolicy(b => b.CircuitBreakerAsync(1, TimeSpan.FromSeconds(5)));
}
```
1. Consume the interface via RestClient\.
```csharp
public class HomeController : Controller
{
private readonly IGeoApi _geoApi;
public HomeController(IRestClient geoApiClient)
{
_geoApi = geoApiClient.Client;
}
[HttpGet("/")]
public async Task Get()
{
var regions = await _geoApi.GetRegionsAsync();
return Ok(regions);
}
}
```
### Example of the ASP.NET MVC HttpClient param binding support
1. Configure some http request headers for the "github" named http client".
```csharp
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc()
.AddRestackModelBinder(); // configure Restack MVC
services.AddRestack(); // configure Restack HttpClientFactory
services.AddRestackGlobalHeaders(o => o.Headers.Add("user-agent", "myagent"));
services.AddRestackHeaders("github", o => o.Headers.Add("Accept", "application/vnd.github.v3+json"));
}
```
1. Consume the HttpClient.
```csharp
public class HomeController : Controller
{
public async Task Index([HttpClientName("github")]HttpClient client)
{
var response = await client.GetAsync("https://api.github.com/users/lecaillon");
...
}
}
```