fedarovich / qbittorrent-net-client

qBittorrent client library for .Net
MIT License
32 stars 14 forks source link

Add a way to check if the client is logged in #9

Open Hona opened 3 years ago

Hona commented 3 years ago

After the QbittorrentClient instance runs for long enough, it returns 403 Forbidden to requests, so it would be nice to have a way to check if the client is logged in, and if not refresh authentication/relogin

Hona commented 3 years ago

If anyone runs into this issue with no workaround, I added this code which should work:

private async Task EnsureLoggedIn()
{
    try
    {
        await _qbittorrent.GetApiVersionAsync();
    }
    catch (QBittorrentClientRequestException e)
    {
        if (e.StatusCode == HttpStatusCode.Forbidden)
        {
            _logger.LogWarning("Qbittorrent logged out, logging in again");
            await _qbittorrent.LoginAsync(_username, _password);
        }
    }
}