Add refined replay draft API client
Create lives with RefineReplay and RetentionDays, save and publish drafts, delete published originals and read the owner-only draft source by bounded, verified byte ranges. PodcastReplayMediaInput can stream the draft source. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
parent
e9ffbc78be
commit
4493ac0149
5 changed files with 76 additions and 7 deletions
|
|
@ -7,6 +7,7 @@ using Avalonia.Headless;
|
|||
using Avalonia.Headless.XUnit;
|
||||
using Avalonia.Interactivity;
|
||||
using PostIt.Services;
|
||||
using Yavsc.Abstract.Streaming;
|
||||
using Yavsc.Api.Client;
|
||||
|
||||
namespace PostIt.Tests;
|
||||
|
|
@ -138,6 +139,31 @@ public sealed class PodcastReplayTests
|
|||
api.DownloadReplayRangeAsync(Id, 0, 256 * 1024 + 1, transport.Bytes.Length, _ct));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Draft_client_targets_owner_routes_and_reads_the_draft_source_by_range()
|
||||
{
|
||||
await using var transport = new RangeTransport();
|
||||
var api = new PodcastApiClient(transport, () => "https://api.example/");
|
||||
var prefix = $"https://api.example/api/v1/podcasts";
|
||||
await api.CreateAsync(new CreatePodcastRequest { Title = "Live", RefineReplay = true, RetentionDays = 0 }, _ct);
|
||||
var save = new SavePodcastDraftRequest { Revision = 2, Marks = [1000], RemovedRanges = [new(0, 500)] };
|
||||
await api.SaveDraftAsync(Id, save, _ct);
|
||||
await api.PublishDraftAsync(Id, 3, _ct);
|
||||
await api.DeleteOriginalsAsync(Id, _ct);
|
||||
Assert.Equal([$"POST {prefix}", $"PUT {prefix}/{Id}/draft", $"POST {prefix}/{Id}/draft/publish",
|
||||
$"DELETE {prefix}/{Id}/originals"], transport.Calls.Select(call => call.Call));
|
||||
var create = Assert.IsType<CreatePodcastRequest>(transport.Calls[0].Body);
|
||||
Assert.True(create.RefineReplay);
|
||||
Assert.Equal(0, create.RetentionDays);
|
||||
Assert.Same(save, transport.Calls[1].Body);
|
||||
Assert.Equal(3, Assert.IsType<PublishPodcastDraftRequest>(transport.Calls[2].Body).Revision);
|
||||
Assert.Null(transport.Calls[3].Body);
|
||||
|
||||
Assert.Equal(transport.Bytes.Skip(5).Take(4), await api.DownloadDraftSourceRangeAsync(Id, 5, 4, transport.Bytes.Length, _ct));
|
||||
Assert.Equal($"{prefix}/{Id}/draft/source.mp4", transport.Url);
|
||||
Assert.Equal("bytes=5-8", transport.Range);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("full")]
|
||||
[InlineData("range")]
|
||||
|
|
@ -315,7 +341,12 @@ public sealed class PodcastReplayTests
|
|||
Failure == "range" ? from + 1 : from, to, Failure == "size" ? Bytes.Length + 1 : Bytes.Length);
|
||||
return Task.FromResult(response);
|
||||
}
|
||||
public Task<T> CallAsync<T>(HttpMethod method, string path, object? body = null, CancellationToken ct = default) => throw new NotSupportedException();
|
||||
public List<(string Call, object? Body)> Calls { get; } = [];
|
||||
public Task<T> CallAsync<T>(HttpMethod method, string path, object? body = null, CancellationToken ct = default)
|
||||
{
|
||||
Calls.Add(($"{method} {path}", body));
|
||||
return Task.FromResult(default(T)!);
|
||||
}
|
||||
public Task<T> CallAsync<T>(HttpMethod method, string path, Func<HttpContent> factory, CancellationToken ct = default) => throw new NotSupportedException();
|
||||
public Task CallAsync(HttpMethod method, string path, object? body = null, CancellationToken ct = default) => throw new NotSupportedException();
|
||||
public Task CallAsync(HttpMethod method, string path, Func<HttpContent> factory, CancellationToken ct = default) => throw new NotSupportedException();
|
||||
|
|
|
|||
|
|
@ -11,17 +11,20 @@ public sealed class PodcastReplayMediaInput : MediaInput
|
|||
private readonly PodcastApiClient _api;
|
||||
private readonly Guid _id;
|
||||
private readonly long _length;
|
||||
private readonly bool _draftSource;
|
||||
private readonly CancellationTokenSource _cancellation = new();
|
||||
private byte[] _buffer = [];
|
||||
private long _bufferStart;
|
||||
private long _position;
|
||||
public event Action<string>? Failed;
|
||||
|
||||
public PodcastReplayMediaInput(PodcastApiClient api, Guid id, long length)
|
||||
/// <param name="draftSource">Reads the owner-only draft source instead of the published MP4.</param>
|
||||
public PodcastReplayMediaInput(PodcastApiClient api, Guid id, long length, bool draftSource = false)
|
||||
{
|
||||
_api = api;
|
||||
_id = id;
|
||||
_length = length;
|
||||
_draftSource = draftSource;
|
||||
CanSeek = true;
|
||||
}
|
||||
|
||||
|
|
@ -50,7 +53,9 @@ public sealed class PodcastReplayMediaInput : MediaInput
|
|||
{
|
||||
using var timeout = CancellationTokenSource.CreateLinkedTokenSource(_cancellation.Token);
|
||||
timeout.CancelAfter(TimeSpan.FromSeconds(20));
|
||||
_buffer = _api.DownloadReplayRangeAsync(_id, _position, 256 * 1024, _length, timeout.Token)
|
||||
_buffer = (_draftSource
|
||||
? _api.DownloadDraftSourceRangeAsync(_id, _position, 256 * 1024, _length, timeout.Token)
|
||||
: _api.DownloadReplayRangeAsync(_id, _position, 256 * 1024, _length, timeout.Token))
|
||||
.GetAwaiter().GetResult();
|
||||
_bufferStart = _position;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,7 +19,11 @@ public sealed class PodcastApiClient(IYavscApiClient api, Func<string> baseAddre
|
|||
=> api.CallAsync<List<PodcastResponse>>(HttpMethod.Get, Absolute(Prefix), ct: ct);
|
||||
|
||||
public Task<PodcastResponse> CreateAsync(string title, CancellationToken ct = default)
|
||||
=> api.CallAsync<PodcastResponse>(HttpMethod.Post, Absolute(Prefix), new CreatePodcastRequest { Title = title }, ct);
|
||||
=> CreateAsync(new CreatePodcastRequest { Title = title }, ct);
|
||||
|
||||
/// <summary>Starts an immediate live, optionally refined with its retention.</summary>
|
||||
public Task<PodcastResponse> CreateAsync(CreatePodcastRequest request, CancellationToken ct = default)
|
||||
=> api.CallAsync<PodcastResponse>(HttpMethod.Post, Absolute(Prefix), request, ct);
|
||||
|
||||
public Task<PodcastResponse> PrepareAsync(PreparePodcastRequest request, Guid? id = null, CancellationToken ct = default)
|
||||
=> api.CallAsync<PodcastResponse>(id.HasValue ? HttpMethod.Put : HttpMethod.Post,
|
||||
|
|
@ -56,15 +60,36 @@ public sealed class PodcastApiClient(IYavscApiClient api, Func<string> baseAddre
|
|||
public Task RetryReplayAsync(Guid id, CancellationToken ct = default)
|
||||
=> api.CallAsync(HttpMethod.Post, Absolute($"{Prefix}/{id}/replay/retry"), ct: ct);
|
||||
|
||||
public async Task<byte[]> DownloadReplayRangeAsync(Guid id, long offset, int count, long size,
|
||||
/// <summary>Saves the marks and removed ranges of the owned draft; a stale revision fails with 409.</summary>
|
||||
public Task<PodcastResponse> SaveDraftAsync(Guid id, SavePodcastDraftRequest request, CancellationToken ct = default)
|
||||
=> api.CallAsync<PodcastResponse>(HttpMethod.Put, Absolute($"{Prefix}/{id}/draft"), request, ct);
|
||||
|
||||
/// <summary>Queues the publication of a saved draft revision; repeating it is harmless.</summary>
|
||||
public Task<PodcastResponse> PublishDraftAsync(Guid id, int revision, CancellationToken ct = default)
|
||||
=> api.CallAsync<PodcastResponse>(HttpMethod.Post, Absolute($"{Prefix}/{id}/draft/publish"),
|
||||
new PublishPodcastDraftRequest { Revision = revision }, ct);
|
||||
|
||||
/// <summary>Deletes the originals and draft of a published refined replay; the MP4 remains.</summary>
|
||||
public Task<PodcastResponse> DeleteOriginalsAsync(Guid id, CancellationToken ct = default)
|
||||
=> api.CallAsync<PodcastResponse>(HttpMethod.Delete, Absolute($"{Prefix}/{id}/originals"), ct: ct);
|
||||
|
||||
public Task<byte[]> DownloadReplayRangeAsync(Guid id, long offset, int count, long size,
|
||||
CancellationToken ct = default)
|
||||
=> DownloadRangeAsync($"{Prefix}/{id}/replay.mp4", offset, count, size, ct);
|
||||
|
||||
/// <summary>Reads a byte range of the owner-only seekable source of a refined replay draft.</summary>
|
||||
public Task<byte[]> DownloadDraftSourceRangeAsync(Guid id, long offset, int count, long size,
|
||||
CancellationToken ct = default)
|
||||
=> DownloadRangeAsync($"{Prefix}/{id}/draft/source.mp4", offset, count, size, ct);
|
||||
|
||||
private async Task<byte[]> DownloadRangeAsync(string path, long offset, int count, long size, CancellationToken ct)
|
||||
{
|
||||
if (offset < 0 || count <= 0 || count > 256 * 1024 || offset >= size)
|
||||
throw new ArgumentOutOfRangeException(nameof(offset), "Invalid replay byte range.");
|
||||
var end = offset + Math.Min(count, size - offset) - 1;
|
||||
using var response = await api.SendAuthenticatedAsync(() =>
|
||||
{
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, Absolute($"{Prefix}/{id}/replay.mp4"));
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, Absolute(path));
|
||||
request.Headers.Range = new RangeHeaderValue(offset, end);
|
||||
return request;
|
||||
}, ct).ConfigureAwait(false);
|
||||
|
|
|
|||
Loading…
Reference in a new issue