Skip to content

Commit

Permalink
Add JMESPath support
Browse files Browse the repository at this point in the history
  • Loading branch information
elsand committed Oct 9, 2023
1 parent 87f4c48 commit 4f7c85b
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 19 deletions.
16 changes: 13 additions & 3 deletions samples/SampleWebApp/Controllers/DanClientTestController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ public async Task<ActionResult> Get(string datasetname, string subject, [FromQue
UnitBasicInformation ubi =
await _danClient.GetDataSet<UnitBasicInformation>(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<CustomUnitBasicInformation>(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");
}
Expand Down Expand Up @@ -152,9 +156,15 @@ public static string ToHtmlTable(this List<DataSetRequestStatus> 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; }

Expand Down Expand Up @@ -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; }

Expand Down
8 changes: 5 additions & 3 deletions samples/SampleWebApp/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ namespace SampleWebApp
{
public class Startup
{
private const string MyClientDefinitionForDan = "my-client-definition-for-dan";

public Startup(IConfiguration configuration)
{
Configuration = configuration;
Expand All @@ -20,12 +22,12 @@ public Startup(IConfiguration configuration)

public void ConfigureServices(IServiceCollection services)
{
services.RegisterMaskinportenClientDefinition<SettingsJwkClientDefinition>("my-client-definition-for-dan",
services.RegisterMaskinportenClientDefinition<SettingsJwkClientDefinition>(MyClientDefinitionForDan,
Configuration.GetSection("MaskinportenSettingsForDanClient"));

services
.AddDanClient(Configuration.GetSection("DanSettings"))
.AddMaskinportenHttpMessageHandler<SettingsJwkClientDefinition>("my-client-definition-for-dan");
.AddMaskinportenHttpMessageHandler<SettingsJwkClientDefinition>(MyClientDefinitionForDan);

services.AddControllers();
}
Expand All @@ -44,4 +46,4 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
}
}
}
}
2 changes: 1 addition & 1 deletion samples/SampleWebApp/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"AllowedHosts": "*",
"MaskinportenSettingsForDanClient": {
"Environment": "ver2",
"Environment": "test",
"ClientId": "my-client-id",
"Scope": "altinn:dataaltinnno",
"EncodedJwk": "eyJwIjo..."
Expand Down
10 changes: 7 additions & 3 deletions src/Altinn.ApiClients.Dan/Interfaces/IDanApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Task<DataSet> GetDirectharvest(
/// <param name="tokenOnBehalfOfOwner">If set, will attempt to get supplier access token onbehalf of the authenticated party</param>
/// <param name="reuseToken">If true, will re-use access token supplied to DAN against the dataset source. Overrides <c>tokenOnBehalfOfOwner</c></param>
/// <param name="forwardAccessToken">If set, will use the supplied value as the access token against the dataset source. Overrides <c>tokenOnBehalfOfOwner</c> and <c>reuseToken</c></param>
/// <param name="query">Optional JMESPath query to filter/transform the dataset</param>
/// <returns>The dataset requested as a JSON string</returns>
[Get("/directharvest/{evidenceCode}?envelope=false")]
Task<string> GetDirectharvestUnenveloped(
Expand All @@ -50,7 +51,8 @@ Task<string> GetDirectharvestUnenveloped(
Dictionary<string, string> 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);


/// <summary>
Expand Down Expand Up @@ -82,14 +84,16 @@ Task<DataSet> GetEvidence(
/// <param name="tokenOnBehalfOfOwner">If set, will attempt to get supplier access token onbehalf of the authenticated party</param>
/// <param name="reuseToken">If true, will re-use access token supplied to DAN against the dataset source. Overrides <c>tokenOnBehalfOfOwner</c></param>
/// <param name="forwardAccessToken">If set, will use the supplied value as the access token against the dataset source. Overrides <c>tokenOnBehalfOfOwner</c> and <c>reuseToken</c></param>
/// <param name="query">Optional JMESPath query to filter/transform the dataset</param>
/// <returns>The dataset requested as a JSON string</returns>
[Get("/evidence/{accreditationId}/{evidenceCode}?envelope=false")]
Task<string> GetEvidenceUnenveloped(
string accreditationId,
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);

/// <summary>
/// Gets the status of all datasets requested in accreditation
Expand All @@ -109,4 +113,4 @@ Task<string> GetEvidenceUnenveloped(
[Headers("Content-Type: application/json")]
Task<Accreditation> PostAuthorization([Body(buffered: true)] AuthorizationRequest authorizationRequest);
}
}
}
10 changes: 7 additions & 3 deletions src/Altinn.ApiClients.Dan/Interfaces/IDanClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Task<DataSet> GetDataSet(
/// <param name="tokenOnBehalfOfOwner">If set, will attempt to get supplier access token onbehalf of the authenticated party</param>
/// <param name="reuseToken">If true, will re-use access token supplied to DAN against the dataset source. Overrides <c>tokenOnBehalfOfOwner</c></param>
/// <param name="forwardAccessToken">If set, will use the supplied value as the access token against the dataset source. Overrides <c>tokenOnBehalfOfOwner</c> and <c>reuseToken</c></param>
/// <param name="query">Optional JMESPath query to filter/transform the dataset. Ignored if <c>deserializeField</c> is set</param>
/// <returns>The dataset mapped to the supplied model</returns>
Task<T> GetDataSet<T>(
string dataSetName,
Expand All @@ -58,7 +59,8 @@ Task<T> GetDataSet<T>(
string deserializeField = null,
bool tokenOnBehalfOfOwner = false,
bool reuseToken = false,
string forwardAccessToken = null) where T : new();
string forwardAccessToken = null,
string query = null) where T : new();

/// <summary>
/// Creates a dataset request to the supplied list of datasets and optional parameters.
Expand Down Expand Up @@ -109,6 +111,7 @@ Task<DataSet> GetDataSetFromAccreditation(
/// <param name="tokenOnBehalfOfOwner">If set, will attempt to get supplier access token onbehalf of the authenticated party</param>
/// <param name="reuseToken">If true, will re-use access token supplied to DAN against the dataset source. Overrides <c>tokenOnBehalfOfOwner</c></param>
/// <param name="forwardAccessToken">If set, will use the supplied value as the access token against the dataset source. Overrides <c>tokenOnBehalfOfOwner</c> and <c>reuseToken</c></param>
/// <param name="query">Optional JMESPath query to filter/transform the dataset. Ignored if <c>deserializeField</c> is set</param>
/// <returns>The dataset mapped to the supplied model</returns>
/// <exception cref="DanException"></exception>
Task<T> GetDataSetFromAccreditation<T>(
Expand All @@ -117,7 +120,8 @@ Task<T> GetDataSetFromAccreditation<T>(
string deserializeField = null,
bool tokenOnBehalfOfOwner = false,
bool reuseToken = false,
string forwardAccessToken = null) where T : new();
string forwardAccessToken = null,
string query = null) where T : new();

/// <summary>
/// Gets the request status for a datasets matching the supplied name within the supplied accreditation
Expand All @@ -136,4 +140,4 @@ Task<T> GetDataSetFromAccreditation<T>(
/// <exception cref="DanException"></exception>
Task<List<DataSetRequestStatus>> GetRequestStatus(string accreditationGuid);
}
}
}
14 changes: 9 additions & 5 deletions src/Altinn.ApiClients.Dan/Services/DanClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ public async Task<T> GetDataSet<T>(
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
{
Expand All @@ -90,7 +91,8 @@ public async Task<T> GetDataSet<T>(
parameters,
tokenOnBehalfOfOwner,
reuseToken,
forwardAccessToken);
forwardAccessToken,
query);

return GetUnenvelopedDataSetAsTyped<T>(result);
}
Expand Down Expand Up @@ -157,7 +159,8 @@ public async Task<T> GetDataSetFromAccreditation<T>(
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
{
Expand All @@ -179,7 +182,8 @@ public async Task<T> GetDataSetFromAccreditation<T>(
datasetname,
tokenOnBehalfOfOwner,
reuseToken,
forwardAccessToken);
forwardAccessToken,
query);

return GetUnenvelopedDataSetAsTyped<T>(result);
}
Expand Down Expand Up @@ -272,4 +276,4 @@ public async Task<List<DataSetRequestStatus>> GetRequestStatus(string accreditat
}
}
}
}
}
4 changes: 3 additions & 1 deletion src/Altinn.ApiClients.Dan/Tests/Services/DanClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ public async Task DeserializeTypedToSuppliedFieldJsonNetDefault_Ok()
It.IsAny<Dictionary<string, string>>(),
It.IsAny<bool>(),
It.IsAny<bool>(),
It.IsAny<string>(),
It.IsAny<string>()))
.ReturnsAsync("{\"SomeString\":\"Bar\",\"SomeNumber\":123,\"SomeDateTime\":\"2035_06_12\"}");

Expand Down Expand Up @@ -221,6 +222,7 @@ public async Task DeserializeTypedWithoutDeserializeField_Ok()
It.IsAny<Dictionary<string, string>>(),
It.IsAny<bool>(),
It.IsAny<bool>(),
It.IsAny<string>(),
It.IsAny<string>()))
.ReturnsAsync("{\"SomeString\":\"Bar\",\"SomeNumber\":123,\"SomeDateTime\":\"2021-12-12T04:56:12\"}");

Expand Down Expand Up @@ -341,4 +343,4 @@ internal class MyModel
public decimal SomeNumber { get; set; }
public DateTime SomeDateTime { get; set; }
}
}
}

0 comments on commit 4f7c85b

Please sign in to comment.