General purpose, simple but useful HttpClient wrapper for .NET & Xamarin/Mono
PM> Install-Package Httwrap
IHttwrapConfiguration configuration = new HttwrapConfiguration("http://localhost:9000/");
IHttwrapClient _httwrap = new HttwrapClient(configuration);
IHttwrapResponse<Product> response = await _httwrap.GetAsync<Product>("api/products/1");
Dump(response.Data);
Dump(response.StatusCode);
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);
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();*/
Product product = new Product{ Name= "Product A", Quantity = 3 };
IHttwrapResponse response = await _httwrap.PostAsync<Product>("api/products",product);
Dump(response.StatusCode);
Product product = new Product{ Name= "Product A", Quantity = 3 };
IHttwrapResponse response = await _httwrap.PutAsync<Product>("api/products/1",product);
Dump(response.StatusCode);
Product product = new Product{ Name= "Product A", Quantity = 3};
IHttwrapResponse response = await _httwrap.PatchAsync<Product>("api/products/1",product);
Dump(response.StatusCode);
IHttwrapResponse response = await _httwrap.DeleteAsync("api/products/1");
Dump(response.StatusCode);
IHttwrapResponse<List<Product>> response =
await _httwrap.GetAsync<List<Product>>("api/products", (statusCode, body) =>
{
_logger.Error("Body :{0}, StatusCode :{1}", body, statusCode);
});
IHttwrapConfiguration configuration = new HttwrapConfiguration("http://localhost:9000/")
{
Credentials = new BasicAuthCredentials("user", "s3cr3t")
};
IHttwrapClient _httwrap = new HttwrapClient(configuration);
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);
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());
If you encounter a bug, performance issue, or malfunction, please add an Issue with steps on how to reproduce the problem.
- Add more tests
- Add more documentation
Code and documentation are available according to the MIT License (see LICENSE).
@ziλasal