diff --git a/PiwigoDotnet/Piwigo.Client/AlbumApi.cs b/PiwigoDotnet/Piwigo.Client/AlbumApi.cs index 2db1c08..6443a95 100644 --- a/PiwigoDotnet/Piwigo.Client/AlbumApi.cs +++ b/PiwigoDotnet/Piwigo.Client/AlbumApi.cs @@ -15,22 +15,28 @@ public class AlbumApi : IAlbumApi _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } - public async Task CalculateOrphansAsync(int albumId) + public async Task DeleteRepresentativeAsync(int albumId, CancellationToken cancellationToken = default) { var formParams = new Dictionary { { "category_id", albumId.ToString() } }; - var response = await _context.PostAsync>(_logger, "pwg.categories.calculateOrphans", formParams); + await _context.PostAsync(_logger, "pwg.categories.deleteRepresentative", formParams, cancellationToken); + } + + public async Task CalculateOrphansAsync(int albumId, CancellationToken cancellationToken = default) + { + var formParams = new Dictionary { { "category_id", albumId.ToString() } }; + var response = await _context.PostAsync>(_logger, "pwg.categories.calculateOrphans", formParams, cancellationToken); // the API seems to only return one result but returns it as a list in json return response.Result.First(); } - public async Task DeleteAsync(int albumId, string apiToken) + public async Task DeleteAsync(int albumId, string apiToken, CancellationToken cancellationToken = default) { var formParams = new Dictionary { { "category_id", albumId.ToString() }, { "pwg_token", apiToken }, { "photo_deletion_mode", "delete_orphans" } }; - var response = await _context.PostAsync(_logger, "pwg.categories.delete", formParams); + await _context.PostAsync(_logger, "pwg.categories.delete", formParams, cancellationToken); } public async Task AddAsync(string name, int? parentId = null, string? comment = null, bool? visible = null, CategoryStatus? status = null, bool? commentable = null, - CategoryPosition? position = null) + CategoryPosition? position = null, CancellationToken cancellationToken = default) { var statusValue = status switch { @@ -50,9 +56,9 @@ public class AlbumApi : IAlbumApi var formParams = new Dictionary { { "name", name } }; formParams.AddIfValueNotNull("parent", parentId?.ToString()).AddIfValueNotNull("comment", comment).AddIfValueNotNull("visible", visible?.ToString()) - .AddIfValueNotNull("status", statusValue).AddIfValueNotNull("commentable", visible?.ToString()).AddIfValueNotNull("position", positionValue); + .AddIfValueNotNull("status", statusValue).AddIfValueNotNull("commentable", commentable?.ToString()).AddIfValueNotNull("position", positionValue); - var response = await _context.PostAsync>(_logger, "pwg.categories.add", formParams); + var response = await _context.PostAsync>(_logger, "pwg.categories.add", formParams, cancellationToken); if (!response.Result.Id.HasValue) { throw new PiwigoException($"Could not create album {name}: {response.Result.Info}"); @@ -61,10 +67,10 @@ public class AlbumApi : IAlbumApi return response.Result.Id.Value; } - public async Task> GetAllAsync() + public async Task> GetAllAsync(CancellationToken cancellationToken = default) { var formParams = new Dictionary { { "recursive", "true" } }; - var response = await _context.PostAsync>(_logger, "pwg.categories.getList", formParams); + var response = await _context.PostAsync>(_logger, "pwg.categories.getList", formParams, cancellationToken); return new ReadOnlyCollection(response.Result.Albums); } } \ No newline at end of file diff --git a/PiwigoDotnet/Piwigo.Client/IAlbumApi.cs b/PiwigoDotnet/Piwigo.Client/IAlbumApi.cs index 4406bcd..19a06dc 100644 --- a/PiwigoDotnet/Piwigo.Client/IAlbumApi.cs +++ b/PiwigoDotnet/Piwigo.Client/IAlbumApi.cs @@ -4,12 +4,14 @@ namespace Piwigo.Client; public interface IAlbumApi { - Task CalculateOrphansAsync(int albumId); + Task CalculateOrphansAsync(int albumId, CancellationToken cancellationToken = default); - Task DeleteAsync(int albumId, string apiToken); + Task DeleteAsync(int albumId, string apiToken, CancellationToken cancellationToken = default); + + Task DeleteRepresentativeAsync(int albumId, CancellationToken cancellationToken = default); Task AddAsync(string name, int? parentId = null, string? comment = null, bool? visible = null, CategoryStatus? status = null, bool? commentable = null, - CategoryPosition? position = null); + CategoryPosition? position = null, CancellationToken cancellationToken = default); - Task> GetAllAsync(); + Task> GetAllAsync(CancellationToken cancellationToken = default); } \ No newline at end of file diff --git a/PiwigoDotnet/Piwigo.Client/IPiwigoContext.cs b/PiwigoDotnet/Piwigo.Client/IPiwigoContext.cs index f548e6f..4e8cb1f 100644 --- a/PiwigoDotnet/Piwigo.Client/IPiwigoContext.cs +++ b/PiwigoDotnet/Piwigo.Client/IPiwigoContext.cs @@ -6,8 +6,8 @@ namespace Piwigo.Client; public interface IPiwigoContext { bool IsLoggedIn { get; } - Task LoginAsync(); - Task LogoutAsync(); - Task PostAsync(ILogger logger, string method, IDictionary formParams) where T : PiwigoResponse; - Task PostAsync(ILogger logger, string method) where T : PiwigoResponse; + Task LoginAsync(CancellationToken cancellationToken = default); + Task LogoutAsync(CancellationToken cancellationToken = default); + Task PostAsync(ILogger logger, string method, IDictionary formParams, CancellationToken cancellationToken = default) where T : PiwigoResponse; + Task PostAsync(ILogger logger, string method, CancellationToken cancellationToken = default) where T : PiwigoResponse; } \ No newline at end of file diff --git a/PiwigoDotnet/Piwigo.Client/ISessionApi.cs b/PiwigoDotnet/Piwigo.Client/ISessionApi.cs index 9b1b4e8..cd3bd8c 100644 --- a/PiwigoDotnet/Piwigo.Client/ISessionApi.cs +++ b/PiwigoDotnet/Piwigo.Client/ISessionApi.cs @@ -4,7 +4,7 @@ namespace Piwigo.Client; public interface ISessionApi { - Task LoginAsync(); - Task LogoutAsync(); - Task GetStatusAsync(); + Task LoginAsync(CancellationToken cancellationToken = default); + Task LogoutAsync(CancellationToken cancellationToken = default); + Task GetStatusAsync(CancellationToken cancellationToken = default); } \ No newline at end of file diff --git a/PiwigoDotnet/Piwigo.Client/PiwigoContext.cs b/PiwigoDotnet/Piwigo.Client/PiwigoContext.cs index de357e5..c8412b0 100644 --- a/PiwigoDotnet/Piwigo.Client/PiwigoContext.cs +++ b/PiwigoDotnet/Piwigo.Client/PiwigoContext.cs @@ -20,7 +20,7 @@ public class PiwigoContext : IPiwigoContext public bool IsLoggedIn { get; private set; } - public async Task LoginAsync() + public async Task LoginAsync(CancellationToken cancellationToken = default) { if (IsLoggedIn) { @@ -42,7 +42,8 @@ public class PiwigoContext : IPiwigoContext _logger.LogInformation("Logging into {PiwigoBaseUri} using username {Username}", _config.BaseUri, userName); - var response = await ConfigureRequest(_logger).PostMultipartAsync(c => c.AddMethod("pwg.session.login").AddString("username", userName).AddString("password", password)); + var response = await ConfigureRequest(_logger) + .PostMultipartAsync(c => c.AddMethod("pwg.session.login").AddString("username", userName).AddString("password", password), cancellationToken); if (response.StatusCode != (int)HttpStatusCode.OK) { @@ -53,7 +54,7 @@ public class PiwigoContext : IPiwigoContext IsLoggedIn = true; } - public async Task LogoutAsync() + public async Task LogoutAsync(CancellationToken cancellationToken = default) { if (!IsLoggedIn) { @@ -62,19 +63,19 @@ public class PiwigoContext : IPiwigoContext } _logger.LogInformation("Logging out from {Uri}", _config.BaseUri); - await PostAsync(_logger, "pwg.session.logout"); + await PostAsync(_logger, "pwg.session.logout", cancellationToken); IsLoggedIn = false; _cookies.Clear(); } - public Task PostAsync(ILogger logger, string method) where T : PiwigoResponse + public Task PostAsync(ILogger logger, string method, CancellationToken cancellationToken = default) where T : PiwigoResponse { - return PostInternalAsync(logger, method, null); + return PostInternalAsync(logger, method, null, cancellationToken); } - public Task PostAsync(ILogger logger, string method, IDictionary formParams) where T : PiwigoResponse + public Task PostAsync(ILogger logger, string method, IDictionary formParams, CancellationToken cancellationToken = default) where T : PiwigoResponse { - return PostInternalAsync(logger, method, formParams); + return PostInternalAsync(logger, method, formParams, cancellationToken); } private static void AddFormParams(CapturedMultipartContent c, IDictionary formParams) @@ -85,9 +86,9 @@ public class PiwigoContext : IPiwigoContext } } - private async Task PostInternalAsync(ILogger logger, string method, IDictionary? formParams) where T : PiwigoResponse + private async Task PostInternalAsync(ILogger logger, string method, IDictionary? formParams, CancellationToken cancellationToken) where T : PiwigoResponse { - await EnsureLoggedInAsync(); + await EnsureLoggedInAsync(cancellationToken); logger.LogInformation("executing {Method} using post", method); @@ -98,7 +99,7 @@ public class PiwigoContext : IPiwigoContext { AddFormParams(c, formParams); } - }); + }, cancellationToken); if (response.StatusCode != (int)HttpStatusCode.OK) { @@ -115,14 +116,14 @@ public class PiwigoContext : IPiwigoContext return typedResponse; } - private async ValueTask EnsureLoggedInAsync() + private async ValueTask EnsureLoggedInAsync(CancellationToken cancellationToken) { if (IsLoggedIn) { return; } - await LoginAsync(); + await LoginAsync(cancellationToken); } private IFlurlRequest ConfigureRequest(ILogger logger) diff --git a/PiwigoDotnet/Piwigo.Client/SessionApi.cs b/PiwigoDotnet/Piwigo.Client/SessionApi.cs index 2a1d409..b996886 100644 --- a/PiwigoDotnet/Piwigo.Client/SessionApi.cs +++ b/PiwigoDotnet/Piwigo.Client/SessionApi.cs @@ -14,19 +14,19 @@ internal class SessionApi : ISessionApi _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } - public Task LogoutAsync() + public Task LogoutAsync(CancellationToken cancellationToken = default) { - return _context.LogoutAsync(); + return _context.LogoutAsync(cancellationToken); } - public Task LoginAsync() + public Task LoginAsync(CancellationToken cancellationToken = default) { - return _context.LoginAsync(); + return _context.LoginAsync(cancellationToken); } - public async Task GetStatusAsync() + public async Task GetStatusAsync(CancellationToken cancellationToken = default) { - var typedResponse = await _context.PostAsync>(_logger, "pwg.session.getStatus"); + var typedResponse = await _context.PostAsync>(_logger, "pwg.session.getStatus", cancellationToken); return typedResponse.Result; } } \ No newline at end of file