mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 06:11:10 +00:00
Adds typed HTTP client to the Web client, for interacting with the API.
This commit is contained in:
1 parent
d8c64920dc
commit
4301b0a3ee
9 files changed
+409
-7
No files matched your search
+52
@@ -0,0 +1,52 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Presentation.Web.Core.Services.UI;
|
||||
using Lumina.Presentation.Web.Common.Api;
|
||||
using Polly;
|
||||
using System.Net;
|
||||
using FluentValidation;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Web.Common.DependencyInjection;
|
||||
|
||||
/// <summary>
|
||||
/// Contains all services of the Presentation Web layer.
|
||||
/// </summary>
|
||||
public static class PresentationWebLayerServices
|
||||
{
|
||||
#region ===================================================================== METHODS ===================================================================================
|
||||
/// <summary>
|
||||
/// Registers the services of the Presentation Web layer into the dependency injection container.
|
||||
/// </summary>
|
||||
/// <param name="services">The service collection to add the services to.</param>
|
||||
/// <returns>The updated <see cref="IServiceCollection"/>.</returns>
|
||||
public static IServiceCollection AddPresentationWebLayerServices(this IServiceCollection services)
|
||||
{
|
||||
// scan the current assembly for validators and register them to the DI container
|
||||
services.AddValidatorsFromAssemblyContaining<Program>(ServiceLifetime.Singleton);
|
||||
|
||||
// handle transient errors like network timeouts or intermittent failures
|
||||
var retryPolicy = Policy
|
||||
.Handle<HttpRequestException>()
|
||||
.OrResult<HttpResponseMessage>(r => !r.IsSuccessStatusCode)
|
||||
.WaitAndRetryAsync(3, retryAttempt =>
|
||||
TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)));
|
||||
|
||||
// use a circuit breaker to prevent repeatedly calling a failing service
|
||||
var circuitBreakerPolicy = Policy
|
||||
.Handle<HttpRequestException>()
|
||||
.OrResult<HttpResponseMessage>(r => r.StatusCode == HttpStatusCode.InternalServerError)
|
||||
.CircuitBreakerAsync(3, TimeSpan.FromSeconds(30));
|
||||
|
||||
var policy = Policy.WrapAsync(retryPolicy, circuitBreakerPolicy);
|
||||
|
||||
// register the typed client used for the API interaction
|
||||
services.AddHttpClient<IApiHttpClient, ApiHttpClient>()
|
||||
.AddPolicyHandler(retryPolicy)
|
||||
.AddPolicyHandler(circuitBreakerPolicy);
|
||||
|
||||
services.AddSingleton<ComboboxService>();
|
||||
|
||||
return services;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
Reference in new issue
Block a user