From b67f85cd85fd5cb9f105caae597019b9bc25444b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20H=C3=A4felfinger?= Date: Thu, 31 Aug 2023 22:51:09 +0200 Subject: [PATCH] makes piwigo client factory injectable --- .../Infrastructure/IPiwigoClientFactory.cs | 9 +++++++++ .../Infrastructure/PiwigoClientFactory.cs | 13 ++++++++++--- PiwigoDirectorySync/Program.cs | 1 + PiwigoDirectorySync/Services/AlbumSynchronizer.cs | 8 ++++---- 4 files changed, 24 insertions(+), 7 deletions(-) create mode 100644 PiwigoDirectorySync/Infrastructure/IPiwigoClientFactory.cs diff --git a/PiwigoDirectorySync/Infrastructure/IPiwigoClientFactory.cs b/PiwigoDirectorySync/Infrastructure/IPiwigoClientFactory.cs new file mode 100644 index 0000000..9b7aeee --- /dev/null +++ b/PiwigoDirectorySync/Infrastructure/IPiwigoClientFactory.cs @@ -0,0 +1,9 @@ +using Piwigo.Client; +using PiwigoDirectorySync.Persistence; + +namespace PiwigoDirectorySync.Infrastructure; + +public interface IPiwigoClientFactory +{ + Task GetPiwigoClientAsync(ServerEntity piwigoServer, CancellationToken ct); +} \ No newline at end of file diff --git a/PiwigoDirectorySync/Infrastructure/PiwigoClientFactory.cs b/PiwigoDirectorySync/Infrastructure/PiwigoClientFactory.cs index 8899b52..f5b440b 100644 --- a/PiwigoDirectorySync/Infrastructure/PiwigoClientFactory.cs +++ b/PiwigoDirectorySync/Infrastructure/PiwigoClientFactory.cs @@ -4,11 +4,18 @@ using PiwigoDirectorySync.Persistence; namespace PiwigoDirectorySync.Infrastructure; -public static class PiwigoClientFactory +public class PiwigoClientFactory : IPiwigoClientFactory { - public static async Task 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 GetPiwigoClientAsync(ServerEntity piwigoServer, CancellationToken ct) + { + var piwigoClient = PiwigoClient.CreateClient(piwigoServer.Url, piwigoServer.Username, piwigoServer.Password, _loggerFactory); await piwigoClient.Session.LoginAsync(ct); return piwigoClient; } diff --git a/PiwigoDirectorySync/Program.cs b/PiwigoDirectorySync/Program.cs index a2a8ba2..54a5a97 100644 --- a/PiwigoDirectorySync/Program.cs +++ b/PiwigoDirectorySync/Program.cs @@ -13,6 +13,7 @@ registrations.AddLogging(l => l.AddSimpleConsole(c => }) .AddDebug()); +registrations.AddTransient(); registrations.AddTransient(); registrations.AddTransient(); registrations.AddTransient(); diff --git a/PiwigoDirectorySync/Services/AlbumSynchronizer.cs b/PiwigoDirectorySync/Services/AlbumSynchronizer.cs index 714af34..74d52e6 100644 --- a/PiwigoDirectorySync/Services/AlbumSynchronizer.cs +++ b/PiwigoDirectorySync/Services/AlbumSynchronizer.cs @@ -9,12 +9,12 @@ namespace PiwigoDirectorySync.Services; public class AlbumSynchronizer : IAlbumSynchronizer { private readonly ILogger _logger; - private readonly ILoggerFactory _loggerFactory; + private readonly IPiwigoClientFactory _piwigoClientFactory; - public AlbumSynchronizer(ILogger logger, ILoggerFactory loggerFactory) + public AlbumSynchronizer(ILogger logger, IPiwigoClientFactory piwigoClientFactory) { _logger = logger; - _loggerFactory = loggerFactory; + _piwigoClientFactory = piwigoClientFactory; } public async Task SynchronizeAlbums(int piwigoServerId, CancellationToken ct) @@ -27,7 +27,7 @@ public class AlbumSynchronizer : IAlbumSynchronizer return; } - var piwigoClient = await PiwigoClientFactory.GetPiwigoClientAsync(piwigoServer, _loggerFactory, ct); + var piwigoClient = await _piwigoClientFactory.GetPiwigoClientAsync(piwigoServer, ct); await UpdatePiwigoAlbumsFromServerAsync(dbContext, piwigoClient, piwigoServer, ct); }