using System; using System.IO; using Microsoft.Extensions.Logging; using System.Net; using System.Net.Http; using System.Net.Http.Json; using System.Text; using System.Text.Json; using System.Threading; using System.Threading.Tasks; namespace Beam.Api; /// /// Wrapper that lets the response body be read any number of times (even concurrently). /// public sealed class ApiResponse { private byte[] _buffer; private bool _read_has_been_deferred; private ApiResponse(HttpResponseMessage response, byte[] buffer, ILogger? logger, object? requestData = null) { Response = response; _buffer = buffer; _read_has_been_deferred = _buffer.Length == 0; Logger = logger; RequestData = requestData; } public HttpResponseMessage Response { get; } public object? RequestData { get; } public ILogger? Logger { get; } /* ---------- creation ---------- */ public static async Task CreateAsync( HttpResponseMessage response, ILogger? logger = null, object? requestData = null, bool readToBuffer = true, CancellationToken ct = default) { if (response is null) throw new ArgumentNullException(nameof(response)); if (!readToBuffer) return new ApiResponse(response, [], logger, requestData); var buffer = response.Content is null ? [] : await response.Content.ReadAsByteArrayAsync(ct).ConfigureAwait(false); return new ApiResponse(response, buffer, logger, requestData); } /* ---------- status helpers ---------- */ public bool Is404 => Response.StatusCode == HttpStatusCode.NotFound; public bool Is403 => Response.StatusCode == HttpStatusCode.Forbidden; public bool Is500 => Response.StatusCode == HttpStatusCode.InternalServerError; public bool Is400 => Response.StatusCode == HttpStatusCode.BadRequest; public bool Is200 => Response.IsSuccessStatusCode; public ApiResponse OnError(Action errorHandler) { if (!Is200) errorHandler(Response.StatusCode); return this; } /* ---------- content helpers ---------- */ private async Task ReadToBuffer(CancellationToken ct = default) { if (!_read_has_been_deferred) return; _buffer = Response.Content is null ? [] : await Response.Content.ReadAsByteArrayAsync(ct).ConfigureAwait(false); _read_has_been_deferred = false; } public async Task AsSerializedObject(CancellationToken ct = default) { if (!Is200) throw new InvalidOperationException(); if (Response.Content?.Headers.ContentType?.MediaType != "application/json") Logger?.LogWarning("Content-Type is not JSON, yet JSON deserialization was requested."); if (_read_has_been_deferred) { return await JsonSerializer.DeserializeAsync(await Response.Content!.ReadAsStreamAsync(ct), (JsonSerializerOptions?)null, ct); } else { return JsonSerializer.Deserialize(_buffer); } } public Task AsDynamicObject(T _, CancellationToken ct = default) => AsSerializedObject(ct); public async Task AsString(CancellationToken ct = default) { if (!Is200) Logger?.LogWarning("Non-success response; attempting to read content."); if (_read_has_been_deferred) { await ReadToBuffer(ct); } return Encoding.UTF8.GetString(_buffer); } public async Task AsBinary(CancellationToken ct = default) { if (!Is200) Logger?.LogWarning("Non-success response; attempting to read content."); if (_read_has_been_deferred) { await ReadToBuffer(ct); } return _buffer; } public async Task AsStream(CancellationToken ct = default) { if (!Is200) Logger?.LogWarning("Non-success response; attempting to read content."); if (_read_has_been_deferred) { return await Response.Content!.ReadAsStreamAsync(ct); } else { return new MemoryStream(_buffer, writable: false); } } }