An open API service indexing awesome lists of open source software.

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.

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");
...
}
}
```