Git Product home page Git Product logo

httpclientsettings's Introduction

HttpClientSettings

.NET

HttpClientSettings is a convention based http endpoint data storage and retrieval mechanism without enforcing how to make the http calls. It is up to the developer what tech the want to use to make the http calls, this is purely for storing and retrieving endpoint data.

The data structure stored in appSettings allow for storing multiple endpoints grouped under a client.

This library removes the pain of coming up with your own storage structure and boilerplate code retrieving endpoint urls, validation on data and replacing parameters in the url.

Usage

In order to use the IOptions to retrieve the stored endpoints the library must be added to the program.

validateSettings is default true. It validates the endpoint values in the appSettings file.

builder.Services.AddHttpClientSettings(builder.Configuration, validateSettings: true);

In the appSettings.json file add a new section called HttpClientSettings with your endpoints grouped under a client. Please ensure each Client Name is unique as well as the endpoint name under each client.

Passing parameters to the url is achieved by specifying the placeholders in the url with an index as you would with normal string.Format in C#

"HttpClientSettings": {
    "Clients": [
      {
        "Name": "Pokemon",
        "BaseUri": "https://pokeapi.co",
        "Endpoints": [
          {
            "Uri": "api/v2/pokemon?offset={0}&limit={1}",
            "Name": "GetPagedPokemon"
          },
          {
            "Uri": "api/v2/pokemon/{0}",
            "Name": "GetPokemon"
          }
        ]
      }
    ]
  }

In order to use the stored settings you use dependency injection to get the IOptions

Below is an example service. To have a better understanding you can view and run the example project HttpClientSettings.Example in source code

public class PokemonService : IPokemonService
{
    private readonly IHttpClientFactory _httpClientFactory;
    private readonly HttpClientAppSettings _httpClientOptions;

    public PokemonService(IHttpClientFactory httpClientFactory,
        IOptions<HttpClientAppSettings> httpClientOptions)
    {
        _httpClientFactory = httpClientFactory;
        _httpClientOptions = httpClientOptions.Value;
    }

    public async Task<PagedPokemon> GetPagedPokemon(int take = 10, int skip = 0, CancellationToken cancellationToken = default)
    {
        var client = _httpClientFactory.CreateClient();

        var endpoint = _httpClientOptions.GetEndpoint("Pokemon", "GetPagedPokemon", skip, take);

        return await client.GetFromJsonAsync<PagedPokemon>(endpoint.FullUri, cancellationToken)
            ?? new PagedPokemon();
    }

    public async Task<Pokemon> GetPokemon(int id, CancellationToken cancellationToken = default)
    {
        var client = _httpClientFactory.CreateClient();

        var endpoint = _httpClientOptions.GetEndpoint("Pokemon", "GetPokemon", id);        

        return await client.GetFromJsonAsync<Pokemon>(endpoint.FullUri, cancellationToken)
            ?? new Pokemon();
    }
}

httpclientsettings's People

Contributors

nield avatar

Watchers

 avatar

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.