Use AllDebridNetClientFactory so AD client can be dependency-injected

This commit is contained in:
Cucumberrbob 2025-02-17 21:56:32 +00:00
parent fe241db5db
commit 95307f2933
No known key found for this signature in database
GPG key ID: 2B935C47401C3614
2 changed files with 26 additions and 17 deletions

View file

@ -16,6 +16,7 @@ public static class DiConfig
public static void RegisterRdtServices(this IServiceCollection services) public static void RegisterRdtServices(this IServiceCollection services)
{ {
services.AddSingleton<IAllDebridNetClientFactory, AllDebridNetClientFactory>();
services.AddScoped<AllDebridTorrentClient>(); services.AddScoped<AllDebridTorrentClient>();
services.AddScoped<Authentication>(); services.AddScoped<Authentication>();
services.AddScoped<Downloads>(); services.AddScoped<Downloads>();

View file

@ -12,13 +12,14 @@ using Torrent = RdtClient.Data.Models.Data.Torrent;
namespace RdtClient.Service.Services.TorrentClients; namespace RdtClient.Service.Services.TorrentClients;
public class AllDebridTorrentClient(ILogger<AllDebridTorrentClient> logger, IHttpClientFactory httpClientFactory) : ITorrentClient public interface IAllDebridNetClientFactory
{ {
private static readonly Int64 SessionId = DateTimeOffset.UtcNow.ToUnixTimeSeconds(); public IAllDebridNETClient GetClient();
private static List<TorrentClientTorrent> _cache = []; }
private static Int64 _sessionCounter = 0;
private AllDebridNETClient GetClient() public class AllDebridNetClientFactory(ILogger<AllDebridNetClientFactory> logger, IHttpClientFactory httpClientFactory) : IAllDebridNetClientFactory
{
public IAllDebridNETClient GetClient()
{ {
try try
{ {
@ -49,6 +50,13 @@ public class AllDebridTorrentClient(ILogger<AllDebridTorrentClient> logger, IHtt
throw; throw;
} }
} }
}
public class AllDebridTorrentClient(ILogger<AllDebridTorrentClient> logger, IAllDebridNetClientFactory allDebridNetClientFactory) : ITorrentClient
{
private static readonly Int64 SessionId = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
private static List<TorrentClientTorrent> _cache = [];
private static Int64 _sessionCounter = 0;
private static TorrentClientTorrent Map(Magnet torrent) private static TorrentClientTorrent Map(Magnet torrent)
{ {
@ -77,7 +85,7 @@ public class AllDebridTorrentClient(ILogger<AllDebridTorrentClient> logger, IHtt
public async Task<IList<TorrentClientTorrent>> GetTorrents() public async Task<IList<TorrentClientTorrent>> GetTorrents()
{ {
var results = await GetClient().Magnet.StatusLiveAsync(SessionId, _sessionCounter); var results = await allDebridNetClientFactory.GetClient().Magnet.StatusLiveAsync(SessionId, _sessionCounter);
_sessionCounter = results.Counter; _sessionCounter = results.Counter;
@ -106,7 +114,7 @@ public class AllDebridTorrentClient(ILogger<AllDebridTorrentClient> logger, IHtt
public async Task<TorrentClientUser> GetUser() public async Task<TorrentClientUser> GetUser()
{ {
var user = await GetClient().User.GetAsync() ?? throw new("Unable to get user"); var user = await allDebridNetClientFactory.GetClient().User.GetAsync() ?? throw new("Unable to get user");
return new() return new()
{ {
@ -117,7 +125,7 @@ public class AllDebridTorrentClient(ILogger<AllDebridTorrentClient> logger, IHtt
public async Task<String> AddMagnet(String magnetLink) public async Task<String> AddMagnet(String magnetLink)
{ {
var result = await GetClient().Magnet.UploadMagnetAsync(magnetLink); var result = await allDebridNetClientFactory.GetClient().Magnet.UploadMagnetAsync(magnetLink);
if (result?.Id == null) if (result?.Id == null)
{ {
@ -131,7 +139,7 @@ public class AllDebridTorrentClient(ILogger<AllDebridTorrentClient> logger, IHtt
public async Task<String> AddFile(Byte[] bytes) public async Task<String> AddFile(Byte[] bytes)
{ {
var result = await GetClient().Magnet.UploadFileAsync(bytes); var result = await allDebridNetClientFactory.GetClient().Magnet.UploadFileAsync(bytes);
if (result?.Id == null) if (result?.Id == null)
{ {
@ -155,12 +163,12 @@ public class AllDebridTorrentClient(ILogger<AllDebridTorrentClient> logger, IHtt
public async Task Delete(String torrentId) public async Task Delete(String torrentId)
{ {
await GetClient().Magnet.DeleteAsync(torrentId); await allDebridNetClientFactory.GetClient().Magnet.DeleteAsync(torrentId);
} }
public async Task<String> Unrestrict(String link) public async Task<String> Unrestrict(String link)
{ {
var result = await GetClient().Links.DownloadLinkAsync(link); var result = await allDebridNetClientFactory.GetClient().Links.DownloadLinkAsync(link);
if (result.Link == null) if (result.Link == null)
{ {
@ -245,7 +253,7 @@ public class AllDebridTorrentClient(ILogger<AllDebridTorrentClient> logger, IHtt
return null; return null;
} }
var allFiles = await GetClient().Magnet.FilesAsync(Int64.Parse(torrent.RdId)); var allFiles = await allDebridNetClientFactory.GetClient().Magnet.FilesAsync(Int64.Parse(torrent.RdId));
var files = GetFiles(allFiles); var files = GetFiles(allFiles);
@ -341,7 +349,7 @@ public class AllDebridTorrentClient(ILogger<AllDebridTorrentClient> logger, IHtt
private async Task<TorrentClientTorrent> GetInfo(String torrentId) private async Task<TorrentClientTorrent> GetInfo(String torrentId)
{ {
var result = await GetClient().Magnet.StatusAsync(torrentId) ?? throw new($"Unable to find magnet with ID {torrentId}"); var result = await allDebridNetClientFactory.GetClient().Magnet.StatusAsync(torrentId) ?? throw new($"Unable to find magnet with ID {torrentId}");
return Map(result); return Map(result);
} }