#region ========================================================================= USING ===================================================================================== using System; using System.IO; using System.Net; using System.Net.Http; using System.Threading; using System.Threading.Tasks; #endregion namespace Lumina.Presentation.Web.Common.Api; /// /// Represents the streamed response of a remote API request. It exposes the status code, the range related headers and the /// content stream of the response, so that a caller can forward the response to a client without ever buffering its content. /// public sealed class ApiStreamResult : IAsyncDisposable { private readonly HttpResponseMessage _response; /// /// Gets the status code of the response. /// public HttpStatusCode StatusCode { get; } /// /// Gets the content stream of the response. /// public Stream Content { get; } /// /// Gets the media type of the content of the response, or when the response declares none. /// public string? ContentType { get; } /// /// Gets the length of the content of the response, or when the response declares none. /// public long? ContentLength { get; } /// /// Gets the range of the content of the response, when the response is a partial content response. /// public string? ContentRange { get; } /// /// Gets the ranges the remote API accepts, when the response declares them. /// public string? AcceptRanges { get; } /// /// Gets the entity tag of the content of the response, when the response declares one. /// public string? ETag { get; } /// /// Gets the date and time the content of the response was last modified, when the response declares it. /// public DateTimeOffset? LastModified { get; } /// /// Gets the caching instructions of the response, when the response declares them. /// public string? CacheControl { get; } /// /// Initializes a new instance of the class. /// /// The response returned by the remote API. /// The content stream of the response. private ApiStreamResult(HttpResponseMessage response, Stream content) { _response = response; Content = content; StatusCode = response.StatusCode; ContentType = response.Content.Headers.ContentType?.ToString(); ContentLength = response.Content.Headers.ContentLength; ContentRange = response.Content.Headers.ContentRange?.ToString(); AcceptRanges = response.Headers.AcceptRanges.Count > 0 ? string.Join(", ", response.Headers.AcceptRanges) : null; ETag = response.Headers.ETag?.ToString(); LastModified = response.Content.Headers.LastModified; CacheControl = response.Headers.CacheControl?.ToString(); } /// /// Creates a from a response, without buffering the content of the response. /// /// The response returned by the remote API. /// Cancellation token that can be used to stop the execution. /// The streamed response. public static async Task CreateAsync(HttpResponseMessage response, CancellationToken cancellationToken) { Stream content = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false); return new ApiStreamResult(response, content); } /// /// Disposes the content stream and the response it belongs to. /// public async ValueTask DisposeAsync() { await Content.DisposeAsync().ConfigureAwait(false); _response.Dispose(); } }