Skip to content

Latest commit

 

History

History
113 lines (97 loc) · 3.64 KB

README.md

File metadata and controls

113 lines (97 loc) · 3.64 KB

Solid.Http License Build status Solid.Http on nuget

Solid.Http is a library to simplify http calls in C# for .net standard 2.0. It's designed to be async and fully extendable.

Packages

Basic usage

The basic package is Solid.Http which includes Solid.Http.Core and Solid.Http.Json.

> dotnet add package Solid.Http

Initialization

Adding Solid.Http is quite simple when you just need the fluent interface and JSON.

public void ConfigureServices(IServiceCollection services)
{
    services.AddSolidHttp();
}

Global event handlers

You can also register event handlers during initialization.

public void ConfigureServices(IServiceCollection services)
{
    services
        .AddSolidHttp(builder => 
        {
            builder
                .Configure(options =>
                {
                    options.OnClientCreated((services, client) =>
                    {
                         // your client created handler here.
                    });
                    options.OnRequestCreated((services, request) =>
                    {
                         // your request created handler here.
                    });
                    options.OnHttpRequest(async (services, httpRequest) =>
                    {
                        // your http request handler here.
                    });
                    options.OnHttpResponse(async (services, httpResponse) =>
                    {
                        // your http response handler here.
                    });
                })
            ;
        })
    ;
}

More about event handlers in the important interfaces section of the Solid.Http.Core README.

Usage

You can inject ISolidHttpClientFactory using dependency injection and then use it to create an ISolidHttpClient. ISolidHttpClient is used to create an ISolidHttpRequest which then has a fully async fluent interface for performing an HTTP request.

[Route("[controller]")]
public class PostsController : Controller
{
    private ISolidHttpClient _client;

    public PostsController(ISolidHttpClientFactory factory)
    {
        _client = factory.CreateWithBaseAddress("https://jsonplaceholder.typicode.com");
    }

    [HttpGet]
    public async Task<IActionResult> GetAsync(CancellationToken cancellationToken)
    {
        var posts = await _client
            .GetAsync("posts", cancellationToken)
            .AsMany<Post>()
        ;

        return Ok(posts);
    }

    [HttpPut("{id}")]
    public async Task<IActionResult> PutAsync([FromRoute] int id, [FromBody] Post body)
    {
        await _client
            .PutAsync("posts/{id}")
            .WithNamedParameter("id", id)
            .WithJsonContent(body)
            .ExpectSuccess()
        ;
        return Ok(body);
    }

    class Post
    {
        public int Id { get; set; }
        public int UserId { get; set; }
        public string Title { get; set; }
        public string Body { get; set; }
    }
}