adds GetAllCategoriesAsync

This commit is contained in:
Philipp Häfelfinger 2022-10-08 00:08:48 +02:00
parent 5cdd637faf
commit b2940dc33b
6 changed files with 143 additions and 10 deletions

View File

@ -1,6 +1,7 @@
using System.Text.Json; using Flurl.Http.Configuration;
using Flurl.Http.Testing; using Flurl.Http.Testing;
using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Logging.Abstractions;
using Newtonsoft.Json;
namespace Piwigo.Client.Tests; namespace Piwigo.Client.Tests;
@ -38,7 +39,7 @@ public class PiwigoClientTests
{ {
await LoginAsync(); await LoginAsync();
var expectedResponse = new PiwigoResponse<PiwigoStatus> var serverResponse = new PiwigoResponse<PiwigoStatus>
{ {
Status = "OK", Status = "OK",
Result = new PiwigoStatus Result = new PiwigoStatus
@ -47,8 +48,7 @@ public class PiwigoClientTests
Version = "12.0.0" Version = "12.0.0"
} }
}; };
var jsonResponse = JsonSerializer.Serialize(expectedResponse); SetJsonResult(serverResponse);
_httpTest?.RespondWith(jsonResponse);
var status = await _piwigoClient.GetStatusAsync(); var status = await _piwigoClient.GetStatusAsync();
@ -63,10 +63,61 @@ public class PiwigoClientTests
await LoginAsync(); await LoginAsync();
_httpTest?.RespondWith("OK"); _httpTest?.RespondWith("OK");
await _piwigoClient.LogoutAsync(); await _piwigoClient.LogoutAsync();
_piwigoClient.IsLoggedIn.Should().BeFalse(); _piwigoClient.IsLoggedIn.Should().BeFalse();
} }
[Test]
public async Task GetAllCategories_should_return_all_existing_categories()
{
await LoginAsync();
var serverResponse = new PiwigoResponse<PiwigoCategoryList>
{
Result = new PiwigoCategoryList
{
Categories = new List<PiwigoCategory>
{
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<T>(PiwigoResponse<T> 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() private async Task LoginAsync()
{ {

View File

@ -7,4 +7,5 @@ public interface IPiwigoClient
Task LoginAsync(Uri uri, string username, string password); Task LoginAsync(Uri uri, string username, string password);
Task LogoutAsync(); Task LogoutAsync();
Task<PiwigoStatus> GetStatusAsync(); Task<PiwigoStatus> GetStatusAsync();
Task<IReadOnlyCollection<PiwigoCategory>> GetAllCategoriesAsync();
} }

View File

@ -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; }
}

View File

@ -0,0 +1,9 @@
using Newtonsoft.Json;
namespace Piwigo.Client;
public class PiwigoCategoryList
{
[JsonProperty("Categories")]
public IList<PiwigoCategory> Categories { get; init; } = null!;
}

View File

@ -1,3 +1,4 @@
using System.Collections.ObjectModel;
using System.Diagnostics; using System.Diagnostics;
using System.Net; using System.Net;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
@ -83,6 +84,16 @@ public class PiwigoClient : IPiwigoClient
return typedResponse.Result; return typedResponse.Result;
} }
public async Task<IReadOnlyCollection<PiwigoCategory>> GetAllCategoriesAsync()
{
EnsureLoggedIn();
_logger.LogInformation("Getting all existing categories from server");
var response = await Request.PostMultipartAsync(c => c.PiwigoGetAllCategories());
var typedResponse = await response.GetJsonAsync<PiwigoResponse<PiwigoCategoryList>>();
return new ReadOnlyCollection<PiwigoCategory>(typedResponse.Result.Categories);
}
private void EnsureLoggedIn([CallerMemberName] string? callerName = null) private void EnsureLoggedIn([CallerMemberName] string? callerName = null)
{ {
if (!IsLoggedIn) if (!IsLoggedIn)

View File

@ -4,20 +4,25 @@ namespace Piwigo.Client;
internal static class PiwigoMethods internal static class PiwigoMethods
{ {
public static CapturedMultipartContent PiwigoLogin(this CapturedMultipartContent part, string username, public static void PiwigoLogin(this CapturedMultipartContent part, string username,
string password) 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) private static CapturedMultipartContent AddMethod(this CapturedMultipartContent part, string piwigoMethod)