UgenTec.Framework.Core.Http
UgenTec.Framework.Core.Http
is a library package containing helper code for configuring HttpClients in applications which require http communication to other systems.
Installation
Use the internal Ugentec Nuget-2 feed to install the UgenTec.Framework.Core.EntityFramework library
install-package UgenTec.Framework.Core.Http
Usage
Registering http clients
This library extends the Microsoft.Extension.Http library by adding new extension methods for configuring the behavior of http clients. Registration of the HttpClient starts by invoking any of the ServiceCollection.AddHttpClient overload available in that library.
The preferred way is by defining a proxy class which takes a HttpClient
dependency. This allows for specific configuration of different HttpClients without having to resort to magic strings.
//Typed proxy :
interface IAdminProxy
{
}
class AdminProxy: IAdminProxy
{
private readonly HttpClient _httpClient;
public ProxyStub(HttpClient httpClient)
{
_httpClient = httpClient;
}
public HttpClient HttpClient => _httpClient;
}
Register the proxy as HttpClient in startup.cs when configuring services :
public void ConfigureServices(IServiceCollection services)
{
services.AddHttpClient<IAdminProxy,AdminProxy>()
//configure specific configuration options using framework core extensions
.WithHttpStatusCodeToExceptionMapping(builder => builder.MapDefaultUgenTecStatusCodesToExceptions())
.WithBasicAuthentication("myuser","mypassword")
.WithTimeOut(TimeSpan.FromSeconds(42))
.WithContentCompression();
}
Samples
services.AddHttpClient<IAdminProxy,AdminProxy>()
//map all default UgenTec status codes (400, 500, 481, 482) and throw corresponding exceptions
.WithHttpStatusCodeToExceptionMapping(builder => builder.MapDefaultUgenTecStatusCodesToExceptions())
//configure mapping of 481 response status code to GenericClientExceptions
//probably not needed when all UgenTec defaults are configured
.WithHttpStatusCodeToExceptionMapping(cfg => cfg.Map(481, r => throw new GenericClientException()));
//authenticate using basic authentication
.WithBasicAuthentication("myuser","mypassword");
//authenticate using bearer token
.WithAccessTokenAuthentication(_tokenClient,"Scope"); //_tokenclient is an IdentityModel.Client.TokenClient configured elsewhere with authority information
//add header demanding response compression
.WithContentCompression();
// If using content response compression, make sure to add a HttpClientHandler for Gzip and Deflate decompression
// Brotli (br) compression is the default, but there is not decompression in .NET Core 2.2 yet so this is handled by the HttpClient extension methods.
.ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler
{
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
});
//add header demanding specific content-type (for content-type negotiation)
.WithAcceptHeader("application/text");
//configure timeout for the request
.WithTimeOut(TimeSpan.FromSeconds(42);
//configure retry behavior on GET requests with 1 retry and 300 ms wait time between attempts
.WithRetryOnTransientHttpErrors(
new RetryPolicy(1, TimeSpan.FromMilliseconds(300));
//configure retry behavior on GET and POST requests with 1 retry and 300 ms wait time between attempts
.WithRetryOnTransientHttpErrors(
new RetryPolicy(1, TimeSpan.FromMilliseconds(300),HttpMethod.Get,HttpMethod.Post));
//configure transfer of username, tenant id, and tenant shortcode for api to api calls
.WithPrincipalFlow()
//NOTE : UgenTec default mappings can be added easily using UgenTec.Defaults package.