Skip to content

Commit

Permalink
Fix multiple registrations of basic services.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronny Birkeli committed May 22, 2023
1 parent 9dc2fda commit 22aeff2
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 8 deletions.
24 changes: 19 additions & 5 deletions src/Altinn.Codelists/SSB/Extensions/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@ public static class ServiceCollectionExtensions
/// </summary>
public static IServiceCollection AddSSBClassifications(this IServiceCollection services)
{
// Basic setup
services.AddMemoryCache();
services.AddOptions<ClassificationSettings>();
services.AddHttpClient<IClassificationsClient, ClassificationsHttpClient>();
services.Decorate<IClassificationsClient, ClassificationsHttpClientCached>();
EnsureBasicServicesRegistered(services);

// Add the codelist providers
services.AddSSBClassificationCodelistProvider("kjønn", Classification.Sex);
Expand All @@ -34,6 +30,20 @@ public static IServiceCollection AddSSBClassifications(this IServiceCollection s
return services;
}

// Services added here should be safe to add multiple times
// either resulting in singleton or replacing the existing service
private static void EnsureBasicServicesRegistered(IServiceCollection services)
{
services.AddMemoryCache();
services.AddOptions<ClassificationSettings>();

if (services.All(x => x.ServiceType != typeof(IClassificationsClient)))
{
services.AddHttpClient<IClassificationsClient, ClassificationsHttpClient>();
services.TryDecorate<IClassificationsClient, ClassificationsHttpClientCached>();
}
}

/// <summary>
/// Adds the specified <see cref="Classification"/> as an <see cref="IAppOptionsProvider"/> with the specified id.
/// </summary>
Expand All @@ -43,6 +53,7 @@ public static IServiceCollection AddSSBClassifications(this IServiceCollection s
/// <param name="defaultKeyValuePairs">Default set of key/value pairs to be used. Will be overriden by matching qyery parameters runtime.</param>
public static IServiceCollection AddSSBClassificationCodelistProvider(this IServiceCollection services, string id, Classification classification, Dictionary<string, string>? defaultKeyValuePairs = null)
{
EnsureBasicServicesRegistered(services);
services.AddTransient<IAppOptionsProvider>(sp => new ClassificationCodelistProvider(id, classification, sp.GetRequiredService<IClassificationsClient>(), defaultKeyValuePairs));

return services;
Expand All @@ -59,6 +70,7 @@ public static IServiceCollection AddSSBClassificationCodelistProvider(this IServ
/// <param name="defaultKeyValuePairs">Default set of key/value pairs to be used. Will be overriden by matching qyery parameters runtime.</param>
public static IServiceCollection AddSSBClassificationCodelistProvider(this IServiceCollection services, string id, Classification classification, ClassificationOptions options, Dictionary<string, string>? defaultKeyValuePairs = null)
{
EnsureBasicServicesRegistered(services);
services.AddTransient<IAppOptionsProvider>(sp => new ClassificationCodelistProvider(id, classification, sp.GetRequiredService<IClassificationsClient>(), options, defaultKeyValuePairs));

return services;
Expand All @@ -74,6 +86,7 @@ public static IServiceCollection AddSSBClassificationCodelistProvider(this IServ
/// <param name="defaultKeyValuePairs">Default set of key/value pairs to be used. Will be overriden by matching qyery parameters runtime.</param>
public static IServiceCollection AddSSBClassificationCodelistProvider(this IServiceCollection services, string id, int classificationId, Dictionary<string, string>? defaultKeyValuePairs = null)
{
EnsureBasicServicesRegistered(services);
services.AddTransient<IAppOptionsProvider>(sp => new ClassificationCodelistProvider(id, classificationId, sp.GetRequiredService<IClassificationsClient>(), defaultKeyValuePairs));

return services;
Expand All @@ -90,6 +103,7 @@ public static IServiceCollection AddSSBClassificationCodelistProvider(this IServ
/// <param name="defaultKeyValuePairs">Default set of key/value pairs to be used. Will be overriden by matching qyery parameters runtime.</param>
public static IServiceCollection AddSSBClassificationCodelistProvider(this IServiceCollection services, string id, int classificationId, ClassificationOptions options, Dictionary<string, string>? defaultKeyValuePairs = null)
{
EnsureBasicServicesRegistered(services);
services.AddTransient<IAppOptionsProvider>(sp => new ClassificationCodelistProvider(id, classificationId, sp.GetRequiredService<IClassificationsClient>(), defaultKeyValuePairs, options));

return services;
Expand Down
27 changes: 27 additions & 0 deletions test/Altinn.Codelists.Tests/SSB/Extensions/ExtensionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Altinn.Codelists.SSB;
using Altinn.Codelists.SSB.Extensions;
using Altinn.Codelists.SSB.Models;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Altinn.Codelists.Tests.SSB.Extensions
{
public class ExtensionTests
{
[Fact]
public void AddSSBClassifications()
{
IServiceCollection services = new ServiceCollection();
services.AddSSBClassificationCodelistProvider("sivilstand", Classification.MaritalStatus);
services.AddSSBClassificationCodelistProvider("yrker", Classification.Occupations);

IServiceProvider serviceProvider = services.BuildServiceProvider();

services.Select(x => x.ServiceType == typeof(IClassificationsClient)).Should().HaveCount(2);
}
}
}
6 changes: 3 additions & 3 deletions test/Altinn.Codelists.Tests/SSB/Testdata/classifications.http
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ Accept: application/json;charset=utf-8

###
# Småvilt med variant
GET https://data.ssb.no/api/klass/v1/classifications/74/codesAt?date=2023-03-12
GET https://data.ssb.no/api/klass/v1/classifications/74/codesAt?date=2023-03-12&language=nn
Accept: application/json;charset=utf-8

###
GET https://data.ssb.no/api/klass/v1/classifications/74/variantAt?&date=2023-01-01&language=nb&variantName=Hønsefugler, spurvefugler, skarver og due 2023-03 - variant av Klassifisering av småvilt 2017-04
GET https://data.ssb.no/api/klass/v1/classifications/74/variantAt?language=nn&date=2023-03-03&variantName=Hønsefugler, spurvefugler og due 2023-03 - variant av Klassifisering av småvilt 2017-04
Accept: application/json;charset=utf-8

###

GET http://data.ssb.no/api/klass/v1/variants/2103
GET http://data.ssb.no/api/klass/v1/variants/2103?language=nb
Accept: application/json;charset=utf-8

0 comments on commit 22aeff2

Please sign in to comment.