Adds typed HTTP client to the Web client, for interacting with the API.

This commit is contained in:
BinaryAlley committed 2024-09-07 17:15:30 +03:00
1 parent d8c64920dc
commit 4301b0a3ee
9 files changed
+409 -7

No files matched your search

@@ -0,0 +1,46 @@
using Lumina.Presentation.Web.Common.Models.Configuration;
using Lumina.Presentation.Web.Common.Utilities;
using Lumina.Presentation.Web.Common.Validators;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
namespace Lumina.Presentation.Web.Common.DependencyInjection;
/// <summary>
/// Class containing the configuration bindings of the Lumina.Presentation.Web project.
/// </summary>
internal static class PresentationWebLayerConfiguration
{
#region ===================================================================== METHODS ===================================================================================
/// <summary>
/// Extension method for adding the Lumina.Presentation.Web configuration to the <see cref="WebAssemblyHostBuilder"/>.
/// </summary>
/// <param name="builder">The <see cref="WebAssemblyHostBuilder"/> to add the configuration to.</param>
/// <returns>The updated <see cref="WebAssemblyHostBuilder"/>.</returns>
public static WebAssemblyHostBuilder AddConfiguration(this WebAssemblyHostBuilder builder)
{
// add the configuration sources to the configuration builder
builder.Configuration.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
builder.Configuration.AddJsonFile("appsettings.Development.json", optional: true, reloadOnChange: true);
builder.Services.BindConfiguration(builder.Configuration);
return builder;
}
/// <summary>
/// Extension method for registering the configuration options of the Lumina.Presentation.Web project into the Dependency Injection container.
/// </summary>
/// <param name="serviceCollection">The <see cref="IServiceCollection"/> to bind the configuration options to.</param>
/// <param name="configuration">The configuration to be bound.</param>
/// <returns>The updated <see cref="IServiceCollection"/>.</returns>
internal static IServiceCollection BindConfiguration(this IServiceCollection serviceCollection, IConfiguration configuration)
{
serviceCollection.AddOptions<ServerConfigurationModel>()
.Bind(configuration.GetRequiredSection(ServerConfigurationModel.SectionName))
.ValidateFluently()
.ValidateOnStart();
return serviceCollection;
}
#endregion
}
@@ -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
}