Skip to content

Commit 6cff1c7

Browse files
Add missing extension methods for DI registration of Posten and RestCountries (#63)
* Add missing extension methods for DI registration of Posten and RestCountries * fix typo
1 parent dc2f089 commit 6cff1c7

20 files changed

+969
-5231
lines changed

.editorconfig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,12 @@ dotnet_diagnostic.S2325.severity = suggestion
167167
# S3267: Loops should be simplified with "LINQ" expressions
168168
dotnet_diagnostic.S3267.severity = suggestion
169169

170+
# S125: Sections of code should not be commented out
171+
dotnet_diagnostic.S125.severity = suggestion
172+
173+
# S1075: URIs should not be hardcoded
174+
dotnet_diagnostic.S1075.severity = suggestion
175+
170176
[Program.cs]
171177
dotnet_diagnostic.CA1050.severity = none
172178
dotnet_diagnostic.S1118.severity = none

Directory.Packages.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@
1616
<PackageVersion Include="PublicApiGenerator" Version="11.4.6" />
1717
<PackageVersion Include="Verify.Xunit" Version="30.11.0" />
1818
<PackageVersion Include="SonarAnalyzer.CSharp" Version="10.15.0.120848" />
19+
<PackageVersion Include="WireMock.Net" Version="1.12.0" />
1920
</ItemGroup>
2021
</Project>

src/Altinn.Codelists/Extensions/ServiceCollectionExtensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Altinn.Codelists.Kartverket.AdministrativeUnits.Extensions;
2+
using Altinn.Codelists.Posten;
23
using Altinn.Codelists.SSB.Extensions;
34
using Microsoft.Extensions.DependencyInjection;
45

@@ -16,6 +17,7 @@ public static IServiceCollection AddAltinnCodelists(this IServiceCollection serv
1617
{
1718
services.AddKartverketAdministrativeUnits();
1819
services.AddSSBClassifications();
20+
services.AddPosten();
1921

2022
return services;
2123
}
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
using Altinn.Codelists.Kartverket.AdministrativeUnits.Clients;
2-
3-
namespace Altinn.Codelists.Kartverket.AdministrativeUnits;
1+
namespace Altinn.Codelists.Kartverket.AdministrativeUnits;
42

53
/// <summary>
6-
/// Options to control the behavior of <see cref="AdministrativeUnitsHttpClient"/>
4+
/// Options to control the behavior of <see cref="IAdministrativeUnitsClient"/>
75
/// </summary>
86
public class AdministrativeUnitsSettings
97
{
108
/// <summary>
11-
/// Base url to the api endpoint for information on administrative units.
9+
/// Base url to the API endpoint for information on administrative units.
1210
/// </summary>
1311
public string BaseApiUrl { get; set; } = "https://ws.geonorge.no/kommuneinfo/v1/";
1412
}

src/Altinn.Codelists/Kartverket/AdministrativeUnits/Extensions/ServiceCollectionExtensions.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Altinn.Codelists.Kartverket.AdministrativeUnits.Clients;
33
using Microsoft.Extensions.Caching.Memory;
44
using Microsoft.Extensions.DependencyInjection;
5+
using Microsoft.Extensions.DependencyInjection.Extensions;
56

67
namespace Altinn.Codelists.Kartverket.AdministrativeUnits.Extensions;
78

@@ -14,16 +15,28 @@ public static class ServiceCollectionExtensions
1415
/// Registers the services required to get support for Kartverkets counties (fylker) and municipalities (kommuner) codelists.
1516
/// </summary>
1617
public static IServiceCollection AddKartverketAdministrativeUnits(this IServiceCollection services)
18+
{
19+
services.AddKartverketAdministrativeUnitsClient();
20+
services.AddTransient<IAppOptionsProvider, CountiesCodelistProvider>();
21+
services.AddTransient<IAppOptionsProvider, MunicipalitiesCodelistProvider>();
22+
23+
return services;
24+
}
25+
26+
/// <summary>
27+
/// Registers the client services required to get support for Kartverkets counties (fylker) and municipalities (kommuner) codelists.
28+
/// </summary>
29+
/// <param name="services"></param>
30+
/// <returns></returns>
31+
public static IServiceCollection AddKartverketAdministrativeUnitsClient(this IServiceCollection services)
1732
{
1833
services.AddMemoryCache();
1934
services.AddOptions<AdministrativeUnitsSettings>();
2035
services.AddHttpClient();
21-
services.AddTransient<IAdministrativeUnitsClient>(sp => new AdministrativeUnitsHttpClientCached(
36+
services.TryAddTransient<IAdministrativeUnitsClient>(sp => new AdministrativeUnitsHttpClientCached(
2237
ActivatorUtilities.CreateInstance<AdministrativeUnitsHttpClient>(sp),
2338
sp.GetRequiredService<IMemoryCache>()
2439
));
25-
services.AddTransient<IAppOptionsProvider, CountiesCodelistProvider>();
26-
services.AddTransient<IAppOptionsProvider, MunicipalitiesCodelistProvider>();
2740

2841
return services;
2942
}

src/Altinn.Codelists/Posten/Clients/PostalCodesCsvParser.cs

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,26 @@
66
/// File used: https://www.bring.no/postnummerregister-ansi.txt
77
/// Examample record: 6863 LEIKANGER 4640 SOGNDAL G
88
/// </summary>
9-
internal sealed class PostalCodesCsvParser
9+
internal sealed class PostalCodesCsvParser(Stream _csvStream)
1010
{
11-
private Stream _csvStream { get; set; }
12-
13-
/// <summary>
14-
/// Creates an instance of <see cref="PostalCodesCsvParser"/>
15-
/// </summary>
16-
public PostalCodesCsvParser(Stream stream)
17-
{
18-
_csvStream = stream;
19-
}
20-
2111
/// <summary>
2212
/// Parses the stream provided in the constructor.
2313
/// </summary>
2414
public async Task<List<PostalCodeRecord>> Parse()
2515
{
2616
List<PostalCodeRecord> postalCodes = new();
2717

28-
using (StreamReader reader = new StreamReader(_csvStream, Encoding.Latin1))
18+
using StreamReader reader = new StreamReader(_csvStream, Encoding.Latin1, leaveOpen: true);
19+
20+
while (!reader.EndOfStream)
2921
{
30-
while (!reader.EndOfStream)
31-
{
32-
string? line = await reader.ReadLineAsync();
22+
string? line = await reader.ReadLineAsync();
3323

34-
if (line != null)
35-
{
36-
string[] columns = line.Split('\t');
37-
PostalCodeRecord postalCode = new(columns[0], columns[1], columns[2], columns[3], columns[4]);
38-
postalCodes.Add(postalCode);
39-
}
24+
if (line != null)
25+
{
26+
string[] columns = line.Split('\t');
27+
PostalCodeRecord postalCode = new(columns[0], columns[1], columns[2], columns[3], columns[4]);
28+
postalCodes.Add(postalCode);
4029
}
4130
}
4231

src/Altinn.Codelists/Posten/Clients/PostalCodesHttpClient.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
namespace Altinn.Codelists.Posten.Clients;
1+
using Microsoft.Extensions.Options;
22

3-
internal sealed class PostalCodesHttpClient(HttpClient _httpClient) : IPostalCodesClient
3+
namespace Altinn.Codelists.Posten.Clients;
4+
5+
internal sealed class PostalCodesHttpClient(HttpClient _httpClient, IOptions<PostenSettings> _options)
6+
: IPostalCodesClient
47
{
5-
private readonly Uri _uri = new("https://www.bring.no/postnummerregister-ansi.txt");
8+
private readonly Uri _uri = new(_options.Value.Url);
69

710
public async Task<List<PostalCodeRecord>> GetPostalCodes()
811
{
9-
using var response = await _httpClient.GetAsync(_uri.ToString());
12+
using var response = await _httpClient.GetAsync(_uri.ToString(), HttpCompletionOption.ResponseHeadersRead);
1013
await using var responseStream = await response.Content.ReadAsStreamAsync();
1114

1215
var parser = new PostalCodesCsvParser(responseStream);
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using Altinn.App.Core.Features;
2+
using Altinn.Codelists.Posten.Clients;
3+
using Microsoft.Extensions.DependencyInjection;
4+
using Microsoft.Extensions.DependencyInjection.Extensions;
5+
6+
namespace Altinn.Codelists.Posten;
7+
8+
/// <summary>
9+
/// Extends the <see cref="IServiceCollection"/>.
10+
/// </summary>
11+
public static class ServiceCollectionExtensions
12+
{
13+
/// <summary>
14+
/// Registers the services required to get postal codes through <see cref="IAppOptionsProvider" /> and <see cref="IPostalCodesClient"/>
15+
/// </summary>
16+
public static IServiceCollection AddPosten(this IServiceCollection services)
17+
{
18+
services.AddPostenClient();
19+
services.AddTransient<IAppOptionsProvider, PostalCodesCodelistsProvider>();
20+
21+
return services;
22+
}
23+
24+
/// <summary>
25+
/// Registers <see cref="IPostalCodesClient"/>
26+
/// </summary>
27+
/// <returns></returns>
28+
public static IServiceCollection AddPostenClient(this IServiceCollection services)
29+
{
30+
services.AddHttpClient();
31+
services.AddOptions<PostenSettings>();
32+
services.TryAddTransient<IPostalCodesClient, PostalCodesHttpClient>();
33+
34+
return services;
35+
}
36+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace Altinn.Codelists.Posten;
2+
3+
/// <summary>
4+
/// Options to control the behavior of <see cref="IPostalCodesClient"/>
5+
/// </summary>
6+
public class PostenSettings
7+
{
8+
internal static readonly string DefaultBaseUrl = "https://www.bring.no";
9+
internal static readonly string DefaultPath = "/postnummerregister-ansi.txt";
10+
11+
/// <summary>
12+
/// URL to Brings postnummerregister API
13+
/// </summary>
14+
public string Url { get; set; } = $"{DefaultBaseUrl}{DefaultPath}";
15+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Altinn.Codelists.RestCountries.Clients;
2+
using Microsoft.Extensions.DependencyInjection;
3+
4+
namespace Altinn.Codelists.RestCountries;
5+
6+
/// <summary>
7+
/// Extends the <see cref="IServiceCollection"/>.
8+
/// </summary>
9+
public static class ServiceCollectionExtensions
10+
{
11+
/// <summary>
12+
/// Registers the <see cref="ICountryClient"/> interface
13+
/// </summary>
14+
public static IServiceCollection AddRestCountriesClient(this IServiceCollection services)
15+
{
16+
services.AddHttpClient();
17+
services.AddTransient<ICountryClient, CountriesClient>();
18+
19+
return services;
20+
}
21+
}

0 commit comments

Comments
 (0)