adds GetUploadChunkSizeAsync to session api

This commit is contained in:
Philipp Häfelfinger 2022-11-15 23:30:54 +01:00
parent d6aaefc0d2
commit 72fbed8604
4 changed files with 94 additions and 14 deletions

View File

@ -16,13 +16,19 @@ public class ApiTestsBase
protected HttpTest? HttpTest { get; private set; } protected HttpTest? HttpTest { get; private set; }
protected IPiwigoContext Context { get; private set; } = null!; protected IPiwigoContext Context { get; private set; } = null!;
[SetUp] protected void ResetHttpTest()
public void SetUp()
{ {
HttpTest?.Dispose();
if (UseHttpTest) if (UseHttpTest)
{ {
HttpTest = new HttpTest(); HttpTest = new HttpTest();
} }
}
[SetUp]
public void SetUp()
{
ResetHttpTest();
Context = new PiwigoContext(new PiwigoConfiguration(TestUri, Username, Password), new NullLogger<PiwigoContext>()); Context = new PiwigoContext(new PiwigoConfiguration(TestUri, Username, Password), new NullLogger<PiwigoContext>());
OnSetUp(); OnSetUp();

View File

@ -25,16 +25,7 @@ public class SessionApiTests : ApiTestsBase
public async Task GetStatus_should_return_config() public async Task GetStatus_should_return_config()
{ {
await LoginAsync(); await LoginAsync();
var serverResponse = new PiwigoResponse<SessionStatus> SetJsonResult(GetStatusResponse());
{
Status = "OK",
Result = new SessionStatus
{
Username = "admin",
Version = "12.0.0"
}
};
SetJsonResult(serverResponse);
var status = await _sessionApi.GetStatusAsync(); var status = await _sessionApi.GetStatusAsync();
@ -43,6 +34,34 @@ public class SessionApiTests : ApiTestsBase
CorrectMethodShouldGetCalled("pwg.session.getStatus"); 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] [Test]
public async Task Logout_should_set_IsLoggedIn_to_false() public async Task Logout_should_set_IsLoggedIn_to_false()
{ {
@ -62,4 +81,18 @@ public class SessionApiTests : ApiTestsBase
await sessionApi.LoginAsync(); await sessionApi.LoginAsync();
Context.IsLoggedIn.Should().BeTrue(); Context.IsLoggedIn.Should().BeTrue();
} }
private static PiwigoResponse<SessionStatus> GetStatusResponse()
{
return new PiwigoResponse<SessionStatus>
{
Status = "OK",
Result = new SessionStatus
{
Username = "admin",
Version = "12.0.0",
UploadFormChunkSize = 512
}
};
}
} }

View File

@ -2,7 +2,34 @@ namespace Piwigo.Client.Session;
public interface ISessionApi public interface ISessionApi
{ {
/// <summary>
/// Logs into the configured piwigo server using the configured credentials.
/// <see cref="IPiwigoConfiguration" />
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task LoginAsync(CancellationToken cancellationToken = default); Task LoginAsync(CancellationToken cancellationToken = default);
/// <summary>
/// Closes current session and logs out of the configured piwigo server.
/// <see cref="IPiwigoConfiguration" />
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task LogoutAsync(CancellationToken cancellationToken = default); Task LogoutAsync(CancellationToken cancellationToken = default);
/// <summary>
/// Gets the status of the current session including some useful information about the server.
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task<SessionStatus> GetStatusAsync(CancellationToken cancellationToken = default); Task<SessionStatus> GetStatusAsync(CancellationToken cancellationToken = default);
/// <summary>
/// Gets the upload chunk size that the server specifies for image upload.
/// The chunk size is only get once and cleared on logout.
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns></returns>
ValueTask<int> GetUploadChunkSizeAsync(CancellationToken cancellationToken = default);
} }

View File

@ -6,6 +6,7 @@ public class SessionApi : ISessionApi
{ {
private readonly IPiwigoContext _context; private readonly IPiwigoContext _context;
private readonly ILogger<SessionApi> _logger; private readonly ILogger<SessionApi> _logger;
private int _chunkSizeInKiB;
public SessionApi(IPiwigoContext context, ILogger<SessionApi> logger) public SessionApi(IPiwigoContext context, ILogger<SessionApi> logger)
{ {
@ -13,9 +14,10 @@ public class SessionApi : ISessionApi
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); _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) public Task LoginAsync(CancellationToken cancellationToken = default)
@ -28,4 +30,16 @@ public class SessionApi : ISessionApi
var typedResponse = await _context.PostAsync<PiwigoResponse<SessionStatus>>(_logger, "pwg.session.getStatus", cancellationToken); var typedResponse = await _context.PostAsync<PiwigoResponse<SessionStatus>>(_logger, "pwg.session.getStatus", cancellationToken);
return typedResponse.Result; return typedResponse.Result;
} }
public async ValueTask<int> GetUploadChunkSizeAsync(CancellationToken cancellationToken = default)
{
if (_chunkSizeInKiB > 0)
{
return _chunkSizeInKiB;
}
var status = await GetStatusAsync(cancellationToken);
_chunkSizeInKiB = Math.Max(status.UploadFormChunkSize, 512);
return _chunkSizeInKiB;
}
} }