diff --git a/src/Piwigo.Client.Tests/ApiTestsBase.cs b/src/Piwigo.Client.Tests/ApiTestsBase.cs index af932fb..7b1f319 100644 --- a/src/Piwigo.Client.Tests/ApiTestsBase.cs +++ b/src/Piwigo.Client.Tests/ApiTestsBase.cs @@ -16,13 +16,19 @@ public class ApiTestsBase protected HttpTest? HttpTest { get; private set; } protected IPiwigoContext Context { get; private set; } = null!; - [SetUp] - public void SetUp() + protected void ResetHttpTest() { + HttpTest?.Dispose(); if (UseHttpTest) { HttpTest = new HttpTest(); } + } + + [SetUp] + public void SetUp() + { + ResetHttpTest(); Context = new PiwigoContext(new PiwigoConfiguration(TestUri, Username, Password), new NullLogger()); OnSetUp(); diff --git a/src/Piwigo.Client.Tests/SessionApiTests.cs b/src/Piwigo.Client.Tests/SessionApiTests.cs index 3954a2c..a5ac65e 100644 --- a/src/Piwigo.Client.Tests/SessionApiTests.cs +++ b/src/Piwigo.Client.Tests/SessionApiTests.cs @@ -25,16 +25,7 @@ public class SessionApiTests : ApiTestsBase public async Task GetStatus_should_return_config() { await LoginAsync(); - var serverResponse = new PiwigoResponse - { - Status = "OK", - Result = new SessionStatus - { - Username = "admin", - Version = "12.0.0" - } - }; - SetJsonResult(serverResponse); + SetJsonResult(GetStatusResponse()); var status = await _sessionApi.GetStatusAsync(); @@ -43,6 +34,34 @@ public class SessionApiTests : ApiTestsBase CorrectMethodShouldGetCalled("pwg.session.getStatus"); } + [Test] + public async Task GetUploadChunkSizeAsync_should_get_size_from_session() + { + await LoginAsync(); + SetJsonResult(GetStatusResponse()); + + var chunkSize = await _sessionApi.GetUploadChunkSizeAsync(); + + chunkSize.Should().Be(512); + CorrectMethodShouldGetCalled("pwg.session.getStatus"); + } + + [Test] + public async Task GetUploadChunkSizeAsync_should_only_get_size_from_session_once() + { + await LoginAsync(); + SetJsonResult(GetStatusResponse()); + + var chunkSize = await _sessionApi.GetUploadChunkSizeAsync(); + chunkSize.Should().Be(512); + CorrectMethodShouldGetCalled("pwg.session.getStatus"); + + ResetHttpTest(); + chunkSize = await _sessionApi.GetUploadChunkSizeAsync(); + chunkSize.Should().Be(512); + HttpTest?.CallLog.Should().BeEmpty(); + } + [Test] public async Task Logout_should_set_IsLoggedIn_to_false() { @@ -62,4 +81,18 @@ public class SessionApiTests : ApiTestsBase await sessionApi.LoginAsync(); Context.IsLoggedIn.Should().BeTrue(); } + + private static PiwigoResponse GetStatusResponse() + { + return new PiwigoResponse + { + Status = "OK", + Result = new SessionStatus + { + Username = "admin", + Version = "12.0.0", + UploadFormChunkSize = 512 + } + }; + } } \ No newline at end of file diff --git a/src/Piwigo.Client/Session/ISessionApi.cs b/src/Piwigo.Client/Session/ISessionApi.cs index 5880caa..8833e01 100644 --- a/src/Piwigo.Client/Session/ISessionApi.cs +++ b/src/Piwigo.Client/Session/ISessionApi.cs @@ -2,7 +2,34 @@ namespace Piwigo.Client.Session; public interface ISessionApi { + /// + /// Logs into the configured piwigo server using the configured credentials. + /// + /// + /// + /// Task LoginAsync(CancellationToken cancellationToken = default); + + /// + /// Closes current session and logs out of the configured piwigo server. + /// + /// + /// + /// Task LogoutAsync(CancellationToken cancellationToken = default); + + /// + /// Gets the status of the current session including some useful information about the server. + /// + /// + /// Task GetStatusAsync(CancellationToken cancellationToken = default); + + /// + /// Gets the upload chunk size that the server specifies for image upload. + /// The chunk size is only get once and cleared on logout. + /// + /// + /// + ValueTask GetUploadChunkSizeAsync(CancellationToken cancellationToken = default); } \ No newline at end of file diff --git a/src/Piwigo.Client/Session/SessionApi.cs b/src/Piwigo.Client/Session/SessionApi.cs index b37e458..c6dd37a 100644 --- a/src/Piwigo.Client/Session/SessionApi.cs +++ b/src/Piwigo.Client/Session/SessionApi.cs @@ -6,6 +6,7 @@ public class SessionApi : ISessionApi { private readonly IPiwigoContext _context; private readonly ILogger _logger; + private int _chunkSizeInKiB; public SessionApi(IPiwigoContext context, ILogger logger) { @@ -13,9 +14,10 @@ public class SessionApi : ISessionApi _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } - public Task LogoutAsync(CancellationToken cancellationToken = default) + public async Task LogoutAsync(CancellationToken cancellationToken = default) { - return _context.LogoutAsync(cancellationToken); + await _context.LogoutAsync(cancellationToken); + _chunkSizeInKiB = 0; } public Task LoginAsync(CancellationToken cancellationToken = default) @@ -28,4 +30,16 @@ public class SessionApi : ISessionApi var typedResponse = await _context.PostAsync>(_logger, "pwg.session.getStatus", cancellationToken); return typedResponse.Result; } + + public async ValueTask GetUploadChunkSizeAsync(CancellationToken cancellationToken = default) + { + if (_chunkSizeInKiB > 0) + { + return _chunkSizeInKiB; + } + + var status = await GetStatusAsync(cancellationToken); + _chunkSizeInKiB = Math.Max(status.UploadFormChunkSize, 512); + return _chunkSizeInKiB; + } } \ No newline at end of file