adds delete album and make context response type aware to handle response status within the call context

This commit is contained in:
Philipp Häfelfinger 2022-10-17 00:16:30 +02:00
parent 09a7a40dff
commit 32a071b6c7
10 changed files with 50 additions and 19 deletions

View File

@ -14,14 +14,23 @@ public class AlbumApiTests : ApiTestsBase
_albumApi = new AlbumApi(Context, new NullLogger<AlbumApi>());
}
[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<AlbumAdded>
{
Status = "ok",
Result = new AlbumAdded
{
Id = 1, Info = "Album added"
@ -54,6 +64,7 @@ public class AlbumApiTests : ApiTestsBase
{
var serverResponse = new PiwigoResponse<AlbumList>
{
Status = "ok",
Result = new AlbumList
{
Albums = new List<Album>

View File

@ -24,10 +24,10 @@ public class SessionApiTests : ApiTestsBase
public async Task GetStatus_should_return_config()
{
await LoginAsync();
var serverResponse = new PiwigoResponse<PiwigoStatus>
var serverResponse = new PiwigoResponse<SessionStatus>
{
Status = "OK",
Result = new PiwigoStatus
Result = new SessionStatus
{
Username = "admin",
Version = "12.0.0"

View File

@ -23,6 +23,12 @@ public class AlbumApi : IAlbumApi
return response.Result.First();
}
public async Task DeleteAsync(int albumId, string apiToken)
{
var formParams = new Dictionary<string, string> { { "category_id", albumId.ToString() }, { "pwg_token", apiToken }, { "photo_deletion_mode", "delete_orphans" } };
var response = await _context.PostAsync<PiwigoResponse>(_logger, "pwg.categories.delete", formParams);
}
public async Task<int> AddAsync(string name, int? parentId = null, string? comment = null, bool? visible = null, CategoryStatus? status = null, bool? commentable = null,
CategoryPosition? position = null)
{

View File

@ -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<T> : PiwigoResponse
public class PiwigoResponse<T> : PiwigoResponse
{
[JsonProperty("result")]
public T Result { get; init; } = default!;

View File

@ -2,7 +2,7 @@ using Newtonsoft.Json;
namespace Piwigo.Client.Contract;
public class PiwigoStatus
public class SessionStatus
{
[JsonProperty("username")]
public string? Username { get; init; }

View File

@ -6,6 +6,8 @@ public interface IAlbumApi
{
Task<AlbumOrphans> CalculateOrphansAsync(int albumId);
Task DeleteAsync(int albumId, string apiToken);
Task<int> AddAsync(string name, int? parentId = null, string? comment = null, bool? visible = null, CategoryStatus? status = null, bool? commentable = null,
CategoryPosition? position = null);

View File

@ -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<T> PostAsync<T>(ILogger logger, string method, IDictionary<string, string> formParams);
Task<T> PostAsync<T>(ILogger logger, string method);
Task<T> PostAsync<T>(ILogger logger, string method, IDictionary<string, string> formParams) where T : PiwigoResponse;
Task<T> PostAsync<T>(ILogger logger, string method) where T : PiwigoResponse;
}

View File

@ -6,5 +6,5 @@ public interface ISessionApi
{
Task LoginAsync();
Task LogoutAsync();
Task<PiwigoStatus> GetStatusAsync();
Task<SessionStatus> GetStatusAsync();
}

View File

@ -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<PiwigoResponse>(_logger, "pwg.session.logout");
IsLoggedIn = false;
_cookies.Clear();
}
public Task<T> PostAsync<T>(ILogger logger, string method)
public Task<T> PostAsync<T>(ILogger logger, string method) where T : PiwigoResponse
{
return PostInternalAsync<T>(logger, method, null);
}
public Task<T> PostAsync<T>(ILogger logger, string method, IDictionary<string, string> formParams)
public Task<T> PostAsync<T>(ILogger logger, string method, IDictionary<string, string> formParams) where T : PiwigoResponse
{
return PostInternalAsync<T>(logger, method, formParams);
}
@ -83,10 +84,12 @@ public class PiwigoContext : IPiwigoContext
}
}
private async Task<T> PostInternalAsync<T>(ILogger logger, string method, IDictionary<string, string>? formParams)
private async Task<T> PostInternalAsync<T>(ILogger logger, string method, IDictionary<string, string>? 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<T>();
if (!typedResponse.IsOk)
{
throw new PiwigoException($"failed to call {method} on {_config.BaseUri}: {typedResponse.Status} - Error: {typedResponse.Error} - Message:{typedResponse.Message}");
}
return typedResponse;
}

View File

@ -24,10 +24,9 @@ internal class SessionApi : ISessionApi
return _context.LoginAsync();
}
public async Task<PiwigoStatus> GetStatusAsync()
public async Task<SessionStatus> GetStatusAsync()
{
_logger.LogInformation("Getting status");
var typedResponse = await _context.PostAsync<PiwigoResponse<PiwigoStatus>>(_logger, "pwg.session.getStatus");
var typedResponse = await _context.PostAsync<PiwigoResponse<SessionStatus>>(_logger, "pwg.session.getStatus");
return typedResponse.Result;
}
}