2022-10-16 22:58:38 +02:00
|
|
|
using System.Net;
|
2022-10-15 00:07:59 +02:00
|
|
|
using Flurl.Http;
|
2022-10-16 22:58:38 +02:00
|
|
|
using Flurl.Http.Content;
|
2022-10-15 00:07:59 +02:00
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
|
|
namespace Piwigo.Client;
|
|
|
|
|
|
|
|
public class PiwigoContext : IPiwigoContext
|
|
|
|
{
|
2022-10-16 22:58:38 +02:00
|
|
|
private readonly IPiwigoConfiguration _config;
|
2022-10-15 00:07:59 +02:00
|
|
|
private readonly CookieJar _cookies = new();
|
|
|
|
private readonly ILogger<PiwigoContext> _logger;
|
|
|
|
|
|
|
|
public PiwigoContext(IPiwigoConfiguration configuration, ILogger<PiwigoContext> logger)
|
|
|
|
{
|
2022-10-16 22:58:38 +02:00
|
|
|
_config = configuration ?? throw new ArgumentNullException(nameof(configuration));
|
2022-10-15 00:07:59 +02:00
|
|
|
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool IsLoggedIn { get; private set; }
|
|
|
|
|
2022-10-16 22:58:38 +02:00
|
|
|
public async Task LoginAsync()
|
2022-10-15 00:07:59 +02:00
|
|
|
{
|
2022-10-16 22:58:38 +02:00
|
|
|
var userName = _config.UserName;
|
|
|
|
if (string.IsNullOrWhiteSpace(userName))
|
2022-10-15 00:07:59 +02:00
|
|
|
{
|
2022-10-16 22:58:38 +02:00
|
|
|
throw new ArgumentException("Value cannot be null or whitespace.", nameof(userName));
|
|
|
|
}
|
|
|
|
|
|
|
|
var password = _config.Password;
|
|
|
|
if (string.IsNullOrWhiteSpace(password))
|
|
|
|
{
|
|
|
|
throw new ArgumentException("Value cannot be null or whitespace.", nameof(password));
|
2022-10-15 00:07:59 +02:00
|
|
|
}
|
|
|
|
|
2022-10-16 22:58:38 +02:00
|
|
|
if (IsLoggedIn)
|
2022-10-15 00:07:59 +02:00
|
|
|
{
|
2022-10-16 22:58:38 +02:00
|
|
|
throw new PiwigoException("The client is already logged in. Create a new instance or log out first!");
|
2022-10-15 00:07:59 +02:00
|
|
|
}
|
|
|
|
|
2022-10-16 22:58:38 +02:00
|
|
|
_logger.LogInformation("Logging into {PiwigoBaseUri} using username {Username}", _config.BaseUri, userName);
|
|
|
|
|
|
|
|
var response = await ConfigureRequest(_logger).PostMultipartAsync(c => c.AddMethod("pwg.session.login").AddString("username", userName).AddString("password", password));
|
|
|
|
|
|
|
|
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;
|
2022-10-15 00:07:59 +02:00
|
|
|
}
|
|
|
|
|
2022-10-16 22:58:38 +02:00
|
|
|
public async Task LogoutAsync()
|
2022-10-15 00:07:59 +02:00
|
|
|
{
|
2022-10-16 22:58:38 +02:00
|
|
|
if (!IsLoggedIn)
|
|
|
|
{
|
|
|
|
_logger.LogWarning("Tried to log out from {Uri} but was not logged in!", _config.BaseUri);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_logger.LogInformation("Logging out from {Uri}", _config.BaseUri);
|
|
|
|
await ConfigureRequest(_logger).PostMultipartAsync(c => c.AddMethod("pwg.session.logout"));
|
2022-10-15 00:07:59 +02:00
|
|
|
_logger.LogInformation("logged out, clearing cookies");
|
|
|
|
IsLoggedIn = false;
|
|
|
|
_cookies.Clear();
|
|
|
|
}
|
|
|
|
|
2022-10-16 22:58:38 +02:00
|
|
|
public Task<T> PostAsync<T>(ILogger logger, string method)
|
2022-10-15 00:07:59 +02:00
|
|
|
{
|
2022-10-16 22:58:38 +02:00
|
|
|
return PostInternalAsync<T>(logger, method, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Task<T> PostAsync<T>(ILogger logger, string method, IDictionary<string, string> formParams)
|
|
|
|
{
|
|
|
|
return PostInternalAsync<T>(logger, method, formParams);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void AddFormParams(CapturedMultipartContent c, IDictionary<string, string> formParams)
|
|
|
|
{
|
|
|
|
foreach (var formParam in formParams)
|
|
|
|
{
|
|
|
|
c.AddString(formParam.Key, formParam.Value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task<T> PostInternalAsync<T>(ILogger logger, string method, IDictionary<string, string>? formParams)
|
|
|
|
{
|
|
|
|
await EnsureLoggedInAsync();
|
|
|
|
|
|
|
|
var response = await ConfigureRequest(logger).PostMultipartAsync(c =>
|
|
|
|
{
|
|
|
|
c.AddMethod(method);
|
|
|
|
if (formParams != null)
|
|
|
|
{
|
|
|
|
AddFormParams(c, formParams);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
var typedResponse = await response.GetJsonAsync<T>();
|
|
|
|
return typedResponse;
|
|
|
|
}
|
|
|
|
|
|
|
|
private async ValueTask EnsureLoggedInAsync()
|
|
|
|
{
|
|
|
|
if (IsLoggedIn)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await LoginAsync();
|
|
|
|
}
|
|
|
|
|
|
|
|
private IFlurlRequest ConfigureRequest(ILogger logger)
|
|
|
|
{
|
|
|
|
if (logger == null)
|
|
|
|
{
|
|
|
|
throw new ArgumentNullException(nameof(logger));
|
|
|
|
}
|
|
|
|
|
|
|
|
return _config.BaseUri.WithCookies(_cookies).ConfigureRequest(r => r.AfterCallAsync = call => LogResponse(call, logger));
|
2022-10-15 00:07:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private static async Task LogResponse(FlurlCall call, ILogger logger)
|
|
|
|
{
|
|
|
|
var responseString = await call.Response.GetStringAsync();
|
|
|
|
logger.LogDebug("PiwigoResponse: {Response}", responseString);
|
|
|
|
}
|
|
|
|
}
|