-
Notifications
You must be signed in to change notification settings - Fork 24
Feature/dan 145 prefill dataset #1626
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pernillehofgaard
wants to merge
28
commits into
Altinn:main
Choose a base branch
from
pernillehofgaard:feature/DAN-145-prefill-dataset
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
1dbadd4
added prefill from Dan to PrefillSI.cs
pernillehofgaard 0d7bfa5
updated interface with correct parameters
pernillehofgaard 76eef15
added danClient
pernillehofgaard 0a232ce
fixed tests
pernillehofgaard 4688744
DanClient returns dictionary
pernillehofgaard e3b555f
removed hardcoding of subject
pernillehofgaard 2f02bc4
use SwapKeyValuesForPrefill so documentation is accurate
pernillehofgaard dde9af6
added xml comments to classes, methods and properties
pernillehofgaard 9e53974
added query-jmespath to api call and helper methods
pernillehofgaard fcc1d42
Only use the json key as jmespath
pernillehofgaard 8a5d15f
cleaned up in code
pernillehofgaard bc7a914
Fixed tests
pernillehofgaard aab9fe7
added comments and cleaned up in code
pernillehofgaard 3d94d7a
fixed null references and warnings
pernillehofgaard ae93171
cleaned up and removed unnecessary code
pernillehofgaard 77d68fe
removed unused code
pernillehofgaard 96719e7
Removed unused packages
pernillehofgaard e33ca33
move headers to constructor
pernillehofgaard b7900dc
Added DanClientMock and fixed tests
pernillehofgaard f59f86d
Return empty dictionary if fields are not specified
pernillehofgaard 775f4c7
added null check for subject and dataset name
pernillehofgaard 94ade4f
moved dan DI to correct method
pernillehofgaard 90e3014
removed mocked client
pernillehofgaard b92b403
removed unneccessary code and added null check
pernillehofgaard 838af72
Added test for prefill of Dan data
pernillehofgaard fd65707
various fixes recommended på codeQL and coderabbit
pernillehofgaard 2d7bd80
fixed test
pernillehofgaard 39d11d5
Made fields in test model nullable
pernillehofgaard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| namespace Altinn.App.Core.Configuration; | ||
|
|
||
| /// <summary> | ||
| /// Settings for DanClient | ||
| /// </summary> | ||
| public class DanSettings | ||
| { | ||
| /// <summary> | ||
| /// Base url for Dan API | ||
| /// </summary> | ||
| public required string BaseUrl { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Api subscription keys | ||
| /// </summary> | ||
| public required string SubscriptionKey { get; set; } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
src/Altinn.App.Core/Infrastructure/Clients/Dan/DanClient.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| using System.Text; | ||
| using System.Text.Json; | ||
| using Altinn.App.Core.Configuration; | ||
| using Altinn.App.Core.Internal.Dan; | ||
| using Microsoft.Extensions.Options; | ||
| using Newtonsoft.Json; | ||
| using JsonException = Newtonsoft.Json.JsonException; | ||
pernillehofgaard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| using JsonSerializer = System.Text.Json.JsonSerializer; | ||
|
|
||
| namespace Altinn.App.Core.Infrastructure.Clients.Dan; | ||
|
|
||
| /// <summary> | ||
| /// Client for interacting with the Dan API. | ||
| /// </summary> | ||
| public class DanClient : IDanClient | ||
| { | ||
| private readonly HttpClient _httpClient; | ||
| private readonly IOptions<DanSettings> _settings; | ||
|
|
||
| /// <summary> | ||
| /// Constructor | ||
| /// </summary> | ||
| /// <param name="factory"></param> | ||
| /// <param name="settings"></param> | ||
| public DanClient(IHttpClientFactory factory, IOptions<DanSettings> settings) | ||
| { | ||
| _httpClient = factory.CreateClient("DanClient"); | ||
| _settings = settings; | ||
|
|
||
| _httpClient.DefaultRequestHeaders.Clear(); | ||
| _httpClient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", _settings.Value.SubscriptionKey); | ||
| _httpClient.DefaultRequestHeaders.Add("Accept", "application/json"); | ||
| _httpClient.BaseAddress = new Uri(settings.Value.BaseUrl); | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /// <summary> | ||
| /// Returns dataset from Dan API. | ||
| /// </summary> | ||
| /// <param name="dataset">Dataset from Dan</param> | ||
| /// <param name="subject">Usually ssn or orgNumber</param> | ||
| /// <param name="fields">The fields we fetch from the api</param> | ||
| /// <returns></returns> | ||
| public async Task<Dictionary<string, string>> GetDataset(string dataset, string subject, string fields) | ||
| { | ||
| var body = new { Subject = subject }; | ||
| var myContent = JsonConvert.SerializeObject(body); | ||
|
|
||
| var fieldsToFill = GetQuery(fields); | ||
| if (fieldsToFill.Count == 0) | ||
| return new Dictionary<string, string>(); | ||
|
|
||
| var baseQuery = $"{fieldsToFill.First()} : {fieldsToFill.First()}"; | ||
| foreach (var jsonKey in fieldsToFill.Skip(1)) | ||
| { | ||
| //if there is more than one field to fetch, add it to the query | ||
| baseQuery += $",{jsonKey} : {jsonKey}"; | ||
Check noticeCode scanning / CodeQL String concatenation in loop Note
String concatenation in loop: use 'StringBuilder'.
|
||
| } | ||
|
|
||
| //ensures that the query returns a list if endpoint returns a list and an object when endpoint returns a single object | ||
| var query = "[].{" + baseQuery + "}||{" + baseQuery + "}"; | ||
pernillehofgaard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| using (var content = new StringContent(myContent, Encoding.UTF8, "application/json")) | ||
| { | ||
| var result = await _httpClient.PostAsync( | ||
| $"directharvest/{dataset}?envelope=false&reuseToken=true&query={query}", | ||
| content | ||
| ); | ||
|
|
||
| if (result.IsSuccessStatusCode) | ||
| { | ||
| Dictionary<string, string>? dictionary; | ||
|
|
||
| var resultJson = await result.Content.ReadAsStringAsync(); | ||
|
|
||
| dictionary = IsJsonArray(resultJson) | ||
| ? await ConvertListToDictionary(resultJson) | ||
| : JsonConvert.DeserializeObject<Dictionary<string, string>>(resultJson); | ||
|
|
||
| return dictionary ?? new Dictionary<string, string>(); | ||
| } | ||
| } | ||
| return new Dictionary<string, string>(); | ||
| } | ||
|
|
||
| private static Task<Dictionary<string, string>> ConvertListToDictionary(string jsonString) | ||
| { | ||
| var list = JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(jsonString); | ||
| if (list != null) | ||
| return Task.FromResult( | ||
| list.SelectMany(d => d) | ||
| .GroupBy(kvp => kvp.Key) | ||
| .ToDictionary(g => g.Key, g => string.Join(",", g.Select(x => x.Value))) | ||
| ); | ||
|
|
||
| return Task.FromResult(new Dictionary<string, string>()); | ||
| } | ||
|
|
||
| private static bool IsJsonArray(string jsonString) | ||
| { | ||
| try | ||
| { | ||
| using var doc = JsonDocument.Parse(jsonString); | ||
| return doc.RootElement.ValueKind == JsonValueKind.Array; | ||
| } | ||
| catch (JsonException) | ||
| { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| private static List<string> GetQuery(string json) | ||
| { | ||
| var list = JsonSerializer.Deserialize<List<Dictionary<string, string>>>(json); | ||
| if (list != null) | ||
| return list.Where(l => l.Count != 0).Select(l => l.Keys.First()).ToList(); | ||
| return new List<string>(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| namespace Altinn.App.Core.Internal.Dan; | ||
|
|
||
| /// <summary> | ||
| /// DanClient interface | ||
| /// </summary> | ||
| public interface IDanClient | ||
| { | ||
| /// <summary> | ||
| /// Method for getting a selected dataset from Dan Api | ||
| /// </summary> | ||
| /// <param name="dataset">Name of the dataset</param> | ||
| /// <param name="subject">Usually ssn or OrgNumber</param> | ||
| /// <param name="fields">fields to fetch from endpoint</param> | ||
| /// <returns></returns> | ||
| public Task<Dictionary<string, string>> GetDataset(string dataset, string subject, string fields); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.