internal virtual async Task LoginInternalAsync(string username, string password) { if (_isFirstLogin) { _isFirstLogin = false; await LogManager.WriteInitialLog().ConfigureAwait(false); } if (LoginState != LoginState.LoggedOut) { await LogoutInternalAsync().ConfigureAwait(false); } LoginState = LoginState.LoggingIn; try { await ApiClient.LoginAsync(username, password).ConfigureAwait(false); await OnLoginAsync(username).ConfigureAwait(false); LoginState = LoginState.LoggedIn; } catch { await LogoutInternalAsync().ConfigureAwait(false); throw; } await _loggedInEvent.InvokeAsync().ConfigureAwait(false); }
public qBittorrentClient(Uri host, string userAgent = "qbit-api-core", RetryMode retry = RetryMode.AlwaysRetry, LogSeverity minimum = LogSeverity.Info) { Preconditions.NotNull(host, nameof(host)); LogManager = new LogManager(minimum); ApiClient = new qBitApiClient(userAgent, retry); ApiClient.SetBaseUrl(Path.Combine(host.AbsoluteUri, $"api/v{qBitApiConfig.APIVersion}")); LogManager.Message += async msg => await _logEvent.InvokeAsync(msg).ConfigureAwait(false); ApiClient.SentRequest += async(method, endpoint, millis) => { await _restLogger.VerboseAsync($"{method} {endpoint}: {millis} ms").ConfigureAwait(false); }; _restLogger = LogManager.CreateLogger("Rest"); _stateLock = new SemaphoreSlim(1, 1); _isFirstLogin = false; _cancelToken = new CancellationTokenSource(); }
internal virtual async Task LogoutInternalAsync() { if (LoginState == LoginState.LoggedOut) { return; } LoginState = LoginState.LoggingOut; await ApiClient.LogoutAsync().ConfigureAwait(false); await OnLogoutAsync().ConfigureAwait(false); LoginState = LoginState.LoggedOut; await _loggedOutEvent.InvokeAsync().ConfigureAwait(false); }