Solid.Http.Core Usage - SOLIDSoftworks/Solid.Http GitHub Wiki

Usage

When working with SolidHttp, you use ISolidHttpClientFactory to create a SolidHttpClient. This SolidHttpClient then creates a SolidHttpRequest. The SolidHttpRequest itself is awaitable so any extension method that returns SolidHttpRequest can be awaited.

Basic example

public class PostsService
{
    private ISolidHttpClientFactory _factory;
    public PostsService(ISolidHttpClientFactory factory)
    {
        // The SolidHttpClientFactory class would be injected here
        _factory = factory;
    }

    public Task<HttpResponseMessage> GetSomethingAsync(CancellationToken cancellationToken)
    {
        var client = _factory.Create();
        var response = await client
            .GetAsync("https://jsonplaceholder.typicode.com/posts", cancellationToken);
        retrun response;
    }
    
}

Intermediate example

public class PostsService
{
    private ISolidHttpClientFactory _factory;
    public PostsService(ISolidHttpClientFactory factory)
    {
        // The SolidHttpClientFactory class would be injected here
        _factory = factory;
    }

    public Task<HttpResponseMessage> EditPostAsync(int id, Post body, CancellationToken cancellationToken)
    {
        var client = _factory.CreateWithBaseAddress("https://jsonplaceholder.typicode.com");
        var json = JsonConvert.SerializeObject(body);
        var content = new StringContent(json, Encoding.UTF8, "application/json");
        var response = await client
            .PutAsync("posts/{id}", cancellationToken)
            .WithContent(content)
            .WithNamedParameter("id", id)
            .ExpectSuccess();
        return respsone;
    }
}