makes piwigo client factory injectable

This commit is contained in:
Philipp Häfelfinger 2023-08-31 22:51:09 +02:00
parent c06e8763a4
commit b67f85cd85
4 changed files with 24 additions and 7 deletions

View File

@ -0,0 +1,9 @@
using Piwigo.Client;
using PiwigoDirectorySync.Persistence;
namespace PiwigoDirectorySync.Infrastructure;
public interface IPiwigoClientFactory
{
Task<IPiwigoClient> GetPiwigoClientAsync(ServerEntity piwigoServer, CancellationToken ct);
}

View File

@ -4,11 +4,18 @@ using PiwigoDirectorySync.Persistence;
namespace PiwigoDirectorySync.Infrastructure; namespace PiwigoDirectorySync.Infrastructure;
public static class PiwigoClientFactory public class PiwigoClientFactory : IPiwigoClientFactory
{ {
public static async Task<IPiwigoClient> GetPiwigoClientAsync(ServerEntity piwigoServer, ILoggerFactory loggerFactory, CancellationToken ct) private readonly ILoggerFactory _loggerFactory;
public PiwigoClientFactory(ILoggerFactory loggerFactory)
{ {
var piwigoClient = PiwigoClient.CreateClient(piwigoServer.Url, piwigoServer.Username, piwigoServer.Password, loggerFactory); _loggerFactory = loggerFactory;
}
public async Task<IPiwigoClient> GetPiwigoClientAsync(ServerEntity piwigoServer, CancellationToken ct)
{
var piwigoClient = PiwigoClient.CreateClient(piwigoServer.Url, piwigoServer.Username, piwigoServer.Password, _loggerFactory);
await piwigoClient.Session.LoginAsync(ct); await piwigoClient.Session.LoginAsync(ct);
return piwigoClient; return piwigoClient;
} }

View File

@ -13,6 +13,7 @@ registrations.AddLogging(l => l.AddSimpleConsole(c =>
}) })
.AddDebug()); .AddDebug());
registrations.AddTransient<IPiwigoClientFactory, PiwigoClientFactory>();
registrations.AddTransient<IFileIndexer, FileIndexer>(); registrations.AddTransient<IFileIndexer, FileIndexer>();
registrations.AddTransient<IFileSystemScanner, FileSystemScanner>(); registrations.AddTransient<IFileSystemScanner, FileSystemScanner>();
registrations.AddTransient<IAlbumSynchronizer, AlbumSynchronizer>(); registrations.AddTransient<IAlbumSynchronizer, AlbumSynchronizer>();

View File

@ -9,12 +9,12 @@ namespace PiwigoDirectorySync.Services;
public class AlbumSynchronizer : IAlbumSynchronizer public class AlbumSynchronizer : IAlbumSynchronizer
{ {
private readonly ILogger<AlbumSynchronizer> _logger; private readonly ILogger<AlbumSynchronizer> _logger;
private readonly ILoggerFactory _loggerFactory; private readonly IPiwigoClientFactory _piwigoClientFactory;
public AlbumSynchronizer(ILogger<AlbumSynchronizer> logger, ILoggerFactory loggerFactory) public AlbumSynchronizer(ILogger<AlbumSynchronizer> logger, IPiwigoClientFactory piwigoClientFactory)
{ {
_logger = logger; _logger = logger;
_loggerFactory = loggerFactory; _piwigoClientFactory = piwigoClientFactory;
} }
public async Task SynchronizeAlbums(int piwigoServerId, CancellationToken ct) public async Task SynchronizeAlbums(int piwigoServerId, CancellationToken ct)
@ -27,7 +27,7 @@ public class AlbumSynchronizer : IAlbumSynchronizer
return; return;
} }
var piwigoClient = await PiwigoClientFactory.GetPiwigoClientAsync(piwigoServer, _loggerFactory, ct); var piwigoClient = await _piwigoClientFactory.GetPiwigoClientAsync(piwigoServer, ct);
await UpdatePiwigoAlbumsFromServerAsync(dbContext, piwigoClient, piwigoServer, ct); await UpdatePiwigoAlbumsFromServerAsync(dbContext, piwigoClient, piwigoServer, ct);
} }