From 27a8ee4828423c4d973ff50fb664027199cf7b87 Mon Sep 17 00:00:00 2001 From: Sewer 56 Date: Tue, 23 Aug 2022 10:14:44 +0100 Subject: [PATCH] Added: Ability to fetch NuGet Package Details --- .../NuGetUpdateResolver.cs | 11 +++++++++++ .../Utilities/NugetRepository.cs | 16 ++++++++++++++++ .../Resolvers/NuGetResolverTests.cs | 18 ++++++++++++++++++ 3 files changed, 45 insertions(+) diff --git a/Sewer56.Update/Resolvers/Sewer56.Update.Resolvers.NuGet/NuGetUpdateResolver.cs b/Sewer56.Update/Resolvers/Sewer56.Update.Resolvers.NuGet/NuGetUpdateResolver.cs index b8fbfb2..7709525 100644 --- a/Sewer56.Update/Resolvers/Sewer56.Update.Resolvers.NuGet/NuGetUpdateResolver.cs +++ b/Sewer56.Update/Resolvers/Sewer56.Update.Resolvers.NuGet/NuGetUpdateResolver.cs @@ -6,6 +6,7 @@ using System.Threading; using System.Threading.Tasks; using NuGet.Packaging.Core; +using NuGet.Protocol.Core.Types; using NuGet.Versioning; using Sewer56.Update.Interfaces; using Sewer56.Update.Interfaces.Extensions; @@ -66,4 +67,14 @@ public async Task GetDownloadFileSizeAsync(NuGetVersion version, ReleaseMe var identity = new PackageIdentity(_resolverSettings.PackageId, version); return (await _resolverSettings.NugetRepository!.GetDownloadUrlUnsafeAsync(identity, token))!; } + + /// + /// Gets the full package details for a given version of the package. + /// + /// Version to get the details for. + /// Token to cancel the fetch operation. + public async Task GetPackageDetailsAsync(NuGetVersion version, CancellationToken token = default) + { + return await _resolverSettings.NugetRepository!.GetPackageDetailsAsync(new PackageIdentity(_resolverSettings.PackageId, version), token); + } } \ No newline at end of file diff --git a/Sewer56.Update/Resolvers/Sewer56.Update.Resolvers.NuGet/Utilities/NugetRepository.cs b/Sewer56.Update/Resolvers/Sewer56.Update.Resolvers.NuGet/Utilities/NugetRepository.cs index 39ed2bf..0d173ce 100644 --- a/Sewer56.Update/Resolvers/Sewer56.Update.Resolvers.NuGet/Utilities/NugetRepository.cs +++ b/Sewer56.Update/Resolvers/Sewer56.Update.Resolvers.NuGet/Utilities/NugetRepository.cs @@ -78,6 +78,22 @@ public async Task> GetPackageDetailsAsync(st catch (Exception) { return new IPackageSearchMetadata[0]; } } + /// + /// Retrieves the details of an individual package. + /// + /// Uniquely identifies the package. + /// A cancellation token to allow cancellation of the task. + /// Return contains an array of versions for this package. + public async Task GetPackageDetailsAsync(PackageIdentity identity, CancellationToken token = default) + { + try + { + var metadataResource = await _packageMetadataResource; + return await metadataResource.GetMetadataAsync(identity, _sourceCacheContext, _nullLogger, token).ConfigureAwait(false); + } + catch (Exception) { return null; } + } + /// /// [WARNING: REFLECTION] /// Uses reflection to get a URL to the package download. diff --git a/Sewer56.Update/Sewer56.Update.Tests/Resolvers/NuGetResolverTests.cs b/Sewer56.Update/Sewer56.Update.Tests/Resolvers/NuGetResolverTests.cs index 4a8feff..3259496 100644 --- a/Sewer56.Update/Sewer56.Update.Tests/Resolvers/NuGetResolverTests.cs +++ b/Sewer56.Update/Sewer56.Update.Tests/Resolvers/NuGetResolverTests.cs @@ -104,4 +104,22 @@ public async Task GetPackageVersionsAsync_CanGetDownloadUrl() Assert.True(!string.IsNullOrEmpty(downloadUrl)); } + [Fact] + public async Task GetPackageVersionsAsync_CanGetPackageDetails() + { + var commonResolverSettings = new CommonPackageResolverSettings() + { + AllowPrereleases = true + }; + + // Act + var resolver = new NuGetUpdateResolver(ResolverConfiguration, commonResolverSettings); + var versions = await resolver.GetPackageVersionsAsync(); + + var packageDetails = await resolver.GetPackageDetailsAsync(versions[0]); + + // Assert + Assert.NotNull(packageDetails); + } + } \ No newline at end of file