From f0e275a0c854977c5ebdfb30f2013d45f0824305 Mon Sep 17 00:00:00 2001 From: Alper Ebicoglu Date: Fri, 20 Dec 2019 11:59:50 +0300 Subject: [PATCH] add Poly to Abp.Cli.Core and use it GetApiKeyOrNullAsync method. --- .../Volo.Abp.Cli.Core.csproj | 2 + .../Volo/Abp/Cli/Commands/SuiteCommand.cs | 16 +++++++- .../Abp/Cli/Licensing/AbpIoApiKeyService.cs | 40 ++++++++++++++++--- 3 files changed, 51 insertions(+), 7 deletions(-) diff --git a/framework/src/Volo.Abp.Cli.Core/Volo.Abp.Cli.Core.csproj b/framework/src/Volo.Abp.Cli.Core/Volo.Abp.Cli.Core.csproj index 46f3d32d6a..37a9f54748 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo.Abp.Cli.Core.csproj +++ b/framework/src/Volo.Abp.Cli.Core/Volo.Abp.Cli.Core.csproj @@ -19,6 +19,8 @@ + + diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/SuiteCommand.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/SuiteCommand.cs index d2913a0090..fe29b281a7 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/SuiteCommand.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/SuiteCommand.cs @@ -60,6 +60,12 @@ namespace Volo.Abp.Cli.Commands private async Task InstallSuiteAsync() { var nugetIndexUrl = await GetNuGetIndexUrlAsync(); + + if (nugetIndexUrl == null) + { + return; + } + var result = CmdHelper.RunCmd("dotnet tool install " + SuitePackageName + " --add-source " + nugetIndexUrl + " -g"); if (result == 0) @@ -72,6 +78,12 @@ namespace Volo.Abp.Cli.Commands private async Task UpdateSuiteAsync() { var nugetIndexUrl = await GetNuGetIndexUrlAsync(); + + if (nugetIndexUrl == null) + { + return; + } + CmdHelper.RunCmd("dotnet tool update " + SuitePackageName + " --add-source " + nugetIndexUrl + " -g"); } @@ -101,8 +113,8 @@ namespace Volo.Abp.Cli.Commands private async Task GetNuGetIndexUrlAsync() { var apiKeyResult = await _apiKeyService.GetApiKeyOrNullAsync(); - if (apiKeyResult == null || - string.IsNullOrEmpty(apiKeyResult.ApiKey)) + + if (apiKeyResult == null || string.IsNullOrEmpty(apiKeyResult.ApiKey)) { Logger.LogError("Couldn't retrieve your NuGet API key!"); Logger.LogWarning(File.Exists(CliPaths.AccessToken) diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Licensing/AbpIoApiKeyService.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Licensing/AbpIoApiKeyService.cs index c8f221b73c..1b9539d012 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Licensing/AbpIoApiKeyService.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Licensing/AbpIoApiKeyService.cs @@ -1,5 +1,12 @@ using System; +using System.Collections.Generic; +using System.Net; +using System.Net.Http; using System.Threading.Tasks; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; +using Polly; +using Polly.Extensions.Http; using Volo.Abp.Cli.Http; using Volo.Abp.Cli.ProjectBuilding; using Volo.Abp.DependencyInjection; @@ -10,22 +17,45 @@ namespace Volo.Abp.Cli.Licensing public class AbpIoApiKeyService : IApiKeyService, ITransientDependency { protected IJsonSerializer JsonSerializer { get; } - protected IRemoteServiceExceptionHandler RemoteServiceExceptionHandler { get; } + private readonly ILogger _logger; - public AbpIoApiKeyService(IJsonSerializer jsonSerializer, IRemoteServiceExceptionHandler remoteServiceExceptionHandler) + public AbpIoApiKeyService(IJsonSerializer jsonSerializer, IRemoteServiceExceptionHandler remoteServiceExceptionHandler, ILogger logger) { JsonSerializer = jsonSerializer; RemoteServiceExceptionHandler = remoteServiceExceptionHandler; + _logger = logger; } public async Task GetApiKeyOrNullAsync() { using (var client = new CliHttpClient()) { - var url = $"{CliUrls.WwwAbpIo}api/license/api-key"; - - var response = await client.GetAsync(url); + 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 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 with {responseMessage.Result.StatusCode}-{responseMessage.Result.ReasonPhrase}. " + + $"Waiting {timeSpan.TotalSeconds} secs for the next try..."); + } + }) + .ExecuteAsync(async () => await client.GetAsync($"{CliUrls.WwwAbpIo}api/license/api-key")); if (!response.IsSuccessStatusCode) {