From 4f7c85b622fd9b287bc77d8fabb8df023a0b6555 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Dybvik=20Langfors?= Date: Mon, 9 Oct 2023 11:12:52 +0200 Subject: [PATCH] Add JMESPath support --- .../Controllers/DanClientTestController.cs | 16 +++++++++++++--- samples/SampleWebApp/Startup.cs | 8 +++++--- samples/SampleWebApp/appsettings.json | 2 +- src/Altinn.ApiClients.Dan/Interfaces/IDanApi.cs | 10 +++++++--- .../Interfaces/IDanClient.cs | 10 +++++++--- src/Altinn.ApiClients.Dan/Services/DanClient.cs | 14 +++++++++----- .../Tests/Services/DanClientTest.cs | 4 +++- 7 files changed, 45 insertions(+), 19 deletions(-) diff --git a/samples/SampleWebApp/Controllers/DanClientTestController.cs b/samples/SampleWebApp/Controllers/DanClientTestController.cs index 179d15a..72f66fe 100644 --- a/samples/SampleWebApp/Controllers/DanClientTestController.cs +++ b/samples/SampleWebApp/Controllers/DanClientTestController.cs @@ -35,7 +35,11 @@ public async Task Get(string datasetname, string subject, [FromQue UnitBasicInformation ubi = await _danClient.GetDataSet(datasetname, subject, null, parameters); - Console.WriteLine($"Retrieved information about {ubi.OrganizationName}"); + // Example using JMESPath query to transform a dataset to a custom model + CustomUnitBasicInformation cubi = + await _danClient.GetDataSet(datasetname, subject, null, parameters, query: "{Orgnr: OrganizationNumber, Navn: OrganizationName}"); + + Console.WriteLine($"Retrieved information about {ubi.OrganizationName} ({cubi.Orgnr})"); return Content(dataset.ToHtmlTable(), "text/html; charset=utf-8"); } @@ -152,9 +156,15 @@ public static string ToHtmlTable(this List drsList) } + public class CustomUnitBasicInformation + { + public int Orgnr { get; set; } + public string Navn { get; set; } + } + public class UnitBasicInformation { - public string OrganizationNumber { get; set; } + public long OrganizationNumber { get; set; } public string OrganizationName { get; set; } @@ -194,7 +204,7 @@ public class UnitBasicInformation public string LatestFinacialStatement { get; set; } - public string NumberOfEmployees { get; set; } + public int NumberOfEmployees { get; set; } public bool IsBeingDissolved { get; set; } diff --git a/samples/SampleWebApp/Startup.cs b/samples/SampleWebApp/Startup.cs index 909c20e..3f6aaae 100644 --- a/samples/SampleWebApp/Startup.cs +++ b/samples/SampleWebApp/Startup.cs @@ -11,6 +11,8 @@ namespace SampleWebApp { public class Startup { + private const string MyClientDefinitionForDan = "my-client-definition-for-dan"; + public Startup(IConfiguration configuration) { Configuration = configuration; @@ -20,12 +22,12 @@ public Startup(IConfiguration configuration) public void ConfigureServices(IServiceCollection services) { - services.RegisterMaskinportenClientDefinition("my-client-definition-for-dan", + services.RegisterMaskinportenClientDefinition(MyClientDefinitionForDan, Configuration.GetSection("MaskinportenSettingsForDanClient")); services .AddDanClient(Configuration.GetSection("DanSettings")) - .AddMaskinportenHttpMessageHandler("my-client-definition-for-dan"); + .AddMaskinportenHttpMessageHandler(MyClientDefinitionForDan); services.AddControllers(); } @@ -44,4 +46,4 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env) app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); } } -} \ No newline at end of file +} diff --git a/samples/SampleWebApp/appsettings.json b/samples/SampleWebApp/appsettings.json index 10ef377..ac48257 100644 --- a/samples/SampleWebApp/appsettings.json +++ b/samples/SampleWebApp/appsettings.json @@ -8,7 +8,7 @@ }, "AllowedHosts": "*", "MaskinportenSettingsForDanClient": { - "Environment": "ver2", + "Environment": "test", "ClientId": "my-client-id", "Scope": "altinn:dataaltinnno", "EncodedJwk": "eyJwIjo..." diff --git a/src/Altinn.ApiClients.Dan/Interfaces/IDanApi.cs b/src/Altinn.ApiClients.Dan/Interfaces/IDanApi.cs index 6fb4276..d5b751e 100644 --- a/src/Altinn.ApiClients.Dan/Interfaces/IDanApi.cs +++ b/src/Altinn.ApiClients.Dan/Interfaces/IDanApi.cs @@ -41,6 +41,7 @@ Task GetDirectharvest( /// If set, will attempt to get supplier access token onbehalf of the authenticated party /// If true, will re-use access token supplied to DAN against the dataset source. Overrides tokenOnBehalfOfOwner /// If set, will use the supplied value as the access token against the dataset source. Overrides tokenOnBehalfOfOwner and reuseToken + /// Optional JMESPath query to filter/transform the dataset /// The dataset requested as a JSON string [Get("/directharvest/{evidenceCode}?envelope=false")] Task GetDirectharvestUnenveloped( @@ -50,7 +51,8 @@ Task GetDirectharvestUnenveloped( Dictionary parameters = null, bool tokenOnBehalfOfOwner = false, bool reuseToken = false, - [Header("X-Forward-Access-Token")] string forwardAccessToken = null); + [Header("X-Forward-Access-Token")] string forwardAccessToken = null, + string query = null); /// @@ -82,6 +84,7 @@ Task GetEvidence( /// If set, will attempt to get supplier access token onbehalf of the authenticated party /// If true, will re-use access token supplied to DAN against the dataset source. Overrides tokenOnBehalfOfOwner /// If set, will use the supplied value as the access token against the dataset source. Overrides tokenOnBehalfOfOwner and reuseToken + /// Optional JMESPath query to filter/transform the dataset /// The dataset requested as a JSON string [Get("/evidence/{accreditationId}/{evidenceCode}?envelope=false")] Task GetEvidenceUnenveloped( @@ -89,7 +92,8 @@ Task GetEvidenceUnenveloped( string evidenceCode, bool tokenOnBehalfOfOwner = false, bool reuseToken = false, - [Header("X-Forward-Access-Token")] string forwardAccessToken = null); + [Header("X-Forward-Access-Token")] string forwardAccessToken = null, + string query = null); /// /// Gets the status of all datasets requested in accreditation @@ -109,4 +113,4 @@ Task GetEvidenceUnenveloped( [Headers("Content-Type: application/json")] Task PostAuthorization([Body(buffered: true)] AuthorizationRequest authorizationRequest); } -} \ No newline at end of file +} diff --git a/src/Altinn.ApiClients.Dan/Interfaces/IDanClient.cs b/src/Altinn.ApiClients.Dan/Interfaces/IDanClient.cs index 8f4e0b5..d141c80 100644 --- a/src/Altinn.ApiClients.Dan/Interfaces/IDanClient.cs +++ b/src/Altinn.ApiClients.Dan/Interfaces/IDanClient.cs @@ -49,6 +49,7 @@ Task GetDataSet( /// If set, will attempt to get supplier access token onbehalf of the authenticated party /// If true, will re-use access token supplied to DAN against the dataset source. Overrides tokenOnBehalfOfOwner /// If set, will use the supplied value as the access token against the dataset source. Overrides tokenOnBehalfOfOwner and reuseToken + /// Optional JMESPath query to filter/transform the dataset. Ignored if deserializeField is set /// The dataset mapped to the supplied model Task GetDataSet( string dataSetName, @@ -58,7 +59,8 @@ Task GetDataSet( string deserializeField = null, bool tokenOnBehalfOfOwner = false, bool reuseToken = false, - string forwardAccessToken = null) where T : new(); + string forwardAccessToken = null, + string query = null) where T : new(); /// /// Creates a dataset request to the supplied list of datasets and optional parameters. @@ -109,6 +111,7 @@ Task GetDataSetFromAccreditation( /// If set, will attempt to get supplier access token onbehalf of the authenticated party /// If true, will re-use access token supplied to DAN against the dataset source. Overrides tokenOnBehalfOfOwner /// If set, will use the supplied value as the access token against the dataset source. Overrides tokenOnBehalfOfOwner and reuseToken + /// Optional JMESPath query to filter/transform the dataset. Ignored if deserializeField is set /// The dataset mapped to the supplied model /// Task GetDataSetFromAccreditation( @@ -117,7 +120,8 @@ Task GetDataSetFromAccreditation( string deserializeField = null, bool tokenOnBehalfOfOwner = false, bool reuseToken = false, - string forwardAccessToken = null) where T : new(); + string forwardAccessToken = null, + string query = null) where T : new(); /// /// Gets the request status for a datasets matching the supplied name within the supplied accreditation @@ -136,4 +140,4 @@ Task GetDataSetFromAccreditation( /// Task> GetRequestStatus(string accreditationGuid); } -} \ No newline at end of file +} diff --git a/src/Altinn.ApiClients.Dan/Services/DanClient.cs b/src/Altinn.ApiClients.Dan/Services/DanClient.cs index 141aec3..f536256 100644 --- a/src/Altinn.ApiClients.Dan/Services/DanClient.cs +++ b/src/Altinn.ApiClients.Dan/Services/DanClient.cs @@ -64,7 +64,8 @@ public async Task GetDataSet( string deserializeField = null, bool tokenOnBehalfOfOwner = false, bool reuseToken = false, - string forwardAccessToken = null) where T : new() + string forwardAccessToken = null, + string query = null) where T : new() { try { @@ -90,7 +91,8 @@ public async Task GetDataSet( parameters, tokenOnBehalfOfOwner, reuseToken, - forwardAccessToken); + forwardAccessToken, + query); return GetUnenvelopedDataSetAsTyped(result); } @@ -157,7 +159,8 @@ public async Task GetDataSetFromAccreditation( string deserializeField = null, bool tokenOnBehalfOfOwner = false, bool reuseToken = false, - string forwardAccessToken = null) where T : new() + string forwardAccessToken = null, + string query = null) where T : new() { try { @@ -179,7 +182,8 @@ public async Task GetDataSetFromAccreditation( datasetname, tokenOnBehalfOfOwner, reuseToken, - forwardAccessToken); + forwardAccessToken, + query); return GetUnenvelopedDataSetAsTyped(result); } @@ -272,4 +276,4 @@ public async Task> GetRequestStatus(string accreditat } } } -} \ No newline at end of file +} diff --git a/src/Altinn.ApiClients.Dan/Tests/Services/DanClientTest.cs b/src/Altinn.ApiClients.Dan/Tests/Services/DanClientTest.cs index 0e786e7..b1e7314 100644 --- a/src/Altinn.ApiClients.Dan/Tests/Services/DanClientTest.cs +++ b/src/Altinn.ApiClients.Dan/Tests/Services/DanClientTest.cs @@ -137,6 +137,7 @@ public async Task DeserializeTypedToSuppliedFieldJsonNetDefault_Ok() It.IsAny>(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny())) .ReturnsAsync("{\"SomeString\":\"Bar\",\"SomeNumber\":123,\"SomeDateTime\":\"2035_06_12\"}"); @@ -221,6 +222,7 @@ public async Task DeserializeTypedWithoutDeserializeField_Ok() It.IsAny>(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny())) .ReturnsAsync("{\"SomeString\":\"Bar\",\"SomeNumber\":123,\"SomeDateTime\":\"2021-12-12T04:56:12\"}"); @@ -341,4 +343,4 @@ internal class MyModel public decimal SomeNumber { get; set; } public DateTime SomeDateTime { get; set; } } -} \ No newline at end of file +}