Advertisements

A fluent http library for .net standard

A fluent http library for .net standard

January 10, 2018 Packages 0

TLDR

There is a new fluent http library out there called Solid.Http. Get it on nuget.

Introducing Solid.Http

We’re proud to announce our fluent http library for .net standard. Yeah, we are aware that there are multiple http libraries out there like RestSharp, unirest, and EasyHttp and although we admire those libraries and what they do, we didn’t feel that they worked for what we wanted to do.

Core values

We had multiple goals when designing Solid.Http.

  1. Had to be async native
  2. Had to support more than just JSON
  3. Had to be extremely extendable

We had worked with similar libraries in our other jobs that were written with JSON and REST in mind, which is fine if that’s all you need. However, the needs changed and the library we had didn’t adapt well.

There were aspects of the older libraries that we liked. The older library was async native; you could just await anything. We wanted to keep that but make it a bit better.

Examples

Adding Solid.Http to you AspNetCore project is easy.

First, get Solid.Http on nuget.

Then add the initialize the Solid.Http.

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddSolidHttp();
    }
    public void Configure(IApplicationBuilder builder)
    {
        // Configuration removed for brevity
    }
}

But using it is easier well… not quite, but almost

[Route("[controller]")]
public class JsonPlaceholderPostsController : Controller
{
    private SolidHttpClient _client;

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

    [HttpGet]
    public async Task GetAsync()
    {
        var schema = new
        {
            Id = string.Empty,
            UserId = string.Empty,
            Title = string.Empty,
            Body = string.Empty
        };
        var posts = await _client
            .GetAsync("posts")
            .AsMany(schema);
        return Ok(posts);
    }

    [HttpGet("{id}")]
    public async Task GetAsync(string id)
    {
        var schema = new
        {
            Id = string.Empty,
            UserId = string.Empty,
            Title = string.Empty,
            Body = string.Empty
        };
        var post = await _client
            .GetAsync("posts/{id}")
            .WithNamedParameter("id", id)
            .As(schema);
        return Ok(post);
    }
}

That’s about it

It’s simple to use and simple to extend. Check out the repository wiki.

Advertisements