Browse Source

Refactor cli NuGetService

pull/10363/head
Yunus Emre Kalkan 5 years ago
parent
commit
e6dca453a6
  1. 59
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/NuGet/NuGetService.cs

59
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/NuGet/NuGetService.cs

@ -1,3 +1,4 @@
using System;
using Newtonsoft.Json; using Newtonsoft.Json;
using NuGet.Versioning; using NuGet.Versioning;
using System.Collections.Generic; using System.Collections.Generic;
@ -24,7 +25,6 @@ namespace Volo.Abp.Cli.NuGet
protected IRemoteServiceExceptionHandler RemoteServiceExceptionHandler { get; } protected IRemoteServiceExceptionHandler RemoteServiceExceptionHandler { get; }
private readonly IApiKeyService _apiKeyService; private readonly IApiKeyService _apiKeyService;
private readonly CliHttpClientFactory _cliHttpClientFactory; private readonly CliHttpClientFactory _cliHttpClientFactory;
private List<string> _proPackageList;
private DeveloperApiKeyResult _apiKeyResult; private DeveloperApiKeyResult _apiKeyResult;
public NuGetService( public NuGetService(
@ -77,36 +77,38 @@ namespace Volo.Abp.Cli.NuGet
public async Task<List<string>> GetPackageVersionListAsync(string packageId, bool includeNightly = false, public async Task<List<string>> GetPackageVersionListAsync(string packageId, bool includeNightly = false,
bool includeReleaseCandidates = false) bool includeReleaseCandidates = false)
{ {
if (AuthService.IsLoggedIn())
{
_proPackageList ??= await GetProPackageListAsync();
}
string url;
if (includeNightly) if (includeNightly)
{ {
url = $"https://www.myget.org/F/abp-nightly/api/v3/flatcontainer/{packageId.ToLowerInvariant()}/index.json"; var includeNightlyUrl = $"https://www.myget.org/F/abp-nightly/api/v3/flatcontainer/{packageId.ToLowerInvariant()}/index.json";
} return await GetPackageVersionListFromUrlAsync(includeNightlyUrl);
else if (_proPackageList?.Contains(packageId) ?? false)
{
url = await GetNuGetUrlForCommercialPackage(packageId);
}
else
{
url = $"https://api.nuget.org/v3-flatcontainer/{packageId.ToLowerInvariant()}/index.json";
} }
var client = _cliHttpClientFactory.CreateClient(needsAuthentication: false); var nugetUrl = $"https://api.nuget.org/v3-flatcontainer/{packageId.ToLowerInvariant()}/index.json";
var commercialUrl = await GetNuGetUrlForCommercialPackage(packageId);
using (var responseMessage = await client.GetHttpResponseMessageWithRetryAsync( return (await GetPackageVersionListFromUrlAsync(nugetUrl)) ?? (await GetPackageVersionListFromUrlAsync(commercialUrl));
url, }
cancellationToken: CancellationTokenProvider.Token,
logger: Logger private async Task<List<string>> GetPackageVersionListFromUrlAsync(string url)
)) {
try
{
var client = _cliHttpClientFactory.CreateClient(needsAuthentication: false);
using (var responseMessage = await client.GetHttpResponseMessageWithRetryAsync(
url,
cancellationToken: CancellationTokenProvider.Token,
logger: Logger
))
{
await RemoteServiceExceptionHandler.EnsureSuccessfulHttpResponseAsync(responseMessage);
var responseContent = await responseMessage.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<NuGetVersionResultDto>(responseContent).Versions;
}
}
catch (Exception)
{ {
await RemoteServiceExceptionHandler.EnsureSuccessfulHttpResponseAsync(responseMessage); return null;
var responseContent = await responseMessage.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<NuGetVersionResultDto>(responseContent).Versions;
} }
} }
@ -115,6 +117,11 @@ namespace Volo.Abp.Cli.NuGet
if (_apiKeyResult == null) if (_apiKeyResult == null)
{ {
_apiKeyResult = await _apiKeyService.GetApiKeyOrNullAsync(); _apiKeyResult = await _apiKeyService.GetApiKeyOrNullAsync();
if (_apiKeyResult == null)
{
return null;
}
} }
return CliUrls.GetNuGetPackageInfoUrl(_apiKeyResult.ApiKey, packageId); return CliUrls.GetNuGetPackageInfoUrl(_apiKeyResult.ApiKey, packageId);
@ -155,4 +162,4 @@ namespace Volo.Abp.Cli.NuGet
public List<string> Versions { get; set; } public List<string> Versions { get; set; }
} }
} }
} }

Loading…
Cancel
Save