2965270928
- ApiResponse: add readToBuffer option to defer/stream body instead of eagerly buffering - TableDataProvider: implement HTML table parser with per-column provider support - StealthConfig: add 10s page load timeout and copyCookiesFrom parameter for cookie sharing - StealthUnitDownloader: catch WebDriverTimeoutException on navigation, log warning instead of throwing - Bump version to 2.9.0
113 lines
4.4 KiB
C#
113 lines
4.4 KiB
C#
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;
|
|
/// <summary>
|
|
/// Wrapper that lets the response body be read any number of times (even concurrently).
|
|
/// </summary>
|
|
public sealed class ApiResponse {
|
|
private byte[] _buffer;
|
|
private bool _read_has_been_deferred;
|
|
|
|
private ApiResponse(HttpResponseMessage response, byte[] buffer, ILogger<ApiResponse>? 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<ApiResponse>? Logger { get; }
|
|
|
|
/* ---------- creation ---------- */
|
|
|
|
public static async Task<ApiResponse> CreateAsync(
|
|
HttpResponseMessage response,
|
|
ILogger<ApiResponse>? 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<HttpStatusCode> 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<T?> AsSerializedObject<T>(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<T>(await Response.Content!.ReadAsStreamAsync(ct), (JsonSerializerOptions?)null, ct);
|
|
} else {
|
|
return JsonSerializer.Deserialize<T>(_buffer);
|
|
}
|
|
}
|
|
|
|
public Task<T?> AsDynamicObject<T>(T _, CancellationToken ct = default)
|
|
=> AsSerializedObject<T>(ct);
|
|
|
|
public async Task<string> 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<byte[]> 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<Stream> 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);
|
|
}
|
|
}
|
|
}
|