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-24 16:28:12 +03:00
|
|
|
using System.Linq;
|
2022-06-14 20:13:08 +03:00
|
|
|
using System.Net.Http;
|
2022-06-24 16:28:12 +03:00
|
|
|
using System.Net.Http.Headers;
|
2022-06-14 20:13:08 +03:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
{
|
2022-06-24 16:28:12 +03:00
|
|
|
private static readonly ProductInfoHeaderValue _userAgent;
|
|
|
|
|
|
|
|
|
|
static ServiceCollectionExtensions()
|
|
|
|
|
{
|
|
|
|
|
var assemblyName = typeof(INordigenClient).Assembly.GetName();
|
|
|
|
|
var assemblyShortName = assemblyName.Name ?? assemblyName.FullName.Split(',').First();
|
|
|
|
|
_userAgent = new(assemblyShortName, assemblyName.Version?.ToString());
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-14 20:13:08 +03:00
|
|
|
/// <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-24 16:28:44 +03:00
|
|
|
.AddSingleton<NordigenJsonSerializerOptions>()
|
2022-06-14 20:13:08 +03:00
|
|
|
.AddTransient<INordigenClient, NordigenClient>()
|
2022-08-29 22:12:09 +03:00
|
|
|
.AddTransient<IAccountClient, AccountClient>(provider =>
|
|
|
|
|
{
|
|
|
|
|
var httpClient = provider.GetRequiredService<IHttpClientFactory>().CreateClient(NordigenOptions.SectionName);
|
|
|
|
|
var options = provider.GetRequiredService<NordigenJsonSerializerOptions>();
|
|
|
|
|
return new(httpClient, options);
|
|
|
|
|
})
|
|
|
|
|
.AddTransient<IAgreementClient, AgreementClient>(provider =>
|
|
|
|
|
{
|
|
|
|
|
var httpClient = provider.GetRequiredService<IHttpClientFactory>().CreateClient(NordigenOptions.SectionName);
|
|
|
|
|
var options = provider.GetRequiredService<NordigenJsonSerializerOptions>();
|
|
|
|
|
return new(httpClient, options);
|
|
|
|
|
})
|
|
|
|
|
.AddTransient<IInstitutionClient, InstitutionClient>(provider =>
|
|
|
|
|
{
|
|
|
|
|
var httpClient = provider.GetRequiredService<IHttpClientFactory>().CreateClient(NordigenOptions.SectionName);
|
|
|
|
|
var options = provider.GetRequiredService<NordigenJsonSerializerOptions>();
|
|
|
|
|
return new(httpClient, options);
|
|
|
|
|
})
|
|
|
|
|
.AddTransient<IRequisitionClient, RequisitionClient>(provider =>
|
|
|
|
|
{
|
|
|
|
|
var httpClient = provider.GetRequiredService<IHttpClientFactory>().CreateClient(NordigenOptions.SectionName);
|
|
|
|
|
var options = provider.GetRequiredService<NordigenJsonSerializerOptions>();
|
|
|
|
|
return new(httpClient, options);
|
|
|
|
|
})
|
2022-06-15 22:37:59 +03:00
|
|
|
.AddTransient(provider => provider.GetRequiredService<IOptionsMonitor<NordigenOptions>>().CurrentValue)
|
2022-08-29 22:12:09 +03:00
|
|
|
.AddHttpClient(NordigenOptions.SectionName, ConfigureNordigenClient)
|
2022-06-16 18:45:54 +03:00
|
|
|
.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-24 16:28:12 +03:00
|
|
|
client.DefaultRequestHeaders.UserAgent.Add(_userAgent);
|
2022-06-14 20:13:08 +03:00
|
|
|
}
|
|
|
|
|
}
|