2022-10-07 13:43:19 +02:00
|
|
|
using System.Diagnostics;
|
2022-10-06 23:54:44 +02:00
|
|
|
using System.Net;
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
using Flurl;
|
|
|
|
using Flurl.Http;
|
2022-10-07 13:43:19 +02:00
|
|
|
using Microsoft.Extensions.Logging;
|
2022-10-06 23:54:44 +02:00
|
|
|
|
|
|
|
namespace Piwigo.Client;
|
|
|
|
|
|
|
|
public class PiwigoClient : IPiwigoClient
|
|
|
|
{
|
|
|
|
private CookieJar _cookies = new();
|
|
|
|
private string _piwigoBaseUri = null!;
|
|
|
|
public bool IsLoggedIn { get; private set; }
|
|
|
|
public int ChunkSize { get; set; } = 512;
|
2022-10-07 13:43:19 +02:00
|
|
|
private IFlurlRequest Request => _piwigoBaseUri.WithCookies(_cookies).ConfigureRequest(r => r.AfterCallAsync = LogResponse);
|
|
|
|
|
|
|
|
private readonly ILogger<PiwigoClient> _logger;
|
|
|
|
|
|
|
|
public PiwigoClient(ILogger<PiwigoClient> logger)
|
|
|
|
{
|
|
|
|
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
|
|
|
}
|
2022-10-06 23:54:44 +02:00
|
|
|
|
|
|
|
public async Task LoginAsync(Uri uri, string username, string password)
|
|
|
|
{
|
|
|
|
if (uri == null)
|
|
|
|
{
|
|
|
|
throw new ArgumentNullException(nameof(uri));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(username))
|
|
|
|
{
|
|
|
|
throw new ArgumentException("Value cannot be null or whitespace.", nameof(username));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(password))
|
|
|
|
{
|
|
|
|
throw new ArgumentException("Value cannot be null or whitespace.", nameof(password));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsLoggedIn)
|
|
|
|
{
|
|
|
|
throw new PiwigoException("The client is already logged in. Create a new instance or log out first!");
|
|
|
|
}
|
|
|
|
|
|
|
|
_piwigoBaseUri = uri.AppendPathSegment("ws.php").SetQueryParam("format", "json");
|
|
|
|
|
2022-10-07 13:43:19 +02:00
|
|
|
_logger.LogInformation("Logging into {PiwigoBaseUri} using username {Username}", _piwigoBaseUri, username);
|
|
|
|
|
2022-10-06 23:54:44 +02:00
|
|
|
var response = await _piwigoBaseUri.WithCookies(out var cookieJar).PostMultipartAsync(c =>
|
|
|
|
c.PiwigoLogin(username, password));
|
|
|
|
|
|
|
|
if (response.StatusCode != (int)HttpStatusCode.OK)
|
|
|
|
{
|
2022-10-07 13:43:19 +02:00
|
|
|
_logger.LogError("Failed to log in {StatusCode}", response.StatusCode);
|
2022-10-06 23:54:44 +02:00
|
|
|
throw new PiwigoException($"Could not log in to {_piwigoBaseUri} using username {username}");
|
|
|
|
}
|
|
|
|
|
2022-10-07 13:43:19 +02:00
|
|
|
_logger.LogInformation("Logging in succeeded");
|
2022-10-06 23:54:44 +02:00
|
|
|
_cookies = cookieJar;
|
|
|
|
IsLoggedIn = true;
|
|
|
|
}
|
|
|
|
|
2022-10-07 13:43:19 +02:00
|
|
|
public async Task LogoutAsync()
|
2022-10-06 23:54:44 +02:00
|
|
|
{
|
|
|
|
EnsureLoggedIn();
|
|
|
|
|
2022-10-07 13:43:19 +02:00
|
|
|
_logger.LogInformation("Logging out from {Uri}", _piwigoBaseUri);
|
2022-10-06 23:54:44 +02:00
|
|
|
await Request.PostMultipartAsync(c => c.PiwigoLogout());
|
|
|
|
|
|
|
|
IsLoggedIn = false;
|
|
|
|
_cookies.Clear();
|
|
|
|
}
|
|
|
|
|
2022-10-07 13:43:19 +02:00
|
|
|
public async Task<PiwigoStatus> GetStatusAsync()
|
|
|
|
{
|
|
|
|
EnsureLoggedIn();
|
|
|
|
|
|
|
|
_logger.LogInformation("Getting status");
|
|
|
|
var response = await Request.PostMultipartAsync(c => c.PiwigoGetStatus());
|
|
|
|
var typedResponse = await response.GetJsonAsync<PiwigoResponse<PiwigoStatus>>();
|
|
|
|
return typedResponse.Result;
|
|
|
|
}
|
|
|
|
|
2022-10-06 23:54:44 +02:00
|
|
|
private void EnsureLoggedIn([CallerMemberName] string? callerName = null)
|
|
|
|
{
|
|
|
|
if (!IsLoggedIn)
|
|
|
|
{
|
|
|
|
throw new InvalidOperationException($"Could not execute {callerName} as the client is not logged in.");
|
|
|
|
}
|
|
|
|
}
|
2022-10-07 13:43:19 +02:00
|
|
|
|
|
|
|
private async Task LogResponse(FlurlCall call)
|
|
|
|
{
|
|
|
|
var responseString = await call.Response.GetStringAsync();
|
|
|
|
_logger.LogDebug("PiwigoResponse: {Response}", responseString);
|
|
|
|
}
|
2022-10-06 23:54:44 +02:00
|
|
|
}
|