Beispiel #1
0
        public async Task <DeveloperApiKeyResult> GetApiKeyOrNullAsync(bool invalidateCache = false)
        {
            if (!AuthService.IsLoggedIn())
            {
                return(null);
            }

            if (invalidateCache)
            {
                _apiKeyResult = null;
            }

            if (_apiKeyResult != null)
            {
                return(_apiKeyResult);
            }

            var url    = $"{CliUrls.WwwAbpIo}api/license/api-key";
            var client = _cliHttpClientFactory.CreateClient();

            using (var response = await client.GetHttpResponseMessageWithRetryAsync(url, CancellationTokenProvider.Token, _logger))
            {
                if (!response.IsSuccessStatusCode)
                {
                    throw new Exception($"ERROR: Remote server returns '{response.StatusCode}'");
                }

                await RemoteServiceExceptionHandler.EnsureSuccessfulHttpResponseAsync(response);

                var responseContent = await response.Content.ReadAsStringAsync();

                return(JsonSerializer.Deserialize <DeveloperApiKeyResult>(responseContent));
            }
        }
Beispiel #2
0
        public async Task <DeveloperApiKeyResult> GetApiKeyOrNullAsync(bool invalidateCache = false)
        {
            if (invalidateCache)
            {
                _apiKeyResult = null;
            }

            if (_apiKeyResult != null)
            {
                return(_apiKeyResult);
            }

            var url = $"{CliUrls.WwwAbpIo}api/license/api-key";

            using (var client = new CliHttpClient())
            {
                var response = await HttpPolicyExtensions
                               .HandleTransientHttpError()
                               .OrResult(msg => !msg.IsSuccessStatusCode)
                               .WaitAndRetryAsync(new[]
                {
                    TimeSpan.FromSeconds(1),
                    TimeSpan.FromSeconds(3),
                    TimeSpan.FromSeconds(7)
                },
                                                  (responseMessage, timeSpan, retryCount, context) =>
                {
                    if (responseMessage.Exception != null)
                    {
                        _logger.LogWarning(
                            $"{retryCount}. request attempt failed to {url} with an error: \"{responseMessage.Exception.Message}\". " +
                            $"Waiting {timeSpan.TotalSeconds} secs for the next try...");
                    }
                    else if (responseMessage.Result != null)
                    {
                        _logger.LogWarning(
                            $"{retryCount}. request attempt failed {url} with {(int)responseMessage.Result.StatusCode}-{responseMessage.Result.ReasonPhrase}. " +
                            $"Waiting {timeSpan.TotalSeconds} secs for the next try...");
                    }
                })
                               .ExecuteAsync(async() => await client.GetAsync(url).ConfigureAwait(false)).ConfigureAwait(false);

                if (!response.IsSuccessStatusCode)
                {
                    throw new Exception($"ERROR: Remote server returns '{response.StatusCode}'");
                }

                await RemoteServiceExceptionHandler.EnsureSuccessfulHttpResponseAsync(response).ConfigureAwait(false);

                var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                var apiKeyResult = JsonSerializer.Deserialize <DeveloperApiKeyResult>(responseContent);

                if (apiKeyResult == null ||
                    string.IsNullOrEmpty(apiKeyResult.ApiKey))
                {
                    _logger.LogError("Couldn't retrieve your NuGet API key!");
                    _logger.LogWarning(File.Exists(CliPaths.AccessToken)
                        ? "Make sure you have an active session and license on commercial.abp.io. To re-sign in you can use the CLI command \"abp login <username>\"."
                        : "You are not signed in to commercial.abp.io. Use the CLI command \"abp login <username>\" to sign in.");

                    return(null);
                }

                return(apiKeyResult);
            }
        }
Beispiel #3
0
        public async Task <DeveloperApiKeyResult> GetApiKeyOrNullAsync(bool invalidateCache = false)
        {
            if (!AuthService.IsLoggedIn())
            {
                return(null);
            }

            if (invalidateCache)
            {
                _apiKeyResult = null;
            }

            if (_apiKeyResult != null)
            {
                return(_apiKeyResult);
            }

            var url = $"{CliUrls.WwwAbpIo}api/license/api-key";

            using (var client = new CliHttpClient())
            {
                var response = await HttpPolicyExtensions
                               .HandleTransientHttpError()
                               .OrResult(msg => !msg.IsSuccessStatusCode)
                               .WaitAndRetryAsync(new[]
                {
                    TimeSpan.FromSeconds(1),
                    TimeSpan.FromSeconds(3),
                    TimeSpan.FromSeconds(7)
                },
                                                  (responseMessage, timeSpan, retryCount, context) =>
                {
                    if (responseMessage.Exception != null)
                    {
                        _logger.LogWarning(
                            $"{retryCount}. request attempt failed to {url} with an error: \"{responseMessage.Exception.Message}\". " +
                            $"Waiting {timeSpan.TotalSeconds} secs for the next try...");
                    }
                    else if (responseMessage.Result != null)
                    {
                        _logger.LogWarning(
                            $"{retryCount}. request attempt failed {url} with {(int)responseMessage.Result.StatusCode}-{responseMessage.Result.ReasonPhrase}. " +
                            $"Waiting {timeSpan.TotalSeconds} secs for the next try...");
                    }
                })
                               .ExecuteAsync(async() => await client.GetAsync(url).ConfigureAwait(false)).ConfigureAwait(false);

                if (!response.IsSuccessStatusCode)
                {
                    throw new Exception($"ERROR: Remote server returns '{response.StatusCode}'");
                }

                await RemoteServiceExceptionHandler.EnsureSuccessfulHttpResponseAsync(response).ConfigureAwait(false);

                var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                var apiKeyResult = JsonSerializer.Deserialize <DeveloperApiKeyResult>(responseContent);

                return(apiKeyResult);
            }
        }