#region ========================================================================= USING =====================================================================================
using Lumina.Plugins.Contracts.Core.Metadata;
using Lumina.Plugins.Contracts.Core.Plugins;
using Lumina.Plugins.OpenLibrary.Common.Models.DTO.Settings;
using Lumina.Plugins.OpenLibrary.Core;
using Lumina.Plugins.OpenLibrary.Core.Api;
using Lumina.Plugins.OpenLibrary.Core.Settings;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using System;
#endregion
namespace Lumina.Plugins.OpenLibrary.Common.DependencyInjection;
///
/// Utility class for registering the services of the Open Library metadata provider into the Dependency Injection container.
///
internal static class OpenLibraryServices
{
///
/// Registers the services of the Open Library metadata provider into the Dependency Injection container.
///
/// The service collection to add the services to.
/// The unique identifier of the plugin that provides the metadata.
/// Action used to configure the .
/// The updated .
internal static IServiceCollection AddOpenLibraryBookMetadataProvider(this IServiceCollection services, Guid pluginId, Action? settingsCallback = null)
{
ArgumentNullException.ThrowIfNull(services);
// Build the runtime settings provider per scope, overlaying the settings persisted by the host over the defaults and the optional callback.
services.TryAddScoped(serviceProvider =>
{
OpenLibrarySettingsDto defaults = new();
settingsCallback?.Invoke(defaults);
IPluginSettingsStore? settingsStore = serviceProvider.GetService();
return new OpenLibrarySettingsProvider(settingsStore, pluginId, defaults);
});
services.AddHttpClient();
// Register the metadata provider as a keyed by pluginId transient service, so it can be resolved specifically among other IMetadataProvider implementations.
services.AddKeyedTransient(pluginId);
return services;
}
}