2022-06-14 20:13:08 +03:00
|
|
|
// Copyright 2022 Valters Melnalksnis
|
|
|
|
|
// Licensed under the Apache License 2.0.
|
|
|
|
|
// See LICENSE file in the project root for full license information.
|
|
|
|
|
|
|
|
|
|
using System;
|
2022-06-15 21:38:42 +03:00
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2022-06-14 20:13:08 +03:00
|
|
|
using System.Net.Http;
|
|
|
|
|
|
|
|
|
|
using JetBrains.Annotations;
|
|
|
|
|
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection.Extensions;
|
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
|
|
|
|
|
using NodaTime;
|
|
|
|
|
|
|
|
|
|
using VMelnalksnis.NordigenDotNet.Accounts;
|
2022-06-15 20:00:31 +03:00
|
|
|
using VMelnalksnis.NordigenDotNet.Agreements;
|
2022-06-14 20:13:08 +03:00
|
|
|
using VMelnalksnis.NordigenDotNet.Institutions;
|
|
|
|
|
using VMelnalksnis.NordigenDotNet.Requisitions;
|
2022-06-16 18:45:54 +03:00
|
|
|
using VMelnalksnis.NordigenDotNet.Tokens;
|
2022-06-14 20:13:08 +03:00
|
|
|
|
|
|
|
|
namespace VMelnalksnis.NordigenDotNet.DependencyInjection;
|
|
|
|
|
|
|
|
|
|
/// <summary>Collection of methods for configuring VMelnalksnis.NordigenDotNet in <see cref="IServiceCollection"/>.</summary>
|
|
|
|
|
[PublicAPI]
|
|
|
|
|
public static class ServiceCollectionExtensions
|
|
|
|
|
{
|
|
|
|
|
/// <summary>Adds all required services for <see cref="INordigenClient"/>.</summary>
|
|
|
|
|
/// <param name="serviceCollection">The service collection in which to register the services.</param>
|
|
|
|
|
/// <param name="configuration">The configuration to which to bind options models.</param>
|
2022-06-16 20:22:52 +03:00
|
|
|
/// <param name="clock">Clock for accessing the current time.</param>
|
2022-06-14 20:13:08 +03:00
|
|
|
/// <param name="dateTimeZoneProvider">Time zone provider for date and time serialization.</param>
|
|
|
|
|
/// <returns>The <see cref="IHttpClientBuilder"/> for the <see cref="HttpClient"/> used by <see cref="INordigenClient"/>.</returns>
|
|
|
|
|
public static IHttpClientBuilder AddNordigenDotNet(
|
|
|
|
|
this IServiceCollection serviceCollection,
|
|
|
|
|
IConfiguration configuration,
|
2022-06-16 20:22:52 +03:00
|
|
|
IClock clock,
|
2022-06-14 20:13:08 +03:00
|
|
|
IDateTimeZoneProvider dateTimeZoneProvider)
|
|
|
|
|
{
|
2022-06-16 20:22:52 +03:00
|
|
|
serviceCollection.TryAddSingleton(clock);
|
2022-06-14 20:13:08 +03:00
|
|
|
serviceCollection.TryAddSingleton(dateTimeZoneProvider);
|
|
|
|
|
return serviceCollection.AddNordigenDotNet(configuration);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Adds all required services for <see cref="INordigenClient"/>, excluding external dependencies.</summary>
|
|
|
|
|
/// <param name="serviceCollection">The service collection in which to register the services.</param>
|
|
|
|
|
/// <param name="configuration">The configuration to which to bind options models.</param>
|
|
|
|
|
/// <returns>The <see cref="IHttpClientBuilder"/> for the <see cref="HttpClient"/> used by <see cref="INordigenClient"/>.</returns>
|
2022-06-15 21:38:42 +03:00
|
|
|
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026", Justification = $"{nameof(NordigenOptions)} contains only system types.")]
|
2022-06-14 20:13:08 +03:00
|
|
|
public static IHttpClientBuilder AddNordigenDotNet(
|
|
|
|
|
this IServiceCollection serviceCollection,
|
|
|
|
|
IConfiguration configuration)
|
|
|
|
|
{
|
|
|
|
|
serviceCollection
|
|
|
|
|
.AddOptions<NordigenOptions>()
|
|
|
|
|
.Bind(configuration.GetSection(NordigenOptions.SectionName))
|
|
|
|
|
.ValidateDataAnnotations();
|
|
|
|
|
|
2022-06-16 18:45:54 +03:00
|
|
|
serviceCollection.AddHttpClient<TokenDelegatingHandler>(ConfigureNordigenClient);
|
|
|
|
|
|
2022-06-14 20:13:08 +03:00
|
|
|
return serviceCollection
|
2022-06-16 20:22:52 +03:00
|
|
|
.AddSingleton<NordigenTokenCache>()
|
2022-06-14 20:13:08 +03:00
|
|
|
.AddTransient<INordigenClient, NordigenClient>()
|
|
|
|
|
.AddTransient<IAccountClient, AccountClient>()
|
2022-06-15 20:00:31 +03:00
|
|
|
.AddTransient<IAgreementClient, AgreementClient>()
|
2022-06-14 20:13:08 +03:00
|
|
|
.AddTransient<IInstitutionClient, InstitutionClient>()
|
|
|
|
|
.AddTransient<IRequisitionClient, RequisitionClient>()
|
2022-06-15 22:37:59 +03:00
|
|
|
.AddTransient(provider => provider.GetRequiredService<IOptionsMonitor<NordigenOptions>>().CurrentValue)
|
2022-06-16 18:45:54 +03:00
|
|
|
.AddHttpClient<NordigenHttpClient>(ConfigureNordigenClient)
|
|
|
|
|
.AddHttpMessageHandler<TokenDelegatingHandler>();
|
|
|
|
|
}
|
2022-06-14 20:13:08 +03:00
|
|
|
|
2022-06-16 18:45:54 +03:00
|
|
|
private static void ConfigureNordigenClient(IServiceProvider provider, HttpClient client)
|
|
|
|
|
{
|
|
|
|
|
client.BaseAddress = provider.GetRequiredService<IOptionsMonitor<NordigenOptions>>().CurrentValue.BaseAddress;
|
2022-06-14 20:13:08 +03:00
|
|
|
|
2022-06-16 18:45:54 +03:00
|
|
|
var assembly = typeof(INordigenClient).Assembly.GetName();
|
2022-06-14 20:13:08 +03:00
|
|
|
|
2022-06-16 18:45:54 +03:00
|
|
|
var assemblyName = assembly.Name ??
|
|
|
|
|
throw new InvalidOperationException($"Assembly {assembly.FullName} name is not specified");
|
2022-06-14 20:13:08 +03:00
|
|
|
|
2022-06-16 18:45:54 +03:00
|
|
|
var assemblyVersion = assembly.Version ??
|
|
|
|
|
throw new InvalidOperationException($"Assembly {assembly.FullName} version is not specified");
|
|
|
|
|
|
|
|
|
|
client.DefaultRequestHeaders.UserAgent.Add(new(assemblyName, assemblyVersion.ToString()));
|
2022-06-14 20:13:08 +03:00
|
|
|
}
|
|
|
|
|
}
|