feat: Add TokenSerializationContext for TokenClient

This commit is contained in:
Valters Melnalksnis
2022-06-15 21:12:41 +03:00
parent 93ef992e23
commit 364b6044cf
6 changed files with 84 additions and 23 deletions
@@ -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; }
}
@@ -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; }
}
@@ -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<AccessToken> _accessTokenInfo = (JsonTypeInfo<AccessToken>)_context.GetTypeInfo(typeof(AccessToken));
private static readonly JsonTypeInfo<Token> _tokenInfo = (JsonTypeInfo<Token>)_context.GetTypeInfo(typeof(Token));
private static readonly JsonTypeInfo<TokenCreation> _tokenCreationInfo = (JsonTypeInfo<TokenCreation>)_context.GetTypeInfo(typeof(TokenCreation));
private static readonly JsonTypeInfo<TokenRefresh> _tokenRefreshInfo = (JsonTypeInfo<TokenRefresh>)_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<Token> 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<Token>(_serializerOptions)
.ConfigureAwait(false);
var token = await tokenResponse.Content.ReadFromJsonAsync(_tokenInfo).ConfigureAwait(false);
return token!;
}
internal async Task<AccessToken> 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<AccessToken>(_serializerOptions)
.ConfigureAwait(false);
var accessToken = await tokenResponse.Content.ReadFromJsonAsync(_accessTokenInfo).ConfigureAwait(false);
return accessToken!;
}
@@ -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; }
}
@@ -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; }
}
@@ -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;
/// <inheritdoc />
[JsonSerializable(typeof(AccessToken))]
[JsonSerializable(typeof(Token))]
[JsonSerializable(typeof(TokenCreation))]
[JsonSerializable(typeof(TokenRefresh))]
internal partial class TokenSerializationContext : JsonSerializerContext
{
}