Coder Social home page Coder Social logo

restack's Introduction

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 and forked from Ryan Nowak work on Kickr.

Its implementation is based on:

Example of the refit / Polly support

  1. Define an interface for your rest service.
    public interface IGeoApi
    {
        [Get("/regions")]
        Task<IEnumerable<Region>> GetRegionsAsync();
    }
  2. Configure the URL for your service and add some http request headers and policies.
    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<IGeoApi>("https://geo.api.gouv.fr")
                .AddRestackHeaders<IGeoApi>(o => o.Headers.Add("api-key", "xxxxx-xxx-xxxxxxxx"))
                .AddRestackPolicy<IGeoApi>(b => b.RetryAsync())
                .AddRestackPolicy<IGeoApi>(b => b.CircuitBreakerAsync(1, TimeSpan.FromSeconds(5)));
    }
  3. Consume the interface via RestClient<T>.
    public class HomeController : Controller
    {
        private readonly IGeoApi _geoApi;
    
        public HomeController(IRestClient<IGeoApi> geoApiClient)
        {
            _geoApi = geoApiClient.Client;
        }
    
        [HttpGet("/")]
        public async Task<IActionResult> 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".
    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"));
    }
  2. Consume the HttpClient.
    public class HomeController : Controller
    {
        public async Task<IActionResult> Index([HttpClientName("github")]HttpClient client)
        {
            var response = await client.GetAsync("https://api.github.com/users/lecaillon");
            ...
        }
    }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.