Skip to content

Commit

Permalink
Align error representation of the institutions error
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinTTY committed Jul 31, 2023
1 parent 6a666e0 commit 307efd3
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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."));
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<InstitutionsError>
{
public override InstitutionsError Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions? options)
{
var error = JsonSerializer.Deserialize<InstitutionsErrorInternal>(ref reader, options);
return new InstitutionsError(error!.Country);
}

public override void Write(Utf8JsonWriter writer, InstitutionsError value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString());
}
}
22 changes: 15 additions & 7 deletions src/RobinTTY.NordigenApiClient/Models/Errors/InstitutionsError.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,28 @@ namespace RobinTTY.NordigenApiClient.Models.Errors;
/// <summary>
/// An error description as returned by the institutions endpoint of the Nordigen API.
/// </summary>
public class InstitutionsError
public class InstitutionsError : BasicError
{
/// <summary>
/// The error related to the requested institutions.
/// Creates a new instance of <see cref="InstitutionsError"/>.
/// </summary>
/// <param name="country">The error related to the requested institutions.</param>
[JsonConstructor]
public InstitutionsError(BasicError country) : base(country.Summary, country.Detail) {}
}

/// <summary>
/// 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.
/// </summary>
internal class InstitutionsErrorInternal
{
[JsonPropertyName("country")]
public BasicError Country { get; }

/// <summary>
/// Creates a new instance of <see cref="InstitutionsError"/>.
/// </summary>
/// <param name="country">The error related to the requested institutions.</param>
[JsonConstructor]
public InstitutionsError(BasicError country)
public InstitutionsErrorInternal(BasicError country)
{
Country = country;
}
Expand Down
10 changes: 7 additions & 3 deletions src/RobinTTY.NordigenApiClient/NordigenClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -96,14 +100,14 @@ internal async Task<NordigenApiResponse<TResponse, TError>> MakeRequest<TRespons
client = _httpClient;
}

HttpResponseMessage ? response;
HttpResponseMessage? response;
if (method == HttpMethod.Get)
response = await client.GetAsync(requestUri, cancellationToken);
else if (method == HttpMethod.Post)
response = await client.PostAsync(requestUri, body, cancellationToken);
else if (method == HttpMethod.Delete)
response = await client.DeleteAsync(requestUri, cancellationToken);
else if(method == HttpMethod.Put)
else if (method == HttpMethod.Put)
response = await client.PutAsync(requestUri, body, cancellationToken);
else
throw new NotImplementedException();
Expand Down

0 comments on commit 307efd3

Please sign in to comment.