Jal.HttpClient

Just another library to wrap the HttpClient class


Keywords
License
Apache-2.0
Install
Install-Package Jal.HttpClient -Version 1.3.0

Documentation

Jal.HttpClient NuGet

Just another library to wrap the HttpClient class

How to use?

Get

using (var response = await client.Get("http://httpbin.org/ip").SendAsync())
{
    var content = await response.Message.Content.ReadAsStringAsync();
}

using (var response = await client.Get("http://httpbin.org/ip")
    .WithQueryParameters( x=> { x.Add("x", "x"); x.Add("y","y"); }).SendAsync())
{
    var content = await response.Message.Content.ReadAsStringAsync();
}

using (var response = await httpclientbuilder.Get("http://httpbin.org/ip")
    .WithHeaders(x=> { x.Add("x", "x"); x.Add("y","y"); }).SendAsync())
{
    var content = await response.Message.Content.ReadAsStringAsync();
}

Post

using (var response = await client.Post("http://httpbin.org/post")
    .Json(@"{""message"":""Hello World!!""}").SendAsync())
{

}

using (var response = await client.Post("http://httpbin.org/post")
    .Xml(@"<message>Hello World!!</message>").SendAsync())
{

}

using (var response = await client.Post("http://httpbin.org/post")
    .FormUrlEncoded(new [] {new KeyValuePair<string, string>("message", "Hello World") }).SendAsync())
{

}

Delete

using (var response = await client.Delete("http://httpbin.org/delete").SendAsync())
{

}

Patch, Put, Options and Head

IHttpFluentHandler interface building

Castle Windsor NuGet

var container = new WindsorContainer();

container.AddHttpClient();

var client = container.GetHttpClient();

LightInject NuGet

var container = new ServiceContainer();

container.AddHttpClient();

var client = container.GetHttpClient();

Microsoft.Extensions.DependencyInjection NuGet

var container = new ServiceCollection();

container.AddHttpClient();

var provider = container.BuildServiceProvider();

var client = provider.GetHttpClient();

Middlewares

using (var response = await _sut.Get("http://httpbin.org/get").WithMiddleware(x =>
{
    x.AddTracing();
    x.AuthorizedByToken("token", "value");
    x.UseMemoryCache(30, y => y.Message.RequestUri.AbsoluteUri, z => z.Message.StatusCode == HttpStatusCode.OK);
}).SendAsync())
{
    var content = await response.Message.Content.ReadAsStringAsync();
}

Serilog NuGet

container.AddHttpClient(c=>c.Add<SerilogMiddelware>());
...
using (var response = await _sut.Get("http://httpbin.org/ip")
    .WithMiddleware(x=>x.UseSerilog()).SendAsync())
{

}

Polly NuGet

container.AddHttpClient(c=>
{
    c.Add<CircuitBreakerMiddelware>();
    c.Add<TimeoutMiddelware>();
    c.Add<OnConditionRetryMiddelware>();
});
...
using (var response = await _sut.Get("http://httpbin.org/ip")
    .WithMiddleware(x=>x.UseTimeout(5)).SendAsync())
{

}

using (var response = await _sut.Get("http://httpbin.org/ip")
    .WithMiddleware(x => x.OnConditionRetry(3, y => y.Message?.StatusCode != HttpStatusCode.OK)).SendAsync())
{

}

var policy = Policy
.HandleResult<HttpResponse>(r => r.Message?.StatusCode!= HttpStatusCode.OK )
.CircuitBreakerAsync(2, TimeSpan.FromSeconds(10));

using (var response = await _sut.Get("http://httpbin.org/ip")
    .WithMiddleware(x => x.UseCircuitBreaker(policy)).SendAsync())
{

}

Application Insights NuGet

container.AddHttpClient(c=>c.Add<ApplicationInsightsMiddelware>());
...
using (var response = await _sut.Get("http://httpbin.org/ip")
    .WithMiddleware(x=>x.UseApplicationInsights()).SendAsync())
{

}

Common Logging NuGet

container.AddHttpClient(c=>c.Add<CommonLoggingMiddelware>());
...
using (var response = await _sut.Get("http://httpbin.org/ip")
    .WithMiddleware(x=>x.UseCommonLogging()).SendAsync())
{

}