diff --git a/source/VMelnalksnis.NordigenDotNet/Tokens/AccessToken.cs b/source/VMelnalksnis.NordigenDotNet/Tokens/AccessToken.cs index 4148651..b884d45 100644 --- a/source/VMelnalksnis.NordigenDotNet/Tokens/AccessToken.cs +++ b/source/VMelnalksnis.NordigenDotNet/Tokens/AccessToken.cs @@ -6,6 +6,17 @@ using System.Text.Json.Serialization; namespace VMelnalksnis.NordigenDotNet.Tokens; -internal record AccessToken( - string Access, - [property: JsonPropertyName("access_expires")] int AccessExpires); +internal class AccessToken +{ + [JsonConstructor] + public AccessToken(string access, int accessExpires) + { + Access = access; + AccessExpires = accessExpires; + } + + public string Access { get; } + + [JsonPropertyName("access_expires")] + public int AccessExpires { get; } +} diff --git a/source/VMelnalksnis.NordigenDotNet/Tokens/Token.cs b/source/VMelnalksnis.NordigenDotNet/Tokens/Token.cs index f83110d..bbe1b82 100644 --- a/source/VMelnalksnis.NordigenDotNet/Tokens/Token.cs +++ b/source/VMelnalksnis.NordigenDotNet/Tokens/Token.cs @@ -6,8 +6,18 @@ using System.Text.Json.Serialization; namespace VMelnalksnis.NordigenDotNet.Tokens; -internal record Token( - string Access, - int AccessExpires, - string Refresh, - [property: JsonPropertyName("refresh_expires")] int RefreshExpires) : AccessToken(Access, AccessExpires); +internal class Token : AccessToken +{ + [JsonConstructor] + public Token(string access, int accessExpires, string refresh, int refreshExpires) + : base(access, accessExpires) + { + Refresh = refresh; + RefreshExpires = refreshExpires; + } + + public string Refresh { get; } + + [JsonPropertyName("refresh_expires")] + public int RefreshExpires { get; } +} diff --git a/source/VMelnalksnis.NordigenDotNet/Tokens/TokenClient.cs b/source/VMelnalksnis.NordigenDotNet/Tokens/TokenClient.cs index 8c9ae79..5cf28c8 100644 --- a/source/VMelnalksnis.NordigenDotNet/Tokens/TokenClient.cs +++ b/source/VMelnalksnis.NordigenDotNet/Tokens/TokenClient.cs @@ -6,14 +6,20 @@ using System.Net; using System.Net.Http; using System.Net.Http.Json; using System.Text.Json; +using System.Text.Json.Serialization.Metadata; using System.Threading.Tasks; namespace VMelnalksnis.NordigenDotNet.Tokens; internal sealed class TokenClient { + private static readonly TokenSerializationContext _context = new(new(JsonSerializerDefaults.Web)); + private static readonly JsonTypeInfo _accessTokenInfo = (JsonTypeInfo)_context.GetTypeInfo(typeof(AccessToken)); + private static readonly JsonTypeInfo _tokenInfo = (JsonTypeInfo)_context.GetTypeInfo(typeof(Token)); + private static readonly JsonTypeInfo _tokenCreationInfo = (JsonTypeInfo)_context.GetTypeInfo(typeof(TokenCreation)); + private static readonly JsonTypeInfo _tokenRefreshInfo = (JsonTypeInfo)_context.GetTypeInfo(typeof(TokenRefresh)); + private readonly HttpClient _httpClient; - private readonly JsonSerializerOptions _serializerOptions = new(JsonSerializerDefaults.Web); internal TokenClient(HttpClient httpClient) { @@ -23,7 +29,7 @@ internal sealed class TokenClient internal async Task New(TokenCreation tokenCreation) { var tokenResponse = await _httpClient - .PostAsJsonAsync(Routes.Tokens.New, tokenCreation, _serializerOptions) + .PostAsJsonAsync(Routes.Tokens.New, tokenCreation, _tokenCreationInfo) .ConfigureAwait(false); if (tokenResponse.StatusCode is not HttpStatusCode.OK) @@ -32,28 +38,23 @@ internal sealed class TokenClient throw new HttpRequestException(content); } - var token = await tokenResponse.Content - .ReadFromJsonAsync(_serializerOptions) - .ConfigureAwait(false); - + var token = await tokenResponse.Content.ReadFromJsonAsync(_tokenInfo).ConfigureAwait(false); return token!; } internal async Task Refresh(TokenRefresh tokenRefresh) { var tokenResponse = await _httpClient - .PostAsJsonAsync(Routes.Tokens.Refresh, tokenRefresh, _serializerOptions) + .PostAsJsonAsync(Routes.Tokens.Refresh, tokenRefresh, _tokenRefreshInfo) .ConfigureAwait(false); if (tokenResponse.StatusCode is not HttpStatusCode.OK) { var content = await tokenResponse.Content.ReadAsStringAsync().ConfigureAwait(false); - throw new HttpRequestException(content); + throw new HttpRequestException(content, null, tokenResponse.StatusCode); } - var accessToken = await tokenResponse.Content - .ReadFromJsonAsync(_serializerOptions) - .ConfigureAwait(false); + var accessToken = await tokenResponse.Content.ReadFromJsonAsync(_accessTokenInfo).ConfigureAwait(false); return accessToken!; } diff --git a/source/VMelnalksnis.NordigenDotNet/Tokens/TokenCreation.cs b/source/VMelnalksnis.NordigenDotNet/Tokens/TokenCreation.cs index 6e1934e..b997a69 100644 --- a/source/VMelnalksnis.NordigenDotNet/Tokens/TokenCreation.cs +++ b/source/VMelnalksnis.NordigenDotNet/Tokens/TokenCreation.cs @@ -6,6 +6,18 @@ using System.Text.Json.Serialization; namespace VMelnalksnis.NordigenDotNet.Tokens; -internal sealed record TokenCreation( - [property: JsonPropertyName("secret_id")] string SecretId, - [property: JsonPropertyName("secret_key")] string SecretKey); +internal sealed class TokenCreation +{ + [JsonConstructor] + public TokenCreation(string secretId, string secretKey) + { + SecretId = secretId; + SecretKey = secretKey; + } + + [JsonPropertyName("secret_id")] + public string SecretId { get; } + + [JsonPropertyName("secret_key")] + public string SecretKey { get; } +} diff --git a/source/VMelnalksnis.NordigenDotNet/Tokens/TokenRefresh.cs b/source/VMelnalksnis.NordigenDotNet/Tokens/TokenRefresh.cs index b6dc601..0726b23 100644 --- a/source/VMelnalksnis.NordigenDotNet/Tokens/TokenRefresh.cs +++ b/source/VMelnalksnis.NordigenDotNet/Tokens/TokenRefresh.cs @@ -2,6 +2,17 @@ // Licensed under the Apache License 2.0. // See LICENSE file in the project root for full license information. +using System.Text.Json.Serialization; + namespace VMelnalksnis.NordigenDotNet.Tokens; -internal sealed record TokenRefresh(string Refresh); +internal sealed class TokenRefresh +{ + [JsonConstructor] + public TokenRefresh(string refresh) + { + Refresh = refresh; + } + + public string Refresh { get; } +} diff --git a/source/VMelnalksnis.NordigenDotNet/Tokens/TokenSerializationContext.cs b/source/VMelnalksnis.NordigenDotNet/Tokens/TokenSerializationContext.cs new file mode 100644 index 0000000..7de9286 --- /dev/null +++ b/source/VMelnalksnis.NordigenDotNet/Tokens/TokenSerializationContext.cs @@ -0,0 +1,16 @@ +// Copyright 2022 Valters Melnalksnis +// Licensed under the Apache License 2.0. +// See LICENSE file in the project root for full license information. + +using System.Text.Json.Serialization; + +namespace VMelnalksnis.NordigenDotNet.Tokens; + +/// +[JsonSerializable(typeof(AccessToken))] +[JsonSerializable(typeof(Token))] +[JsonSerializable(typeof(TokenCreation))] +[JsonSerializable(typeof(TokenRefresh))] +internal partial class TokenSerializationContext : JsonSerializerContext +{ +}