From 09a7a40dff3869715f51e90dffb5e40148eabad2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20H=C3=A4felfinger?= Date: Sun, 16 Oct 2022 23:53:17 +0200 Subject: [PATCH] starts using json strings from api examples to check mapping adds CalculateOrphansAsync to AlbumApi removes some not needed log lines removes tests explicit login calls --- .../Piwigo.Client.Tests/AlbumApiTests.cs | 22 +++++++++++++++---- .../Piwigo.Client.Tests/ApiTestsBase.cs | 13 ++++++----- .../Piwigo.Client.Tests/SessionApiTests.cs | 11 ++++++++-- PiwigoDotnet/Piwigo.Client/AlbumApi.cs | 9 +++++++- .../Piwigo.Client/Contract/AlbumOrphans.cs | 15 +++++++++++++ PiwigoDotnet/Piwigo.Client/IAlbumApi.cs | 2 ++ PiwigoDotnet/Piwigo.Client/PiwigoContext.cs | 10 +++++---- 7 files changed, 66 insertions(+), 16 deletions(-) create mode 100644 PiwigoDotnet/Piwigo.Client/Contract/AlbumOrphans.cs diff --git a/PiwigoDotnet/Piwigo.Client.Tests/AlbumApiTests.cs b/PiwigoDotnet/Piwigo.Client.Tests/AlbumApiTests.cs index 2d0cce9..b5d8c16 100644 --- a/PiwigoDotnet/Piwigo.Client.Tests/AlbumApiTests.cs +++ b/PiwigoDotnet/Piwigo.Client.Tests/AlbumApiTests.cs @@ -14,11 +14,27 @@ public class AlbumApiTests : ApiTestsBase _albumApi = new AlbumApi(Context, new NullLogger()); } + [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 + } ] +}"); + var response = await _albumApi.CalculateOrphansAsync(1); + + response.Should().NotBeNull(); + response.AssociatedOutsideCount.Should().Be(1); + response.BecomingOrphanCount.Should().Be(2); + response.RecursiveImageCount.Should().Be(3); + } + [Test] public async Task Add_should_create_album_and_return_id() { - await LoginAsync(); - var serverResponse = new PiwigoResponse { Result = new AlbumAdded @@ -36,8 +52,6 @@ public class AlbumApiTests : ApiTestsBase [Test] public async Task GetAll_should_return_all_existing_albums() { - await LoginAsync(); - var serverResponse = new PiwigoResponse { Result = new AlbumList diff --git a/PiwigoDotnet/Piwigo.Client.Tests/ApiTestsBase.cs b/PiwigoDotnet/Piwigo.Client.Tests/ApiTestsBase.cs index 8a85bb4..c4e5194 100644 --- a/PiwigoDotnet/Piwigo.Client.Tests/ApiTestsBase.cs +++ b/PiwigoDotnet/Piwigo.Client.Tests/ApiTestsBase.cs @@ -37,12 +37,15 @@ public class ApiTestsBase HttpTest?.Dispose(); } - protected async Task LoginAsync() + + internal void SetOkResult() { - HttpTest?.RespondWith("{}", 200, cookies: new { pwg_id = "pwg_id" }); - var sessionApi = new SessionApi(Context, new NullLogger()); - await sessionApi.LoginAsync(); - Context.IsLoggedIn.Should().BeTrue(); + SetJsonResult(@"{stat: ""ok""}"); + } + + internal void SetJsonResult(string json) + { + HttpTest?.RespondWith(json); } internal void SetJsonResult(PiwigoResponse serverResponse) diff --git a/PiwigoDotnet/Piwigo.Client.Tests/SessionApiTests.cs b/PiwigoDotnet/Piwigo.Client.Tests/SessionApiTests.cs index 34f21ef..4b9ddd2 100644 --- a/PiwigoDotnet/Piwigo.Client.Tests/SessionApiTests.cs +++ b/PiwigoDotnet/Piwigo.Client.Tests/SessionApiTests.cs @@ -39,7 +39,6 @@ public class SessionApiTests : ApiTestsBase status.Should().NotBeNull(); status.Username.Should().Be("admin"); - status.Version.Should().NotBeEmpty(); } [Test] @@ -47,9 +46,17 @@ public class SessionApiTests : ApiTestsBase { await LoginAsync(); - HttpTest?.RespondWith("OK"); + SetOkResult(); await _sessionApi.LogoutAsync(); Context.IsLoggedIn.Should().BeFalse(); } + + private async Task LoginAsync() + { + HttpTest?.RespondWith("{}", 200, cookies: new { pwg_id = "pwg_id" }); + var sessionApi = new SessionApi(Context, new NullLogger()); + await sessionApi.LoginAsync(); + Context.IsLoggedIn.Should().BeTrue(); + } } \ No newline at end of file diff --git a/PiwigoDotnet/Piwigo.Client/AlbumApi.cs b/PiwigoDotnet/Piwigo.Client/AlbumApi.cs index c0b2f16..25fba23 100644 --- a/PiwigoDotnet/Piwigo.Client/AlbumApi.cs +++ b/PiwigoDotnet/Piwigo.Client/AlbumApi.cs @@ -15,6 +15,14 @@ public class AlbumApi : IAlbumApi _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } + public async Task CalculateOrphansAsync(int albumId) + { + var formParams = new Dictionary { { "category_id", albumId.ToString() } }; + var response = await _context.PostAsync>(_logger, "pwg.categories.calculateOrphans", formParams); + // the API seems to only return one result but returns it as a list in json + return response.Result.First(); + } + public async Task AddAsync(string name, int? parentId = null, string? comment = null, bool? visible = null, CategoryStatus? status = null, bool? commentable = null, CategoryPosition? position = null) { @@ -49,7 +57,6 @@ public class AlbumApi : IAlbumApi public async Task> GetAllAsync() { - _logger.LogInformation("Getting all existing categories from server"); var formParams = new Dictionary { { "recursive", "true" } }; var response = await _context.PostAsync>(_logger, "pwg.categories.getList", formParams); return new ReadOnlyCollection(response.Result.Albums); diff --git a/PiwigoDotnet/Piwigo.Client/Contract/AlbumOrphans.cs b/PiwigoDotnet/Piwigo.Client/Contract/AlbumOrphans.cs new file mode 100644 index 0000000..192ddc4 --- /dev/null +++ b/PiwigoDotnet/Piwigo.Client/Contract/AlbumOrphans.cs @@ -0,0 +1,15 @@ +using Newtonsoft.Json; + +namespace Piwigo.Client.Contract; + +public class AlbumOrphans +{ + [JsonProperty("nb_images_associated_outside")] + public int? AssociatedOutsideCount { get; init; } + + [JsonProperty("nb_images_becoming_orphan")] + public int? BecomingOrphanCount { get; init; } + + [JsonProperty("nb_images_recursive")] + public int? RecursiveImageCount { get; init; } +} \ No newline at end of file diff --git a/PiwigoDotnet/Piwigo.Client/IAlbumApi.cs b/PiwigoDotnet/Piwigo.Client/IAlbumApi.cs index 6d44a56..554d965 100644 --- a/PiwigoDotnet/Piwigo.Client/IAlbumApi.cs +++ b/PiwigoDotnet/Piwigo.Client/IAlbumApi.cs @@ -4,6 +4,8 @@ namespace Piwigo.Client; public interface IAlbumApi { + Task CalculateOrphansAsync(int albumId); + 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/PiwigoContext.cs b/PiwigoDotnet/Piwigo.Client/PiwigoContext.cs index 84d057d..f5bb651 100644 --- a/PiwigoDotnet/Piwigo.Client/PiwigoContext.cs +++ b/PiwigoDotnet/Piwigo.Client/PiwigoContext.cs @@ -44,12 +44,10 @@ public class PiwigoContext : IPiwigoContext if (response.StatusCode != (int)HttpStatusCode.OK) { - _logger.LogError("Failed to log in {StatusCode}", response.StatusCode); throw new PiwigoException($"Could not log in to {_config.BaseUri} using username {userName}"); } _logger.LogInformation("Logging in succeeded"); - _logger.LogInformation("logged in"); IsLoggedIn = true; } @@ -63,7 +61,6 @@ public class PiwigoContext : IPiwigoContext _logger.LogInformation("Logging out from {Uri}", _config.BaseUri); await ConfigureRequest(_logger).PostMultipartAsync(c => c.AddMethod("pwg.session.logout")); - _logger.LogInformation("logged out, clearing cookies"); IsLoggedIn = false; _cookies.Clear(); } @@ -99,6 +96,11 @@ public class PiwigoContext : IPiwigoContext } }); + if (response.StatusCode != (int)HttpStatusCode.OK) + { + throw new PiwigoException($"failed to call {method} on {_config.BaseUri}: {response.StatusCode}"); + } + var typedResponse = await response.GetJsonAsync(); return typedResponse; } @@ -126,6 +128,6 @@ public class PiwigoContext : IPiwigoContext private static async Task LogResponse(FlurlCall call, ILogger logger) { var responseString = await call.Response.GetStringAsync(); - logger.LogDebug("PiwigoResponse: {Response}", responseString); + logger.LogDebug("Response: {Response}", responseString); } } \ No newline at end of file