piwigodotnet/PiwigoDotnet/Piwigo.Client/PiwigoContext.cs

131 lines
4.1 KiB
C#

using System.Net;
using Flurl.Http;
using Flurl.Http.Content;
using Microsoft.Extensions.Logging;
namespace Piwigo.Client;
public class PiwigoContext : IPiwigoContext
{
private readonly IPiwigoConfiguration _config;
private readonly CookieJar _cookies = new();
private readonly ILogger<PiwigoContext> _logger;
public PiwigoContext(IPiwigoConfiguration configuration, ILogger<PiwigoContext> logger)
{
_config = configuration ?? throw new ArgumentNullException(nameof(configuration));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
public bool IsLoggedIn { get; private set; }
public async Task LoginAsync()
{
var userName = _config.UserName;
if (string.IsNullOrWhiteSpace(userName))
{
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));
}
if (IsLoggedIn)
{
throw new PiwigoException("The client is already logged in. Create a new instance or log out first!");
}
_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;
}
public async Task LogoutAsync()
{
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"));
_logger.LogInformation("logged out, clearing cookies");
IsLoggedIn = false;
_cookies.Clear();
}
public Task<T> PostAsync<T>(ILogger logger, string method)
{
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));
}
private static async Task LogResponse(FlurlCall call, ILogger logger)
{
var responseString = await call.Response.GetStringAsync();
logger.LogDebug("PiwigoResponse: {Response}", responseString);
}
}