adds DeleteRepresentativeAsync to album api

passes cancellation token to all async requests
This commit is contained in:
Philipp Häfelfinger 2022-10-17 23:22:48 +02:00
parent a05e149b31
commit f3ff670ef0
6 changed files with 48 additions and 39 deletions

View File

@ -15,22 +15,28 @@ public class AlbumApi : IAlbumApi
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); _logger = logger ?? throw new ArgumentNullException(nameof(logger));
} }
public async Task<AlbumOrphans> CalculateOrphansAsync(int albumId) public async Task DeleteRepresentativeAsync(int albumId, CancellationToken cancellationToken = default)
{ {
var formParams = new Dictionary<string, string> { { "category_id", albumId.ToString() } }; var formParams = new Dictionary<string, string> { { "category_id", albumId.ToString() } };
var response = await _context.PostAsync<PiwigoResponse<AlbumOrphans[]>>(_logger, "pwg.categories.calculateOrphans", formParams); await _context.PostAsync<PiwigoResponse>(_logger, "pwg.categories.deleteRepresentative", formParams, cancellationToken);
}
public async Task<AlbumOrphans> CalculateOrphansAsync(int albumId, CancellationToken cancellationToken = default)
{
var formParams = new Dictionary<string, string> { { "category_id", albumId.ToString() } };
var response = await _context.PostAsync<PiwigoResponse<AlbumOrphans[]>>(_logger, "pwg.categories.calculateOrphans", formParams, cancellationToken);
// the API seems to only return one result but returns it as a list in json // the API seems to only return one result but returns it as a list in json
return response.Result.First(); return response.Result.First();
} }
public async Task DeleteAsync(int albumId, string apiToken) public async Task DeleteAsync(int albumId, string apiToken, CancellationToken cancellationToken = default)
{ {
var formParams = new Dictionary<string, string> { { "category_id", albumId.ToString() }, { "pwg_token", apiToken }, { "photo_deletion_mode", "delete_orphans" } }; 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); await _context.PostAsync<PiwigoResponse>(_logger, "pwg.categories.delete", formParams, cancellationToken);
} }
public async Task<int> AddAsync(string name, int? parentId = null, string? comment = null, bool? visible = null, CategoryStatus? status = null, bool? commentable = null, 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) CategoryPosition? position = null, CancellationToken cancellationToken = default)
{ {
var statusValue = status switch var statusValue = status switch
{ {
@ -50,9 +56,9 @@ public class AlbumApi : IAlbumApi
var formParams = new Dictionary<string, string> { { "name", name } }; var formParams = new Dictionary<string, string> { { "name", name } };
formParams.AddIfValueNotNull("parent", parentId?.ToString()).AddIfValueNotNull("comment", comment).AddIfValueNotNull("visible", visible?.ToString()) formParams.AddIfValueNotNull("parent", parentId?.ToString()).AddIfValueNotNull("comment", comment).AddIfValueNotNull("visible", visible?.ToString())
.AddIfValueNotNull("status", statusValue).AddIfValueNotNull("commentable", visible?.ToString()).AddIfValueNotNull("position", positionValue); .AddIfValueNotNull("status", statusValue).AddIfValueNotNull("commentable", commentable?.ToString()).AddIfValueNotNull("position", positionValue);
var response = await _context.PostAsync<PiwigoResponse<AlbumAdded>>(_logger, "pwg.categories.add", formParams); var response = await _context.PostAsync<PiwigoResponse<AlbumAdded>>(_logger, "pwg.categories.add", formParams, cancellationToken);
if (!response.Result.Id.HasValue) if (!response.Result.Id.HasValue)
{ {
throw new PiwigoException($"Could not create album {name}: {response.Result.Info}"); throw new PiwigoException($"Could not create album {name}: {response.Result.Info}");
@ -61,10 +67,10 @@ public class AlbumApi : IAlbumApi
return response.Result.Id.Value; return response.Result.Id.Value;
} }
public async Task<IReadOnlyCollection<Album>> GetAllAsync() public async Task<IReadOnlyCollection<Album>> GetAllAsync(CancellationToken cancellationToken = default)
{ {
var formParams = new Dictionary<string, string> { { "recursive", "true" } }; var formParams = new Dictionary<string, string> { { "recursive", "true" } };
var response = await _context.PostAsync<PiwigoResponse<AlbumList>>(_logger, "pwg.categories.getList", formParams); var response = await _context.PostAsync<PiwigoResponse<AlbumList>>(_logger, "pwg.categories.getList", formParams, cancellationToken);
return new ReadOnlyCollection<Album>(response.Result.Albums); return new ReadOnlyCollection<Album>(response.Result.Albums);
} }
} }

View File

@ -4,12 +4,14 @@ namespace Piwigo.Client;
public interface IAlbumApi public interface IAlbumApi
{ {
Task<AlbumOrphans> CalculateOrphansAsync(int albumId); Task<AlbumOrphans> CalculateOrphansAsync(int albumId, CancellationToken cancellationToken = default);
Task DeleteAsync(int albumId, string apiToken); Task DeleteAsync(int albumId, string apiToken, CancellationToken cancellationToken = default);
Task DeleteRepresentativeAsync(int albumId, CancellationToken cancellationToken = default);
Task<int> AddAsync(string name, int? parentId = null, string? comment = null, bool? visible = null, CategoryStatus? status = null, bool? commentable = null, Task<int> AddAsync(string name, int? parentId = null, string? comment = null, bool? visible = null, CategoryStatus? status = null, bool? commentable = null,
CategoryPosition? position = null); CategoryPosition? position = null, CancellationToken cancellationToken = default);
Task<IReadOnlyCollection<Album>> GetAllAsync(); Task<IReadOnlyCollection<Album>> GetAllAsync(CancellationToken cancellationToken = default);
} }

View File

@ -6,8 +6,8 @@ namespace Piwigo.Client;
public interface IPiwigoContext public interface IPiwigoContext
{ {
bool IsLoggedIn { get; } bool IsLoggedIn { get; }
Task LoginAsync(); Task LoginAsync(CancellationToken cancellationToken = default);
Task LogoutAsync(); Task LogoutAsync(CancellationToken cancellationToken = default);
Task<T> PostAsync<T>(ILogger logger, string method, IDictionary<string, string> formParams) where T : PiwigoResponse; Task<T> PostAsync<T>(ILogger logger, string method, IDictionary<string, string> formParams, CancellationToken cancellationToken = default) where T : PiwigoResponse;
Task<T> PostAsync<T>(ILogger logger, string method) where T : PiwigoResponse; Task<T> PostAsync<T>(ILogger logger, string method, CancellationToken cancellationToken = default) where T : PiwigoResponse;
} }

View File

@ -4,7 +4,7 @@ namespace Piwigo.Client;
public interface ISessionApi public interface ISessionApi
{ {
Task LoginAsync(); Task LoginAsync(CancellationToken cancellationToken = default);
Task LogoutAsync(); Task LogoutAsync(CancellationToken cancellationToken = default);
Task<SessionStatus> GetStatusAsync(); Task<SessionStatus> GetStatusAsync(CancellationToken cancellationToken = default);
} }

View File

@ -20,7 +20,7 @@ public class PiwigoContext : IPiwigoContext
public bool IsLoggedIn { get; private set; } public bool IsLoggedIn { get; private set; }
public async Task LoginAsync() public async Task LoginAsync(CancellationToken cancellationToken = default)
{ {
if (IsLoggedIn) if (IsLoggedIn)
{ {
@ -42,7 +42,8 @@ public class PiwigoContext : IPiwigoContext
_logger.LogInformation("Logging into {PiwigoBaseUri} using username {Username}", _config.BaseUri, userName); _logger.LogInformation("Logging into {PiwigoBaseUri} using username {Username}", _config.BaseUri, userName);
var response = await ConfigureRequest(_logger).PostMultipartAsync(c => c.AddMethod("pwg.session.login").AddString("username", userName).AddString("password", password)); var response = await ConfigureRequest(_logger)
.PostMultipartAsync(c => c.AddMethod("pwg.session.login").AddString("username", userName).AddString("password", password), cancellationToken);
if (response.StatusCode != (int)HttpStatusCode.OK) if (response.StatusCode != (int)HttpStatusCode.OK)
{ {
@ -53,7 +54,7 @@ public class PiwigoContext : IPiwigoContext
IsLoggedIn = true; IsLoggedIn = true;
} }
public async Task LogoutAsync() public async Task LogoutAsync(CancellationToken cancellationToken = default)
{ {
if (!IsLoggedIn) if (!IsLoggedIn)
{ {
@ -62,19 +63,19 @@ public class PiwigoContext : IPiwigoContext
} }
_logger.LogInformation("Logging out from {Uri}", _config.BaseUri); _logger.LogInformation("Logging out from {Uri}", _config.BaseUri);
await PostAsync<PiwigoResponse>(_logger, "pwg.session.logout"); await PostAsync<PiwigoResponse>(_logger, "pwg.session.logout", cancellationToken);
IsLoggedIn = false; IsLoggedIn = false;
_cookies.Clear(); _cookies.Clear();
} }
public Task<T> PostAsync<T>(ILogger logger, string method) where T : PiwigoResponse public Task<T> PostAsync<T>(ILogger logger, string method, CancellationToken cancellationToken = default) where T : PiwigoResponse
{ {
return PostInternalAsync<T>(logger, method, null); return PostInternalAsync<T>(logger, method, null, cancellationToken);
} }
public Task<T> PostAsync<T>(ILogger logger, string method, IDictionary<string, string> formParams) where T : PiwigoResponse public Task<T> PostAsync<T>(ILogger logger, string method, IDictionary<string, string> formParams, CancellationToken cancellationToken = default) where T : PiwigoResponse
{ {
return PostInternalAsync<T>(logger, method, formParams); return PostInternalAsync<T>(logger, method, formParams, cancellationToken);
} }
private static void AddFormParams(CapturedMultipartContent c, IDictionary<string, string> formParams) private static void AddFormParams(CapturedMultipartContent c, IDictionary<string, string> formParams)
@ -85,9 +86,9 @@ public class PiwigoContext : IPiwigoContext
} }
} }
private async Task<T> PostInternalAsync<T>(ILogger logger, string method, IDictionary<string, string>? formParams) where T : PiwigoResponse private async Task<T> PostInternalAsync<T>(ILogger logger, string method, IDictionary<string, string>? formParams, CancellationToken cancellationToken) where T : PiwigoResponse
{ {
await EnsureLoggedInAsync(); await EnsureLoggedInAsync(cancellationToken);
logger.LogInformation("executing {Method} using post", method); logger.LogInformation("executing {Method} using post", method);
@ -98,7 +99,7 @@ public class PiwigoContext : IPiwigoContext
{ {
AddFormParams(c, formParams); AddFormParams(c, formParams);
} }
}); }, cancellationToken);
if (response.StatusCode != (int)HttpStatusCode.OK) if (response.StatusCode != (int)HttpStatusCode.OK)
{ {
@ -115,14 +116,14 @@ public class PiwigoContext : IPiwigoContext
return typedResponse; return typedResponse;
} }
private async ValueTask EnsureLoggedInAsync() private async ValueTask EnsureLoggedInAsync(CancellationToken cancellationToken)
{ {
if (IsLoggedIn) if (IsLoggedIn)
{ {
return; return;
} }
await LoginAsync(); await LoginAsync(cancellationToken);
} }
private IFlurlRequest ConfigureRequest(ILogger logger) private IFlurlRequest ConfigureRequest(ILogger logger)

View File

@ -14,19 +14,19 @@ internal class SessionApi : ISessionApi
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); _logger = logger ?? throw new ArgumentNullException(nameof(logger));
} }
public Task LogoutAsync() public Task LogoutAsync(CancellationToken cancellationToken = default)
{ {
return _context.LogoutAsync(); return _context.LogoutAsync(cancellationToken);
} }
public Task LoginAsync() public Task LoginAsync(CancellationToken cancellationToken = default)
{ {
return _context.LoginAsync(); return _context.LoginAsync(cancellationToken);
} }
public async Task<SessionStatus> GetStatusAsync() public async Task<SessionStatus> GetStatusAsync(CancellationToken cancellationToken = default)
{ {
var typedResponse = await _context.PostAsync<PiwigoResponse<SessionStatus>>(_logger, "pwg.session.getStatus"); var typedResponse = await _context.PostAsync<PiwigoResponse<SessionStatus>>(_logger, "pwg.session.getStatus", cancellationToken);
return typedResponse.Result; return typedResponse.Result;
} }
} }