diff --git a/src/Ombi.Core/Authentication/PlexTokenKeepAliveService.cs b/src/Ombi.Core/Authentication/PlexTokenKeepAliveService.cs new file mode 100644 index 000000000..d29da4a5a --- /dev/null +++ b/src/Ombi.Core/Authentication/PlexTokenKeepAliveService.cs @@ -0,0 +1,52 @@ +using System; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Extensions.Logging; +using Ombi.Api.Plex; + +namespace Ombi.Core.Authentication +{ + public interface IPlexTokenKeepAliveService + { + Task KeepTokenAliveAsync(string token, CancellationToken cancellationToken); + } + + public class PlexTokenKeepAliveService : IPlexTokenKeepAliveService + { + private readonly IPlexApi _plexApi; + private readonly ILogger _logger; + + public PlexTokenKeepAliveService(IPlexApi plexApi, ILogger logger) + { + _plexApi = plexApi; + _logger = logger; + } + + public async Task KeepTokenAliveAsync(string token, CancellationToken cancellationToken) + { + try + { + if (string.IsNullOrEmpty(token)) + { + _logger.LogWarning("Token is null or empty"); + return false; + } + + // Use the Ping method to validate the token + var isValid = await _plexApi.Ping(token, cancellationToken); + + if (!isValid) + { + _logger.LogWarning("Token validation failed - token may be expired or invalid"); + } + + return isValid; + } + catch (Exception ex) + { + _logger.LogError(ex, "Error occurred while keeping token alive"); + return false; + } + } + } +} \ No newline at end of file diff --git a/src/Ombi.Schedule/Jobs/Plex/PlexWatchlistImport.cs b/src/Ombi.Schedule/Jobs/Plex/PlexWatchlistImport.cs index 61151a56f..3de332879 100644 --- a/src/Ombi.Schedule/Jobs/Plex/PlexWatchlistImport.cs +++ b/src/Ombi.Schedule/Jobs/Plex/PlexWatchlistImport.cs @@ -27,7 +27,6 @@ using Ombi.Core.Notifications; using Microsoft.AspNetCore.Identity; using Ombi.Store.Repository.Requests; using Ombi.Core; -using Ombi.Core.Authentication; namespace Ombi.Schedule.Jobs.Plex {