From 307efd31d2fae874805f2f089189a9dd0cafae67 Mon Sep 17 00:00:00 2001 From: RobinTTY Date: Mon, 31 Jul 2023 02:42:47 +0200 Subject: [PATCH] Align error representation of the institutions error --- .../Endpoints/InstitutionsEndpointTests.cs | 4 ++-- .../InstitutionsErrorConverter.cs | 18 +++++++++++++++ .../Models/Errors/InstitutionsError.cs | 22 +++++++++++++------ .../NordigenClient.cs | 10 ++++++--- 4 files changed, 42 insertions(+), 12 deletions(-) create mode 100644 src/RobinTTY.NordigenApiClient/JsonConverters/InstitutionsErrorConverter.cs diff --git a/src/RobinTTY.NordigenApiClient.Tests/Endpoints/InstitutionsEndpointTests.cs b/src/RobinTTY.NordigenApiClient.Tests/Endpoints/InstitutionsEndpointTests.cs index 54cbdb0..ff433a4 100644 --- a/src/RobinTTY.NordigenApiClient.Tests/Endpoints/InstitutionsEndpointTests.cs +++ b/src/RobinTTY.NordigenApiClient.Tests/Endpoints/InstitutionsEndpointTests.cs @@ -80,8 +80,8 @@ public async Task GetInstitutionsForNotCoveredCountry() Assert.Multiple(() => { - Assert.That(response.Error!.Country.Detail, Is.EqualTo("US is not a valid choice.")); - Assert.That(response.Error!.Country.Summary, Is.EqualTo("Invalid country choice.")); + Assert.That(response.Error!.Detail, Is.EqualTo("US is not a valid choice.")); + Assert.That(response.Error!.Summary, Is.EqualTo("Invalid country choice.")); }); } diff --git a/src/RobinTTY.NordigenApiClient/JsonConverters/InstitutionsErrorConverter.cs b/src/RobinTTY.NordigenApiClient/JsonConverters/InstitutionsErrorConverter.cs new file mode 100644 index 0000000..d874c03 --- /dev/null +++ b/src/RobinTTY.NordigenApiClient/JsonConverters/InstitutionsErrorConverter.cs @@ -0,0 +1,18 @@ +using System.Text.Json; +using System.Text.Json.Serialization; +using RobinTTY.NordigenApiClient.Models.Errors; + +namespace RobinTTY.NordigenApiClient.JsonConverters; +internal class InstitutionsErrorConverter : JsonConverter +{ + public override InstitutionsError Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions? options) + { + var error = JsonSerializer.Deserialize(ref reader, options); + return new InstitutionsError(error!.Country); + } + + public override void Write(Utf8JsonWriter writer, InstitutionsError value, JsonSerializerOptions options) + { + writer.WriteStringValue(value.ToString()); + } +} diff --git a/src/RobinTTY.NordigenApiClient/Models/Errors/InstitutionsError.cs b/src/RobinTTY.NordigenApiClient/Models/Errors/InstitutionsError.cs index fe23424..be67e76 100644 --- a/src/RobinTTY.NordigenApiClient/Models/Errors/InstitutionsError.cs +++ b/src/RobinTTY.NordigenApiClient/Models/Errors/InstitutionsError.cs @@ -5,20 +5,28 @@ namespace RobinTTY.NordigenApiClient.Models.Errors; /// /// An error description as returned by the institutions endpoint of the Nordigen API. /// -public class InstitutionsError +public class InstitutionsError : BasicError { /// - /// The error related to the requested institutions. + /// Creates a new instance of . /// + /// The error related to the requested institutions. + [JsonConstructor] + public InstitutionsError(BasicError country) : base(country.Summary, country.Detail) {} +} + +/// +/// Representation of the institutions error as returned by the Nordigen API. +/// Since this representation doesn't add any useful information (only extra encapsulation) +/// it is transformed to align this error with other errors returned by the API. +/// +internal class InstitutionsErrorInternal +{ [JsonPropertyName("country")] public BasicError Country { get; } - /// - /// Creates a new instance of . - /// - /// The error related to the requested institutions. [JsonConstructor] - public InstitutionsError(BasicError country) + public InstitutionsErrorInternal(BasicError country) { Country = country; } diff --git a/src/RobinTTY.NordigenApiClient/NordigenClient.cs b/src/RobinTTY.NordigenApiClient/NordigenClient.cs index e3570db..4b86ea4 100644 --- a/src/RobinTTY.NordigenApiClient/NordigenClient.cs +++ b/src/RobinTTY.NordigenApiClient/NordigenClient.cs @@ -63,7 +63,11 @@ public NordigenClient(HttpClient httpClient, NordigenClientCredentials credentia _httpClient = httpClient; _serializerOptions = new JsonSerializerOptions { - Converters = { new JsonWebTokenConverter(), new GuidConverter(), new CultureSpecificDecimalConverter() } + Converters = + { + new JsonWebTokenConverter(), new GuidConverter(), + new CultureSpecificDecimalConverter(), new InstitutionsErrorConverter() + } }; Credentials = credentials; @@ -96,14 +100,14 @@ internal async Task> MakeRequest