From b2940dc33b90dcc470c4a5a32b677495b3be0fb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20H=C3=A4felfinger?= Date: Sat, 8 Oct 2022 00:08:48 +0200 Subject: [PATCH] adds GetAllCategoriesAsync --- .../Piwigo.Client.Tests/PiwigoClientTests.cs | 59 +++++++++++++++++-- PiwigoDotnet/Piwigo.Client/IPiwigoClient.cs | 1 + PiwigoDotnet/Piwigo.Client/PiwigoCategory.cs | 56 ++++++++++++++++++ .../Piwigo.Client/PiwigoCategoryList.cs | 9 +++ PiwigoDotnet/Piwigo.Client/PiwigoClient.cs | 11 ++++ PiwigoDotnet/Piwigo.Client/PiwigoMethods.cs | 17 ++++-- 6 files changed, 143 insertions(+), 10 deletions(-) create mode 100644 PiwigoDotnet/Piwigo.Client/PiwigoCategory.cs create mode 100644 PiwigoDotnet/Piwigo.Client/PiwigoCategoryList.cs diff --git a/PiwigoDotnet/Piwigo.Client.Tests/PiwigoClientTests.cs b/PiwigoDotnet/Piwigo.Client.Tests/PiwigoClientTests.cs index 50defcc..e4f33d0 100644 --- a/PiwigoDotnet/Piwigo.Client.Tests/PiwigoClientTests.cs +++ b/PiwigoDotnet/Piwigo.Client.Tests/PiwigoClientTests.cs @@ -1,6 +1,7 @@ -using System.Text.Json; +using Flurl.Http.Configuration; using Flurl.Http.Testing; using Microsoft.Extensions.Logging.Abstractions; +using Newtonsoft.Json; namespace Piwigo.Client.Tests; @@ -38,7 +39,7 @@ public class PiwigoClientTests { await LoginAsync(); - var expectedResponse = new PiwigoResponse + var serverResponse = new PiwigoResponse { Status = "OK", Result = new PiwigoStatus @@ -47,8 +48,7 @@ public class PiwigoClientTests Version = "12.0.0" } }; - var jsonResponse = JsonSerializer.Serialize(expectedResponse); - _httpTest?.RespondWith(jsonResponse); + SetJsonResult(serverResponse); var status = await _piwigoClient.GetStatusAsync(); @@ -63,10 +63,61 @@ public class PiwigoClientTests await LoginAsync(); _httpTest?.RespondWith("OK"); + await _piwigoClient.LogoutAsync(); _piwigoClient.IsLoggedIn.Should().BeFalse(); } + [Test] + public async Task GetAllCategories_should_return_all_existing_categories() + { + await LoginAsync(); + + + var serverResponse = new PiwigoResponse + { + Result = new PiwigoCategoryList + { + Categories = new List + { + new() { Id = 1, Name = "UnitTestMain" }, + new() { Id = 3, Name = "UnitTestSub2", IdUpperCat = 1 }, + new() { Id = 2, Name = "UnitTestSub1", IdUpperCat = 1 } + } + } + }; + SetJsonResult(serverResponse); + var response = await _piwigoClient.GetAllCategoriesAsync(); + + response.Should().HaveCount(3); + response.Should().SatisfyRespectively(c => + { + c.Id.Should().Be(1); + c.Name.Should().Be("UnitTestMain"); + }, c => + { + c.Id.Should().Be(3); + c.Name.Should().Be("UnitTestSub2"); + c.IdUpperCat.Should().Be(1); + }, c => + { + c.Id.Should().Be(2); + c.Name.Should().Be("UnitTestSub1"); + c.IdUpperCat.Should().Be(1); + }); + } + + private void SetJsonResult(PiwigoResponse serverResponse) + { + var settings = new JsonSerializerSettings + { + Formatting = Formatting.Indented, + NullValueHandling = NullValueHandling.Ignore + }; + var serializer = new NewtonsoftJsonSerializer(settings); + var jsonResponse = serializer.Serialize(serverResponse); + _httpTest?.RespondWith(jsonResponse); + } private async Task LoginAsync() { diff --git a/PiwigoDotnet/Piwigo.Client/IPiwigoClient.cs b/PiwigoDotnet/Piwigo.Client/IPiwigoClient.cs index 58a0301..4c5e8df 100644 --- a/PiwigoDotnet/Piwigo.Client/IPiwigoClient.cs +++ b/PiwigoDotnet/Piwigo.Client/IPiwigoClient.cs @@ -7,4 +7,5 @@ public interface IPiwigoClient Task LoginAsync(Uri uri, string username, string password); Task LogoutAsync(); Task GetStatusAsync(); + Task> GetAllCategoriesAsync(); } \ No newline at end of file diff --git a/PiwigoDotnet/Piwigo.Client/PiwigoCategory.cs b/PiwigoDotnet/Piwigo.Client/PiwigoCategory.cs new file mode 100644 index 0000000..37c2633 --- /dev/null +++ b/PiwigoDotnet/Piwigo.Client/PiwigoCategory.cs @@ -0,0 +1,56 @@ +using System.Diagnostics.CodeAnalysis; +using Newtonsoft.Json; + +namespace Piwigo.Client; + +[SuppressMessage("ReSharper", "StringLiteralTypo")] +public class PiwigoCategory +{ + [JsonProperty("id")] + public int Id { get; init; } + + [JsonProperty("name")] + public string Name { get; init; } = string.Empty; + + [JsonProperty("comment")] + public string? Comment { get; init; } + + [JsonProperty("permalink")] + public string? Permalink { get; init; } + + [JsonProperty("status")] + public string? Status { get; init; } + + [JsonProperty("uppercats")] + public string? UpperCats { get; init; } + + [JsonProperty("global_rank")] + public string? GlobalRank { get; init; } + + [JsonProperty("id_uppercat")] + public int? IdUpperCat { get; init; } + + [JsonProperty("nb_images")] + public int? NbImages { get; init; } + + [JsonProperty("total_nb_images")] + public int? TotalNbImages { get; init; } + + [JsonProperty("representative_picture_id")] + public string? RepresentativePictureId { get; init; } + + [JsonProperty("date_last")] + public string? DateLast { get; init; } + + [JsonProperty("max_date_last")] + public string? MaxDateLast { get; init; } + + [JsonProperty("nb_categories")] + public int? NbCategories { get; init; } + + [JsonProperty("url")] + public string? Url { get; init; } + + [JsonProperty("tn_url")] + public string? TnUrl { get; init; } +} \ No newline at end of file diff --git a/PiwigoDotnet/Piwigo.Client/PiwigoCategoryList.cs b/PiwigoDotnet/Piwigo.Client/PiwigoCategoryList.cs new file mode 100644 index 0000000..435b0ce --- /dev/null +++ b/PiwigoDotnet/Piwigo.Client/PiwigoCategoryList.cs @@ -0,0 +1,9 @@ +using Newtonsoft.Json; + +namespace Piwigo.Client; + +public class PiwigoCategoryList +{ + [JsonProperty("Categories")] + public IList Categories { get; init; } = null!; +} \ No newline at end of file diff --git a/PiwigoDotnet/Piwigo.Client/PiwigoClient.cs b/PiwigoDotnet/Piwigo.Client/PiwigoClient.cs index e8db4c3..8b646cb 100644 --- a/PiwigoDotnet/Piwigo.Client/PiwigoClient.cs +++ b/PiwigoDotnet/Piwigo.Client/PiwigoClient.cs @@ -1,3 +1,4 @@ +using System.Collections.ObjectModel; using System.Diagnostics; using System.Net; using System.Runtime.CompilerServices; @@ -83,6 +84,16 @@ public class PiwigoClient : IPiwigoClient return typedResponse.Result; } + public async Task> GetAllCategoriesAsync() + { + EnsureLoggedIn(); + + _logger.LogInformation("Getting all existing categories from server"); + var response = await Request.PostMultipartAsync(c => c.PiwigoGetAllCategories()); + var typedResponse = await response.GetJsonAsync>(); + return new ReadOnlyCollection(typedResponse.Result.Categories); + } + private void EnsureLoggedIn([CallerMemberName] string? callerName = null) { if (!IsLoggedIn) diff --git a/PiwigoDotnet/Piwigo.Client/PiwigoMethods.cs b/PiwigoDotnet/Piwigo.Client/PiwigoMethods.cs index 49b9b31..2aaed46 100644 --- a/PiwigoDotnet/Piwigo.Client/PiwigoMethods.cs +++ b/PiwigoDotnet/Piwigo.Client/PiwigoMethods.cs @@ -4,20 +4,25 @@ namespace Piwigo.Client; internal static class PiwigoMethods { - public static CapturedMultipartContent PiwigoLogin(this CapturedMultipartContent part, string username, + public static void PiwigoLogin(this CapturedMultipartContent part, string username, string password) { - return part.AddMethod("pwg.session.login").AddString("username", username).AddString("password", password); + part.AddMethod("pwg.session.login").AddString("username", username).AddString("password", password); } - public static CapturedMultipartContent PiwigoLogout(this CapturedMultipartContent part) + public static void PiwigoLogout(this CapturedMultipartContent part) { - return part.AddMethod("pwg.session.logout"); + part.AddMethod("pwg.session.logout"); } - public static CapturedMultipartContent PiwigoGetStatus(this CapturedMultipartContent part) + public static void PiwigoGetStatus(this CapturedMultipartContent part) { - return part.AddMethod("pwg.session.getStatus"); + part.AddMethod("pwg.session.getStatus"); + } + + public static void PiwigoGetAllCategories(this CapturedMultipartContent part, bool recursive = true) + { + part.AddMethod("pwg.categories.getList").AddString("recursive", recursive.ToString()); } private static CapturedMultipartContent AddMethod(this CapturedMultipartContent part, string piwigoMethod)