adds first tag api calls

This commit is contained in:
Philipp Häfelfinger 2022-10-27 23:48:07 +02:00
parent 737f3ff198
commit e29c1c7c03
13 changed files with 255 additions and 0 deletions

View File

@ -41,6 +41,21 @@
<None Update="ImageApiTests.GetInfo_should_pass_request_and_return_data.verified.txt">
<DependentUpon>ImageApiTests.cs</DependentUpon>
</None>
<None Update="TagApi.GetAdminList.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="TagApiTests.AddAsync_should_add_tag_and_return_correct_result.verified.txt">
<DependentUpon>TagApiTests.cs</DependentUpon>
</None>
<None Update="TagApi.GetList.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="TagApiTests.GetAdminListAsync_should_return_all_tags.verified.txt">
<DependentUpon>TagApiTests.cs</DependentUpon>
</None>
<None Update="TagApiTests.GetListAsync_should_return_all_tags.verified.txt">
<DependentUpon>TagApiTests.cs</DependentUpon>
</None>
</ItemGroup>
</Project>

View File

@ -0,0 +1,19 @@
{
"stat": "ok",
"result": {
"tags": [
{
"id": "1",
"name": "tag1",
"url_name": "tag1",
"lastmodified": "2022-10-27 21:17:59"
},
{
"id": "2",
"name": "tag2",
"url_name": "tag2",
"lastmodified": "2022-10-27 21:23:34"
}
]
}
}

View File

@ -0,0 +1,15 @@
{
"stat": "ok",
"result": {
"tags": [
{
"id": 1,
"name": "tag1",
"url_name": "tag1",
"lastmodified": "2022-10-27 21:17:59",
"counter": 1,
"url": "http://localhost:8080/index.php?/tags/1-tag1"
}
]
}
}

View File

@ -0,0 +1,6 @@
{
info: Keyword "test3" has been added,
id: 3,
name: test3,
url_name: test3
}

View File

@ -0,0 +1,14 @@
[
{
id: 1,
name: tag1,
url_name: tag1,
lastmodified: DateTime_1
},
{
id: 2,
name: tag2,
url_name: tag2,
lastmodified: DateTime_2
}
]

View File

@ -0,0 +1,10 @@
[
{
counter: 1,
url: http://localhost:8080/index.php?/tags/1-tag1,
id: 1,
name: tag1,
url_name: tag1,
lastmodified: DateTime_1
}
]

View File

@ -0,0 +1,54 @@
using Microsoft.Extensions.Logging.Abstractions;
using Piwigo.Client.Tags;
namespace Piwigo.Client.Tests;
[TestFixture]
public class TagApiTests : ApiTestsBase
{
private ITagApi _tagApi = null!;
private const string ApiToken = "37ff55f201cf54eadf11f734a74f1d0e";
protected override void OnSetUp()
{
base.OnSetUp();
_tagApi = new TagApi(Context, new NullLogger<TagApi>());
}
[Test]
public async Task AddAsync_should_add_tag_and_return_correct_result()
{
SetJsonResult(@"{""stat"":""ok"",""result"":{""info"":""Keyword \""test3\"" has been added"",""id"":3,""name"":""test3"",""url_name"":""test3""}}");
var response = await _tagApi.AddAsync("test3");
CorrectMethodShouldGetCalled("pwg.tags.add");
CorrectParamShouldGetSent("name", "test3");
await Verify(response);
}
[Test]
public async Task GetAdminListAsync_should_return_all_tags()
{
await SetJsonResultFromFileAsync("TagApi.GetAdminList.json");
var response = await _tagApi.GetAdminListAsync();
CorrectMethodShouldGetCalled("pwg.tags.getAdminList");
await Verify(response);
}
[Test]
public async Task GetListAsync_should_return_all_tags()
{
await SetJsonResultFromFileAsync("TagApi.GetList.json");
var response = await _tagApi.GetListAsync();
CorrectMethodShouldGetCalled("pwg.tags.getList");
await Verify(response);
}
}

View File

@ -0,0 +1,12 @@
using Newtonsoft.Json;
namespace Piwigo.Client.Tags;
public record AssignedTag : Tag
{
[JsonProperty("counter")]
public int Counter { get; init; }
[JsonProperty("url")]
public string Url { get; init; }
}

View File

@ -2,4 +2,32 @@ namespace Piwigo.Client.Tags;
public interface ITagApi
{
/// <summary>
/// Adds a new tag to the gallery
/// </summary>
/// <param name="name">the name of the tag</param>
/// <param name="cancellationToken">
/// <see cref="CancellationToken" />
/// </param>
/// <returns>Information about the uploaded tag</returns>
Task<TagAdded> AddAsync(string name, CancellationToken cancellationToken = default);
/// <summary>
/// Gets a list of all known tags. This call is only available for admins
/// </summary>
/// <param name="cancellationToken">
/// <see cref="CancellationToken" />
/// </param>
/// <returns>all available tags</returns>
Task<IReadOnlyCollection<Tag>> GetAdminListAsync(CancellationToken cancellationToken = default);
/// <summary>
/// Gits a list of known tags that are at least assigned to one image
/// </summary>
/// <param name="sortByCounter">sorts the result by the number of assigned images of a tag</param>
/// <param name="cancellationToken">
/// <see cref="CancellationToken" />
/// </param>
/// <returns>all tags with at least one image assigned</returns>
Task<IReadOnlyCollection<AssignedTag>> GetListAsync(bool? sortByCounter = null, CancellationToken cancellationToken = default);
}

View File

@ -0,0 +1,20 @@
using System.Diagnostics.CodeAnalysis;
using Newtonsoft.Json;
namespace Piwigo.Client.Tags;
[SuppressMessage("ReSharper", "StringLiteralTypo")]
public record Tag
{
[JsonProperty("id")]
public int Id { get; init; }
[JsonProperty("name")]
public string? Name { get; init; }
[JsonProperty("url_name")]
public string? UrlName { get; init; }
[JsonProperty("lastmodified")]
public DateTime? LastModified { get; init; }
}

View File

@ -0,0 +1,18 @@
using Newtonsoft.Json;
namespace Piwigo.Client.Tags;
public record TagAdded
{
[JsonProperty("info")]
public string? Info { get; init; }
[JsonProperty("id")]
public int Id { get; init; }
[JsonProperty("name")]
public string? Name { get; init; }
[JsonProperty("url_name")]
public string? UrlName { get; init; }
}

View File

@ -1,5 +1,40 @@
using Microsoft.Extensions.Logging;
namespace Piwigo.Client.Tags;
public class TagApi : ITagApi
{
private readonly IPiwigoContext _context;
private readonly ILogger<TagApi> _logger;
public TagApi(IPiwigoContext context, ILogger<TagApi> logger)
{
_context = context ?? throw new ArgumentNullException(nameof(context));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
public async Task<TagAdded> AddAsync(string name, CancellationToken cancellationToken = default)
{
var formParams = new Dictionary<string, string>
{
{ "name", name }
};
var response = await _context.PostAsync<PiwigoResponse<TagAdded>>(_logger, "pwg.tags.add", formParams, cancellationToken);
return response.Result;
}
public async Task<IReadOnlyCollection<Tag>> GetAdminListAsync(CancellationToken cancellationToken = default)
{
var response = await _context.PostAsync<PiwigoResponse<TagList<Tag>>>(_logger, "pwg.tags.getAdminList", new Dictionary<string, string>(), cancellationToken);
return response.Result.Tags;
}
public async Task<IReadOnlyCollection<AssignedTag>> GetListAsync(bool? sortByCounter = null, CancellationToken cancellationToken = default)
{
var formParams = new Dictionary<string, string>();
formParams.AddIfValueNotNull("sort_by_counter", sortByCounter?.ToString());
var response = await _context.PostAsync<PiwigoResponse<TagList<AssignedTag>>>(_logger, "pwg.tags.getList", formParams, cancellationToken);
return response.Result.Tags;
}
}

View File

@ -0,0 +1,9 @@
using Newtonsoft.Json;
namespace Piwigo.Client.Tags;
internal record TagList<TTag>
{
[JsonProperty("tags")]
public IReadOnlyCollection<TTag> Tags { get; init; } = null!;
}