starts using json strings from api examples to check mapping
adds CalculateOrphansAsync to AlbumApi removes some not needed log lines removes tests explicit login calls
This commit is contained in:
parent
181ccef825
commit
09a7a40dff
@ -14,11 +14,27 @@ public class AlbumApiTests : ApiTestsBase
|
||||
_albumApi = new AlbumApi(Context, new NullLogger<AlbumApi>());
|
||||
}
|
||||
|
||||
[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
|
||||
} ]
|
||||
}");
|
||||
var response = await _albumApi.CalculateOrphansAsync(1);
|
||||
|
||||
response.Should().NotBeNull();
|
||||
response.AssociatedOutsideCount.Should().Be(1);
|
||||
response.BecomingOrphanCount.Should().Be(2);
|
||||
response.RecursiveImageCount.Should().Be(3);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Add_should_create_album_and_return_id()
|
||||
{
|
||||
await LoginAsync();
|
||||
|
||||
var serverResponse = new PiwigoResponse<AlbumAdded>
|
||||
{
|
||||
Result = new AlbumAdded
|
||||
@ -36,8 +52,6 @@ public class AlbumApiTests : ApiTestsBase
|
||||
[Test]
|
||||
public async Task GetAll_should_return_all_existing_albums()
|
||||
{
|
||||
await LoginAsync();
|
||||
|
||||
var serverResponse = new PiwigoResponse<AlbumList>
|
||||
{
|
||||
Result = new AlbumList
|
||||
|
@ -37,12 +37,15 @@ public class ApiTestsBase
|
||||
HttpTest?.Dispose();
|
||||
}
|
||||
|
||||
protected async Task LoginAsync()
|
||||
|
||||
internal void SetOkResult()
|
||||
{
|
||||
HttpTest?.RespondWith("{}", 200, cookies: new { pwg_id = "pwg_id" });
|
||||
var sessionApi = new SessionApi(Context, new NullLogger<SessionApi>());
|
||||
await sessionApi.LoginAsync();
|
||||
Context.IsLoggedIn.Should().BeTrue();
|
||||
SetJsonResult(@"{stat: ""ok""}");
|
||||
}
|
||||
|
||||
internal void SetJsonResult(string json)
|
||||
{
|
||||
HttpTest?.RespondWith(json);
|
||||
}
|
||||
|
||||
internal void SetJsonResult<T>(PiwigoResponse<T> serverResponse)
|
||||
|
@ -39,7 +39,6 @@ public class SessionApiTests : ApiTestsBase
|
||||
|
||||
status.Should().NotBeNull();
|
||||
status.Username.Should().Be("admin");
|
||||
status.Version.Should().NotBeEmpty();
|
||||
}
|
||||
|
||||
[Test]
|
||||
@ -47,9 +46,17 @@ public class SessionApiTests : ApiTestsBase
|
||||
{
|
||||
await LoginAsync();
|
||||
|
||||
HttpTest?.RespondWith("OK");
|
||||
SetOkResult();
|
||||
|
||||
await _sessionApi.LogoutAsync();
|
||||
Context.IsLoggedIn.Should().BeFalse();
|
||||
}
|
||||
|
||||
private async Task LoginAsync()
|
||||
{
|
||||
HttpTest?.RespondWith("{}", 200, cookies: new { pwg_id = "pwg_id" });
|
||||
var sessionApi = new SessionApi(Context, new NullLogger<SessionApi>());
|
||||
await sessionApi.LoginAsync();
|
||||
Context.IsLoggedIn.Should().BeTrue();
|
||||
}
|
||||
}
|
@ -15,6 +15,14 @@ public class AlbumApi : IAlbumApi
|
||||
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||
}
|
||||
|
||||
public async Task<AlbumOrphans> CalculateOrphansAsync(int albumId)
|
||||
{
|
||||
var formParams = new Dictionary<string, string> { { "category_id", albumId.ToString() } };
|
||||
var response = await _context.PostAsync<PiwigoResponse<AlbumOrphans[]>>(_logger, "pwg.categories.calculateOrphans", formParams);
|
||||
// the API seems to only return one result but returns it as a list in json
|
||||
return response.Result.First();
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
@ -49,7 +57,6 @@ public class AlbumApi : IAlbumApi
|
||||
|
||||
public async Task<IReadOnlyCollection<Album>> GetAllAsync()
|
||||
{
|
||||
_logger.LogInformation("Getting all existing categories from server");
|
||||
var formParams = new Dictionary<string, string> { { "recursive", "true" } };
|
||||
var response = await _context.PostAsync<PiwigoResponse<AlbumList>>(_logger, "pwg.categories.getList", formParams);
|
||||
return new ReadOnlyCollection<Album>(response.Result.Albums);
|
||||
|
15
PiwigoDotnet/Piwigo.Client/Contract/AlbumOrphans.cs
Normal file
15
PiwigoDotnet/Piwigo.Client/Contract/AlbumOrphans.cs
Normal file
@ -0,0 +1,15 @@
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Piwigo.Client.Contract;
|
||||
|
||||
public class AlbumOrphans
|
||||
{
|
||||
[JsonProperty("nb_images_associated_outside")]
|
||||
public int? AssociatedOutsideCount { get; init; }
|
||||
|
||||
[JsonProperty("nb_images_becoming_orphan")]
|
||||
public int? BecomingOrphanCount { get; init; }
|
||||
|
||||
[JsonProperty("nb_images_recursive")]
|
||||
public int? RecursiveImageCount { get; init; }
|
||||
}
|
@ -4,6 +4,8 @@ namespace Piwigo.Client;
|
||||
|
||||
public interface IAlbumApi
|
||||
{
|
||||
Task<AlbumOrphans> CalculateOrphansAsync(int albumId);
|
||||
|
||||
Task<int> AddAsync(string name, int? parentId = null, string? comment = null, bool? visible = null, CategoryStatus? status = null, bool? commentable = null,
|
||||
CategoryPosition? position = null);
|
||||
|
||||
|
@ -44,12 +44,10 @@ public class PiwigoContext : IPiwigoContext
|
||||
|
||||
if (response.StatusCode != (int)HttpStatusCode.OK)
|
||||
{
|
||||
_logger.LogError("Failed to log in {StatusCode}", response.StatusCode);
|
||||
throw new PiwigoException($"Could not log in to {_config.BaseUri} using username {userName}");
|
||||
}
|
||||
|
||||
_logger.LogInformation("Logging in succeeded");
|
||||
_logger.LogInformation("logged in");
|
||||
IsLoggedIn = true;
|
||||
}
|
||||
|
||||
@ -63,7 +61,6 @@ public class PiwigoContext : IPiwigoContext
|
||||
|
||||
_logger.LogInformation("Logging out from {Uri}", _config.BaseUri);
|
||||
await ConfigureRequest(_logger).PostMultipartAsync(c => c.AddMethod("pwg.session.logout"));
|
||||
_logger.LogInformation("logged out, clearing cookies");
|
||||
IsLoggedIn = false;
|
||||
_cookies.Clear();
|
||||
}
|
||||
@ -99,6 +96,11 @@ public class PiwigoContext : IPiwigoContext
|
||||
}
|
||||
});
|
||||
|
||||
if (response.StatusCode != (int)HttpStatusCode.OK)
|
||||
{
|
||||
throw new PiwigoException($"failed to call {method} on {_config.BaseUri}: {response.StatusCode}");
|
||||
}
|
||||
|
||||
var typedResponse = await response.GetJsonAsync<T>();
|
||||
return typedResponse;
|
||||
}
|
||||
@ -126,6 +128,6 @@ public class PiwigoContext : IPiwigoContext
|
||||
private static async Task LogResponse(FlurlCall call, ILogger logger)
|
||||
{
|
||||
var responseString = await call.Response.GetStringAsync();
|
||||
logger.LogDebug("PiwigoResponse: {Response}", responseString);
|
||||
logger.LogDebug("Response: {Response}", responseString);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user