Skip to content

General purpose, simple but useful HttpClient wrapper for .NET & Xamarin/Mono

License

Notifications You must be signed in to change notification settings

bugthesystem/Httwrap

Repository files navigation

Httwrap

General purpose, simple but useful HttpClient wrapper for .NET & Xamarin/Mono

Build statusCoverage Status

How to use

Install

PM> Install-Package Httwrap

Init

  IHttwrapConfiguration configuration = new HttwrapConfiguration("http://localhost:9000/");
  IHttwrapClient _httwrap = new HttwrapClient(configuration);

GET

Basic

IHttwrapResponse<Product> response = await _httwrap.GetAsync<Product>("api/products/1");
Dump(response.Data);
Dump(response.StatusCode);

With QueryString

It supports DataMember and IgnoreDataMember attributes.

/*
public class FilterRequest
{
  [DataMember(Name = "cat")]
  public string Category { get; set; }
  
  public int NumberOfItems { get; set; }
}
*/

var payload = new FilterRequest
{
  Category = "Shoes",
  NumberOfItems = 10
};

//Url: api/test?cat=Shoes&NumberOfItems=10
IHttwrapResponse<List<Product>> response =
                            await _client.GetAsync<List<Product>>("api/test", payload);

Dump(response.Data);
Dump(response.StatusCode);

Serialize response

IHttwrapResponse response = await _httwrap.GetAsync("api/products");
List<Product> values = response.ResultAs<List<Product>>();
Dump(response.StatusCode);

/* ResultAs<T>() extension method uses Newtonsoft.Json serializer by default.  
To use your own serializer set JExtensions.Serializer = new YourCustomSerializerImpl();*/

POST

Product product = new Product{ Name= "Product A", Quantity = 3 };
IHttwrapResponse response = await _httwrap.PostAsync<Product>("api/products",product);
Dump(response.StatusCode);

PUT

Product product = new Product{ Name= "Product A", Quantity = 3 };
IHttwrapResponse response = await _httwrap.PutAsync<Product>("api/products/1",product);
Dump(response.StatusCode);

PATCH

Product product = new Product{ Name= "Product A", Quantity = 3};
IHttwrapResponse response = await _httwrap.PatchAsync<Product>("api/products/1",product);
Dump(response.StatusCode);

DELETE

IHttwrapResponse response = await _httwrap.DeleteAsync("api/products/1");
Dump(response.StatusCode);

Error Handler

IHttwrapResponse<List<Product>> response =
      await _httwrap.GetAsync<List<Product>>("api/products", (statusCode, body) =>
      {
        _logger.Error("Body :{0}, StatusCode :{1}", body, statusCode);
      });

Auth

Basic Credentials

IHttwrapConfiguration configuration = new HttwrapConfiguration("http://localhost:9000/")
{
  Credentials = new BasicAuthCredentials("user", "s3cr3t")
};
IHttwrapClient _httwrap = new HttwrapClient(configuration);

OAuth Credentials

Use existing token

IHttwrapConfiguration configuration = new HttwrapConfiguration("http://localhost:9000/")
{
  Credentials = new OAuthCredentials("token")
};
IHttwrapClient _httwrap = new HttwrapClient(configuration);

Use Username / password to get token from edpoint

IHttwrapConfiguration configuration = new HttwrapConfiguration("http://localhost:9000/")
{
  Credentials = new OAuthCredentials("us3r", "p4ssw0rd", BaseAddress + "/token")
};
IHttwrapClient _httwrap = new HttwrapClient(configuration);

Req/Res Interceptor

public class DummyInterceptor : IHttpInterceptor
{
    private readonly IHttwrapClient _client;

    public void OnRequest(HttpRequestMessage request)
    {
    }

   public void OnResponse(HttpRequestMessage request, HttpResponseMessage response)
   {
      response.StatusCode = HttpStatusCode.Accepted;
   }
}
client.AddInterceptor(new DummyInterceptor());

Bugs

If you encounter a bug, performance issue, or malfunction, please add an Issue with steps on how to reproduce the problem.

TODO

  • Add more tests
  • Add more documentation

License

Code and documentation are available according to the MIT License (see LICENSE).

@ziλasal

About

General purpose, simple but useful HttpClient wrapper for .NET & Xamarin/Mono

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages