using System.Threading.Channels; using ErsatzTV.Core; using ErsatzTV.Core.Interfaces.Plex; namespace ErsatzTV.Application.Plex; public class StartPlexPinFlowHandler : IRequestHandler> { private readonly ChannelWriter _channel; private readonly IPlexTvApiClient _plexTvApiClient; public StartPlexPinFlowHandler( IPlexTvApiClient plexTvApiClient, ChannelWriter channel) { _plexTvApiClient = plexTvApiClient; _channel = channel; } public Task> Handle( StartPlexPinFlow request, CancellationToken cancellationToken) => _plexTvApiClient.StartPinFlow().Bind( result => result.Match( Left: error => Task.FromResult(Left(error)), Right: async pin => { await _channel.WriteAsync(new TryCompletePlexPinFlow(pin), cancellationToken); return Right(pin.Url); }) ); }