Skip to content

Commit

Permalink
Localtest: Add service owner orgs as possible lookups in register api…
Browse files Browse the repository at this point in the history
…. (#9684)

* Localtest: Use CDN orgs in register api.

* Get cdn orgs url from config.
  • Loading branch information
ivarne authored Feb 6, 2023
1 parent e13c69d commit 74bec64
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/Clients/CdnAltinnOrgs/AltinnOrgsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
using Altinn.Platform.Authentication.Configuration;
using Microsoft.Extensions.Options;

namespace LocalTest.Clients.CdnAltinnOrgs;

Expand All @@ -12,15 +14,17 @@ public class AltinnOrgsClient
{
private static JsonSerializerOptions JSON_OPTIONS = new JsonSerializerOptions(JsonSerializerDefaults.Web);
private readonly HttpClient _client;
private readonly GeneralSettings _authOptions;

public AltinnOrgsClient(HttpClient client)
public AltinnOrgsClient(HttpClient client, IOptions<GeneralSettings> authOptions)
{
_client = client;
_authOptions = authOptions.Value;
}

public async Task<CdnOrgs> GetCdnOrgs()
{
var orgsJson = await _client.GetByteArrayAsync("https://altinncdn.no/orgs/altinn-orgs.json");
var orgsJson = await _client.GetByteArrayAsync(_authOptions.GetOrganisationRepositoryLocation);
return JsonSerializer.Deserialize<CdnOrgs>(orgsJson, JSON_OPTIONS) ?? throw new JsonException("altinn-orgs respones was \"null\"");
}
}
24 changes: 22 additions & 2 deletions src/Services/Register/Implementation/OrganizationsWrapper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#nullable enable
using Altinn.Platform.Register.Models;
using LocalTest.Clients.CdnAltinnOrgs;
using LocalTest.Services.Register.Interface;
using LocalTest.Services.TestData;

Expand All @@ -11,17 +12,36 @@ namespace LocalTest.Services.Register.Implementation
public class OrganizationsWrapper : IOrganizations
{
private readonly TestDataService _testDataService;
private readonly AltinnOrgsClient _orgsClient;

public OrganizationsWrapper(TestDataService testDataService)
public OrganizationsWrapper(TestDataService testDataService, AltinnOrgsClient orgsClient)
{
_testDataService = testDataService;
_orgsClient = orgsClient;
}

/// <inheritdoc />
public async Task<Organization?> GetOrganization(string orgNr)
{
var data = await _testDataService.GetTestData();
return data.Register.Org.TryGetValue(orgNr, out var value) ? value : null;

if (data.Register.Org.TryGetValue(orgNr, out var value))
{
return value;
}

// Make lookup work for all the orgs that has apps in altinn.
var cdnOrgs = await _orgsClient.GetCdnOrgs();
var cdnOrg = cdnOrgs?.Orgs?.Values.FirstOrDefault(org=>org.Orgnr == orgNr);
if(cdnOrg is not null)
{
return new Organization
{
Name = cdnOrg.Name?.Nb,
OrgNumber = cdnOrg.Orgnr,
};
}
return null;
}
}
}

0 comments on commit 74bec64

Please sign in to comment.