mirror of
https://dev.azure.com/tstanciu94/PhantomMind/_git/Bitip
synced 2025-10-13 01:52:19 +03:00
25 lines
991 B
C#
25 lines
991 B
C#
using Bitip.Client.Models;
|
|
using System.Net.Http;
|
|
using System.Net.Http.Json;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Bitip.Client.Extensions
|
|
{
|
|
internal static class HttpMessageExtensions
|
|
{
|
|
public static async Task<HttpResponseMessage> EnsureSuccessOperation(this HttpResponseMessage response, CancellationToken cancellationToken = default)
|
|
{
|
|
if (response.IsSuccessStatusCode)
|
|
return response;
|
|
var errorResponse = await response.Content.ReadFromJsonAsync<ErrorResponse>(cancellationToken);
|
|
if (errorResponse is null)
|
|
throw new HttpRequestException("An error occurred while fetching Bitip information.");
|
|
var message = $"{errorResponse.Error}: {errorResponse.Message}";
|
|
if (!string.IsNullOrWhiteSpace(errorResponse.Ip))
|
|
message += $" (IP: {errorResponse.Ip})";
|
|
throw new HttpRequestException(message);
|
|
}
|
|
}
|
|
}
|