fix: Move all instances of throw new HttpRequestException to single method

This commit is contained in:
Valters Melnalksnis
2022-06-15 21:17:21 +03:00
parent 364b6044cf
commit 99ea02654e
3 changed files with 29 additions and 34 deletions
@@ -0,0 +1,22 @@
// Copyright 2022 Valters Melnalksnis
// Licensed under the Apache License 2.0.
// See LICENSE file in the project root for full license information.
using System.Net.Http;
using System.Threading.Tasks;
namespace VMelnalksnis.NordigenDotNet;
internal static class HttpResponseMessageExtensions
{
internal static async Task ThrowIfNotSuccessful(this HttpResponseMessage response)
{
if (response.IsSuccessStatusCode)
{
return;
}
var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
throw new HttpRequestException(content, null, response.StatusCode);
}
}
@@ -86,39 +86,23 @@ public sealed class NordigenHttpClient
{
var client = await GetClient().ConfigureAwait(false);
var response = await client.PostAsJsonAsync(requestUri, request, _serializerOptions).ConfigureAwait(false);
if (response.IsSuccessStatusCode)
{
return await response.Content.ReadFromJsonAsync<TResult>(_serializerOptions).ConfigureAwait(false);
}
var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
throw new HttpRequestException(content, null, response.StatusCode);
await response.ThrowIfNotSuccessful().ConfigureAwait(false);
return await response.Content.ReadFromJsonAsync<TResult>(_serializerOptions).ConfigureAwait(false);
}
internal async Task<TResult?> PutAsJson<TRequest, TResult>(string requestUri, TRequest request)
{
var client = await GetClient().ConfigureAwait(false);
var response = await client.PutAsJsonAsync(requestUri, request, _serializerOptions).ConfigureAwait(false);
if (response.IsSuccessStatusCode)
{
return await response.Content.ReadFromJsonAsync<TResult>(_serializerOptions).ConfigureAwait(false);
}
var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
throw new HttpRequestException(content, null, response.StatusCode);
await response.ThrowIfNotSuccessful().ConfigureAwait(false);
return await response.Content.ReadFromJsonAsync<TResult>(_serializerOptions).ConfigureAwait(false);
}
internal async Task Delete(string requestUri)
{
var client = await GetClient().ConfigureAwait(false);
var response = await client.DeleteAsync(requestUri).ConfigureAwait(false);
if (response.IsSuccessStatusCode)
{
return;
}
var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
throw new HttpRequestException(content, null, response.StatusCode);
await response.ThrowIfNotSuccessful().ConfigureAwait(false);
}
private async Task<HttpClient> GetClient()
@@ -2,7 +2,6 @@
// Licensed under the Apache License 2.0.
// See LICENSE file in the project root for full license information.
using System.Net;
using System.Net.Http;
using System.Net.Http.Json;
using System.Text.Json;
@@ -32,12 +31,7 @@ internal sealed class TokenClient
.PostAsJsonAsync(Routes.Tokens.New, tokenCreation, _tokenCreationInfo)
.ConfigureAwait(false);
if (tokenResponse.StatusCode is not HttpStatusCode.OK)
{
var content = await tokenResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
throw new HttpRequestException(content);
}
await tokenResponse.ThrowIfNotSuccessful().ConfigureAwait(false);
var token = await tokenResponse.Content.ReadFromJsonAsync(_tokenInfo).ConfigureAwait(false);
return token!;
}
@@ -48,12 +42,7 @@ internal sealed class TokenClient
.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, null, tokenResponse.StatusCode);
}
await tokenResponse.ThrowIfNotSuccessful().ConfigureAwait(false);
var accessToken = await tokenResponse.Content.ReadFromJsonAsync(_accessTokenInfo).ConfigureAwait(false);
return accessToken!;