noamyogev84 / HttpClient.Abstractions

An abstraction layer over System.Net.Http.HttpClient implementation.
https://www.nuget.org/packages/HttpClient.Abstractions/
4 stars 1 forks source link
client csharp hacktoberfest http httpclient json net nuget serialization web

HttpClient.Abstractions

Nuget Package: https://www.nuget.org/packages/HttpClient.Abstractions/

HttpClient.Abstractions is a lightweight package providing a complete abstraction over System.Net.Http.HttpClient. The package consists of:

Examples

public class HttpClientConsumer
{
    public string BaseUrl { get; set; }
    private IHttpClient _client;

    [importing constructor]
    public HttpClientConsumer(IHttpClient client) {
        _client = client;
        _client.BaseAddress = BaseUrl;
    }

    //Get   
    public async IEnumerable<CustomrerDto> GetCustomersAsync() {
        IHttpResult result = await _client.GetAsync<IEnumerable<CustomerDto>>("customers");
        return result.Content;
    }

    //Post
    public async void CreateCustomerAsync(CustomerDto customer) {
        IHttpResult result = await _client.PostAsync<CustomerDto>("customers",customer);
        //handle result...
    }

    //Put
    public async void ReplaceCustomerAsync(CustomerUpdateDto customer) {
        IHttpResult result = await _client.PutAsync<CustomerUpdateDto>($"customers/{customer.id}",customer);
    }

    //Patch
    public async void UpdateCustomerAsync(CustomerPatchDoc patchDoc) {
        IHttpResult result = await _client.PatchAsync<CustomerPatchDoc>($"customers/{patchDoc.id}", patchDoc);
    }

    //Delete
    public async void RemoveCustomerAsync(CustomerDto customer) {
        IHttpResult result = await _client.DeleteAsync($"customers/{customer.id}");
    }
}