From 32a071b6c7be86e3b8dbd6a82f1c74ef9e6a2121 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20H=C3=A4felfinger?= Date: Mon, 17 Oct 2022 00:16:30 +0200 Subject: [PATCH] adds delete album and make context response type aware to handle response status within the call context --- .../Piwigo.Client.Tests/AlbumApiTests.cs | 19 +++++++++++++++---- .../Piwigo.Client.Tests/SessionApiTests.cs | 4 ++-- PiwigoDotnet/Piwigo.Client/AlbumApi.cs | 6 ++++++ .../Piwigo.Client/Contract/PiwigoResponse.cs | 7 +++++-- .../{PiwigoStatus.cs => SessionStatus.cs} | 2 +- PiwigoDotnet/Piwigo.Client/IAlbumApi.cs | 2 ++ PiwigoDotnet/Piwigo.Client/IPiwigoContext.cs | 5 +++-- PiwigoDotnet/Piwigo.Client/ISessionApi.cs | 2 +- PiwigoDotnet/Piwigo.Client/PiwigoContext.cs | 17 +++++++++++++---- PiwigoDotnet/Piwigo.Client/SessionApi.cs | 5 ++--- 10 files changed, 50 insertions(+), 19 deletions(-) rename PiwigoDotnet/Piwigo.Client/Contract/{PiwigoStatus.cs => SessionStatus.cs} (97%) diff --git a/PiwigoDotnet/Piwigo.Client.Tests/AlbumApiTests.cs b/PiwigoDotnet/Piwigo.Client.Tests/AlbumApiTests.cs index b5d8c16..f5e5de5 100644 --- a/PiwigoDotnet/Piwigo.Client.Tests/AlbumApiTests.cs +++ b/PiwigoDotnet/Piwigo.Client.Tests/AlbumApiTests.cs @@ -14,14 +14,23 @@ public class AlbumApiTests : ApiTestsBase _albumApi = new AlbumApi(Context, new NullLogger()); } + [Test] + public async Task Delete_should_remove_album() + { + SetJsonResult(@"{stat: ""ok"", result: null }"); + + await _albumApi.DeleteAsync(1, "apiToken"); + } + [Test] public async Task CalculateOrphansAsync_should_return_correct_values() { SetJsonResult(@"{ - ""result"": [ { - ""nb_images_associated_outside"": 1, - ""nb_images_becoming_orphan"": 2, - ""nb_images_recursive"": 3 + stat: ""ok"", + result: [ { + nb_images_associated_outside: 1, + nb_images_becoming_orphan: 2, + nb_images_recursive: 3 } ] }"); var response = await _albumApi.CalculateOrphansAsync(1); @@ -37,6 +46,7 @@ public class AlbumApiTests : ApiTestsBase { var serverResponse = new PiwigoResponse { + Status = "ok", Result = new AlbumAdded { Id = 1, Info = "Album added" @@ -54,6 +64,7 @@ public class AlbumApiTests : ApiTestsBase { var serverResponse = new PiwigoResponse { + Status = "ok", Result = new AlbumList { Albums = new List diff --git a/PiwigoDotnet/Piwigo.Client.Tests/SessionApiTests.cs b/PiwigoDotnet/Piwigo.Client.Tests/SessionApiTests.cs index 4b9ddd2..af34ecf 100644 --- a/PiwigoDotnet/Piwigo.Client.Tests/SessionApiTests.cs +++ b/PiwigoDotnet/Piwigo.Client.Tests/SessionApiTests.cs @@ -24,10 +24,10 @@ public class SessionApiTests : ApiTestsBase public async Task GetStatus_should_return_config() { await LoginAsync(); - var serverResponse = new PiwigoResponse + var serverResponse = new PiwigoResponse { Status = "OK", - Result = new PiwigoStatus + Result = new SessionStatus { Username = "admin", Version = "12.0.0" diff --git a/PiwigoDotnet/Piwigo.Client/AlbumApi.cs b/PiwigoDotnet/Piwigo.Client/AlbumApi.cs index 25fba23..2db1c08 100644 --- a/PiwigoDotnet/Piwigo.Client/AlbumApi.cs +++ b/PiwigoDotnet/Piwigo.Client/AlbumApi.cs @@ -23,6 +23,12 @@ public class AlbumApi : IAlbumApi return response.Result.First(); } + public async Task DeleteAsync(int albumId, string apiToken) + { + 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); + } + public async Task AddAsync(string name, int? parentId = null, string? comment = null, bool? visible = null, CategoryStatus? status = null, bool? commentable = null, CategoryPosition? position = null) { diff --git a/PiwigoDotnet/Piwigo.Client/Contract/PiwigoResponse.cs b/PiwigoDotnet/Piwigo.Client/Contract/PiwigoResponse.cs index 9605144..e5201b8 100644 --- a/PiwigoDotnet/Piwigo.Client/Contract/PiwigoResponse.cs +++ b/PiwigoDotnet/Piwigo.Client/Contract/PiwigoResponse.cs @@ -2,8 +2,11 @@ using Newtonsoft.Json; namespace Piwigo.Client.Contract; -internal class PiwigoResponse +public class PiwigoResponse { + [JsonIgnore] + public bool IsOk => Status is not null && Status.ToLower().Equals("ok"); + [JsonProperty("stat")] public string? Status { get; init; } @@ -14,7 +17,7 @@ internal class PiwigoResponse public string? Message { get; init; } } -internal class PiwigoResponse : PiwigoResponse +public class PiwigoResponse : PiwigoResponse { [JsonProperty("result")] public T Result { get; init; } = default!; diff --git a/PiwigoDotnet/Piwigo.Client/Contract/PiwigoStatus.cs b/PiwigoDotnet/Piwigo.Client/Contract/SessionStatus.cs similarity index 97% rename from PiwigoDotnet/Piwigo.Client/Contract/PiwigoStatus.cs rename to PiwigoDotnet/Piwigo.Client/Contract/SessionStatus.cs index 081888b..0ccdaef 100644 --- a/PiwigoDotnet/Piwigo.Client/Contract/PiwigoStatus.cs +++ b/PiwigoDotnet/Piwigo.Client/Contract/SessionStatus.cs @@ -2,7 +2,7 @@ using Newtonsoft.Json; namespace Piwigo.Client.Contract; -public class PiwigoStatus +public class SessionStatus { [JsonProperty("username")] public string? Username { get; init; } diff --git a/PiwigoDotnet/Piwigo.Client/IAlbumApi.cs b/PiwigoDotnet/Piwigo.Client/IAlbumApi.cs index 554d965..4406bcd 100644 --- a/PiwigoDotnet/Piwigo.Client/IAlbumApi.cs +++ b/PiwigoDotnet/Piwigo.Client/IAlbumApi.cs @@ -6,6 +6,8 @@ public interface IAlbumApi { Task CalculateOrphansAsync(int albumId); + Task DeleteAsync(int albumId, string apiToken); + Task AddAsync(string name, int? parentId = null, string? comment = null, bool? visible = null, CategoryStatus? status = null, bool? commentable = null, CategoryPosition? position = null); diff --git a/PiwigoDotnet/Piwigo.Client/IPiwigoContext.cs b/PiwigoDotnet/Piwigo.Client/IPiwigoContext.cs index 3ea7478..f548e6f 100644 --- a/PiwigoDotnet/Piwigo.Client/IPiwigoContext.cs +++ b/PiwigoDotnet/Piwigo.Client/IPiwigoContext.cs @@ -1,4 +1,5 @@ using Microsoft.Extensions.Logging; +using Piwigo.Client.Contract; namespace Piwigo.Client; @@ -7,6 +8,6 @@ public interface IPiwigoContext bool IsLoggedIn { get; } Task LoginAsync(); Task LogoutAsync(); - Task PostAsync(ILogger logger, string method, IDictionary formParams); - Task PostAsync(ILogger logger, string method); + Task PostAsync(ILogger logger, string method, IDictionary formParams) where T : PiwigoResponse; + Task PostAsync(ILogger logger, string method) where T : PiwigoResponse; } \ No newline at end of file diff --git a/PiwigoDotnet/Piwigo.Client/ISessionApi.cs b/PiwigoDotnet/Piwigo.Client/ISessionApi.cs index 1d5eec5..9b1b4e8 100644 --- a/PiwigoDotnet/Piwigo.Client/ISessionApi.cs +++ b/PiwigoDotnet/Piwigo.Client/ISessionApi.cs @@ -6,5 +6,5 @@ public interface ISessionApi { Task LoginAsync(); Task LogoutAsync(); - Task GetStatusAsync(); + Task GetStatusAsync(); } \ No newline at end of file diff --git a/PiwigoDotnet/Piwigo.Client/PiwigoContext.cs b/PiwigoDotnet/Piwigo.Client/PiwigoContext.cs index f5bb651..19c62e1 100644 --- a/PiwigoDotnet/Piwigo.Client/PiwigoContext.cs +++ b/PiwigoDotnet/Piwigo.Client/PiwigoContext.cs @@ -2,6 +2,7 @@ using System.Net; using Flurl.Http; using Flurl.Http.Content; using Microsoft.Extensions.Logging; +using Piwigo.Client.Contract; namespace Piwigo.Client; @@ -60,17 +61,17 @@ public class PiwigoContext : IPiwigoContext } _logger.LogInformation("Logging out from {Uri}", _config.BaseUri); - await ConfigureRequest(_logger).PostMultipartAsync(c => c.AddMethod("pwg.session.logout")); + await PostAsync(_logger, "pwg.session.logout"); IsLoggedIn = false; _cookies.Clear(); } - public Task PostAsync(ILogger logger, string method) + public Task PostAsync(ILogger logger, string method) where T : PiwigoResponse { return PostInternalAsync(logger, method, null); } - public Task PostAsync(ILogger logger, string method, IDictionary formParams) + public Task PostAsync(ILogger logger, string method, IDictionary formParams) where T : PiwigoResponse { return PostInternalAsync(logger, method, formParams); } @@ -83,10 +84,12 @@ public class PiwigoContext : IPiwigoContext } } - private async Task PostInternalAsync(ILogger logger, string method, IDictionary? formParams) + private async Task PostInternalAsync(ILogger logger, string method, IDictionary? formParams) where T : PiwigoResponse { await EnsureLoggedInAsync(); + logger.LogInformation("executing {Method} using post", method); + var response = await ConfigureRequest(logger).PostMultipartAsync(c => { c.AddMethod(method); @@ -102,6 +105,12 @@ public class PiwigoContext : IPiwigoContext } var typedResponse = await response.GetJsonAsync(); + + if (!typedResponse.IsOk) + { + throw new PiwigoException($"failed to call {method} on {_config.BaseUri}: {typedResponse.Status} - Error: {typedResponse.Error} - Message:{typedResponse.Message}"); + } + return typedResponse; } diff --git a/PiwigoDotnet/Piwigo.Client/SessionApi.cs b/PiwigoDotnet/Piwigo.Client/SessionApi.cs index 1da2683..2a1d409 100644 --- a/PiwigoDotnet/Piwigo.Client/SessionApi.cs +++ b/PiwigoDotnet/Piwigo.Client/SessionApi.cs @@ -24,10 +24,9 @@ internal class SessionApi : ISessionApi return _context.LoginAsync(); } - public async Task GetStatusAsync() + public async Task GetStatusAsync() { - _logger.LogInformation("Getting status"); - var typedResponse = await _context.PostAsync>(_logger, "pwg.session.getStatus"); + var typedResponse = await _context.PostAsync>(_logger, "pwg.session.getStatus"); return typedResponse.Result; } } \ No newline at end of file