Skip to content

Commit

Permalink
Added: Ability to fetch NuGet Package Details
Browse files Browse the repository at this point in the history
  • Loading branch information
Sewer56 committed Aug 23, 2022
1 parent 65c26e1 commit 27a8ee4
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -66,4 +67,14 @@ public async Task<long> GetDownloadFileSizeAsync(NuGetVersion version, ReleaseMe
var identity = new PackageIdentity(_resolverSettings.PackageId, version);
return (await _resolverSettings.NugetRepository!.GetDownloadUrlUnsafeAsync(identity, token))!;
}

/// <summary>
/// Gets the full package details for a given version of the package.
/// </summary>
/// <param name="version">Version to get the details for.</param>
/// <param name="token">Token to cancel the fetch operation.</param>
public async Task<IPackageSearchMetadata?> GetPackageDetailsAsync(NuGetVersion version, CancellationToken token = default)
{
return await _resolverSettings.NugetRepository!.GetPackageDetailsAsync(new PackageIdentity(_resolverSettings.PackageId, version), token);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,22 @@ public async Task<IEnumerable<IPackageSearchMetadata>> GetPackageDetailsAsync(st
catch (Exception) { return new IPackageSearchMetadata[0]; }
}

/// <summary>
/// Retrieves the details of an individual package.
/// </summary>
/// <param name="identity">Uniquely identifies the package.</param>
/// <param name="token">A cancellation token to allow cancellation of the task.</param>
/// <returns>Return contains an array of versions for this package.</returns>
public async Task<IPackageSearchMetadata?> 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; }
}

/// <summary>
/// [WARNING: REFLECTION]
/// Uses reflection to get a URL to the package download.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}

0 comments on commit 27a8ee4

Please sign in to comment.