Пример #1
0
        public async Task <(Data token, string baseUri)> Authenticate()
        {
            var timeToLiveElapsed = _lastUpdated.Add(_settings.AuthenticationTimeToLive) <= _dateTimeProvider();

            if (!_forceAuthenticate && _response != null && !timeToLiveElapsed)
            {
                return(_response.data, _response.api);
            }

            _forceAuthenticate = false;
            _authenticate?.Invoke();

            _lastUpdated = _dateTimeProvider();
            var payload = new
            {
                account = _settings.Username,
                pwd     = _settings.Password
            };
            var result = await _executeRequest(_settings.AuthenticationUri,
                                               payload,
                                               Data.CreateEmpty)
                         .ConfigureAwait(false);

            if (!result.IsSuccessful)
            {
                throw AuthenticationFailed.Create(result);
            }

            _response = LoginResponse.From(result.Content);

            if (_response.hasError || _response.code == authenticationErrorCode)
            {
                throw AuthenticationFailed.Create(_response);
            }

            return(_response.data, _response.api);
        }