mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-02 06:01:10 +00:00
Adds music stack test coverage and fixes playlist cache and stream error handling
This commit is contained in:
1 parent
394f6b510c
commit
aba803b106
197 files changed
+29081
-34
No files matched your search
+4
@@ -51,6 +51,10 @@ public class AddPlaylistEntriesEndpoint : BaseEndpoint<AddPlaylistEntriesRequest
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
public override async Task<IResult> ExecuteAsync(AddPlaylistEntriesRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
// The playlists belong to the authenticated user, so no intermediary may store any response of this route, including its problem
|
||||
// responses, and every response varies by the bearer token that authorized it.
|
||||
HttpContext.Response.Headers["Cache-Control"] = "private, no-store";
|
||||
HttpContext.Response.Headers["Vary"] = "Authorization";
|
||||
// The playlist is identified by the route value, not by any id posted in the body.
|
||||
string? playlistId = HttpContext.Request.RouteValues["playlistId"]?.ToString();
|
||||
Result<PlaylistEntriesAddedResponse> result = await _addPlaylistEntriesCommandHandler.HandleAsync(request.ToCommand(playlistId), cancellationToken).ConfigureAwait(false);
|
||||
|
||||
+4
@@ -51,6 +51,10 @@ public class AddPlaylistEntryEndpoint : BaseEndpoint<AddPlaylistEntryRequest, IR
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
public override async Task<IResult> ExecuteAsync(AddPlaylistEntryRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
// The playlists belong to the authenticated user, so no intermediary may store any response of this route, including its problem
|
||||
// responses, and every response varies by the bearer token that authorized it.
|
||||
HttpContext.Response.Headers["Cache-Control"] = "private, no-store";
|
||||
HttpContext.Response.Headers["Vary"] = "Authorization";
|
||||
// The playlist is identified by the route value, not by any id posted in the body.
|
||||
string? playlistId = HttpContext.Request.RouteValues["playlistId"]?.ToString();
|
||||
Result<PlaylistEntryAddedResponse> result = await _addPlaylistEntryCommandHandler.HandleAsync(request.ToCommand(playlistId), cancellationToken).ConfigureAwait(false);
|
||||
|
||||
+4
@@ -51,6 +51,10 @@ public class AddPlaylistScopeEntriesEndpoint : BaseEndpoint<AddPlaylistScopeEntr
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
public override async Task<IResult> ExecuteAsync(AddPlaylistScopeEntriesRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
// The playlists belong to the authenticated user, so no intermediary may store any response of this route, including its problem
|
||||
// responses, and every response varies by the bearer token that authorized it.
|
||||
HttpContext.Response.Headers["Cache-Control"] = "private, no-store";
|
||||
HttpContext.Response.Headers["Vary"] = "Authorization";
|
||||
// The playlist is identified by the route value, not by any id posted in the body.
|
||||
string? playlistId = HttpContext.Request.RouteValues["playlistId"]?.ToString();
|
||||
Result<PlaylistEntriesAddedResponse> result = await _addPlaylistScopeEntriesCommandHandler.HandleAsync(request.ToCommand(playlistId), cancellationToken).ConfigureAwait(false);
|
||||
|
||||
+4
@@ -51,6 +51,10 @@ public class CreatePlaylistEndpoint : BaseEndpoint<CreatePlaylistRequest, IResul
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
public override async Task<IResult> ExecuteAsync(CreatePlaylistRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
// The playlists belong to the authenticated user, so no intermediary may store any response of this route, including its problem
|
||||
// responses, and every response varies by the bearer token that authorized it.
|
||||
HttpContext.Response.Headers["Cache-Control"] = "private, no-store";
|
||||
HttpContext.Response.Headers["Vary"] = "Authorization";
|
||||
Result<PlaylistResponse> result = await _createPlaylistCommandHandler.HandleAsync(request.ToCommand(), cancellationToken).ConfigureAwait(false);
|
||||
return result.Match(success => TypedResults.Ok(success), Problem);
|
||||
}
|
||||
|
||||
+4
@@ -48,6 +48,10 @@ public class DeletePlaylistEndpoint : BaseEndpoint<FastEndpoints.EmptyRequest, I
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
public override async Task<IResult> ExecuteAsync(FastEndpoints.EmptyRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
// The playlists belong to the authenticated user, so no intermediary may store any response of this route, including its problem
|
||||
// responses, and every response varies by the bearer token that authorized it.
|
||||
HttpContext.Response.Headers["Cache-Control"] = "private, no-store";
|
||||
HttpContext.Response.Headers["Vary"] = "Authorization";
|
||||
// Take the unique identifier from the route.
|
||||
string? playlistId = HttpContext.Request.RouteValues["playlistId"]?.ToString();
|
||||
Result<Deleted> result = await _deletePlaylistCommandHandler.HandleAsync(new DeletePlaylistCommand(playlistId), cancellationToken).ConfigureAwait(false);
|
||||
|
||||
+4
@@ -51,6 +51,10 @@ public class MovePlaylistEntryEndpoint : BaseEndpoint<MovePlaylistEntryRequest,
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
public override async Task<IResult> ExecuteAsync(MovePlaylistEntryRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
// The playlists belong to the authenticated user, so no intermediary may store any response of this route, including its problem
|
||||
// responses, and every response varies by the bearer token that authorized it.
|
||||
HttpContext.Response.Headers["Cache-Control"] = "private, no-store";
|
||||
HttpContext.Response.Headers["Vary"] = "Authorization";
|
||||
// The playlist and the entry are identified by the route values, not by any id posted in the body.
|
||||
string? playlistId = HttpContext.Request.RouteValues["playlistId"]?.ToString();
|
||||
string? playlistEntryId = HttpContext.Request.RouteValues["playlistEntryId"]?.ToString();
|
||||
|
||||
+4
@@ -49,6 +49,10 @@ public class RemovePlaylistEntryEndpoint : BaseEndpoint<FastEndpoints.EmptyReque
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
public override async Task<IResult> ExecuteAsync(FastEndpoints.EmptyRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
// The playlists belong to the authenticated user, so no intermediary may store any response of this route, including its problem
|
||||
// responses, and every response varies by the bearer token that authorized it.
|
||||
HttpContext.Response.Headers["Cache-Control"] = "private, no-store";
|
||||
HttpContext.Response.Headers["Vary"] = "Authorization";
|
||||
// Take the unique identifiers from the route.
|
||||
string? playlistId = HttpContext.Request.RouteValues["playlistId"]?.ToString();
|
||||
string? playlistEntryId = HttpContext.Request.RouteValues["playlistEntryId"]?.ToString();
|
||||
|
||||
+4
@@ -51,6 +51,10 @@ public class UpdatePlaylistEndpoint : BaseEndpoint<UpdatePlaylistRequest, IResul
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
public override async Task<IResult> ExecuteAsync(UpdatePlaylistRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
// The playlists belong to the authenticated user, so no intermediary may store any response of this route, including its problem
|
||||
// responses, and every response varies by the bearer token that authorized it.
|
||||
HttpContext.Response.Headers["Cache-Control"] = "private, no-store";
|
||||
HttpContext.Response.Headers["Vary"] = "Authorization";
|
||||
// The playlist is identified by the route value, not by any id posted in the body, so that a mismatched id can never redirect the
|
||||
// update to another playlist.
|
||||
string? playlistId = HttpContext.Request.RouteValues["playlistId"]?.ToString();
|
||||
|
||||
+16
-3
@@ -2,6 +2,7 @@
|
||||
using FastEndpoints;
|
||||
using Lumina.Presentation.Web.Common.Api;
|
||||
using Lumina.Presentation.Web.Common.DependencyInjection;
|
||||
using Lumina.Presentation.Web.Common.Exceptions;
|
||||
using Lumina.Presentation.Web.Common.Http;
|
||||
using Lumina.Presentation.Web.Common.Requests.Library.AudioLibrary.MusicLibrary.Tracks;
|
||||
using Lumina.Presentation.Web.Common.Routes;
|
||||
@@ -11,6 +12,7 @@ using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Primitives;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
@@ -75,10 +77,21 @@ public class StreamMusicTrackEndpoint : BaseEndpoint<StreamMusicTrackRequest, IR
|
||||
AddHeader(forwardHeaders, "If-Modified-Since", HttpContext.Request.Headers.IfModifiedSince);
|
||||
|
||||
// The API response is disposed once the copy ends, so that the underlying connection is released as soon as the client stops reading.
|
||||
await using (ApiStreamResult streamResult = await _streamingApiHttpClient.GetStreamAsync(endpoint, forwardHeaders, cancellationToken).ConfigureAwait(false))
|
||||
{ // The proxy owns the status, header, caching and body rules shared with the normalized segment proxy.
|
||||
await ApiStreamProxy.WriteAsync(HttpContext.Response, streamResult, cancellationToken).ConfigureAwait(false);
|
||||
ApiStreamResult streamResult;
|
||||
try
|
||||
{
|
||||
streamResult = await _streamingApiHttpClient.GetStreamAsync(endpoint, forwardHeaders, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
catch (ApiException apiException) when (apiException.HttpStatusCode is HttpStatusCode.ServiceUnavailable or HttpStatusCode.TooManyRequests)
|
||||
{
|
||||
// A busy normalization pipeline or the stream concurrency limit is an expected, transient condition that the player retries. It is
|
||||
// answered with a bare status instead of being logged as an unexpected API failure with a stack trace.
|
||||
HttpContext.Response.StatusCode = (int)apiException.HttpStatusCode;
|
||||
HttpContext.Response.ContentLength = 0;
|
||||
return Results.Empty;
|
||||
}
|
||||
await using (streamResult) // The proxy owns the status, header, caching and body rules shared with the normalized segment proxy.
|
||||
await ApiStreamProxy.WriteAsync(HttpContext.Response, streamResult, cancellationToken).ConfigureAwait(false);
|
||||
return Results.Empty;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user