Skip to content

Commit 44acf8b

Browse files
authored
[Client SDK] Add method to check for package existence (loic-sharma#351)
1 parent eae2f27 commit 44acf8b

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

samples/Sample02_Search.cs

+19
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,31 @@
22
using System.Collections.Generic;
33
using System.Threading.Tasks;
44
using BaGet.Protocol.Models;
5+
using NuGet.Versioning;
56
using Xunit;
67

78
namespace BaGet.Protocol.Samples.Tests
89
{
910
public class Sample02_Search
1011
{
12+
[Fact]
13+
public async Task Exists()
14+
{
15+
// Check if a package exists.
16+
NuGetClient client = new NuGetClient("https://api.nuget.org/v3/index.json");
17+
18+
if (!await client.ExistsAsync("newtonsoft.json"))
19+
{
20+
Console.WriteLine("Package 'newtonsoft.json' does not exist!");
21+
}
22+
23+
var packageVersion = NuGetVersion.Parse("12.0.1");
24+
if (!await client.ExistsAsync("newtonsoft.json", packageVersion))
25+
{
26+
Console.WriteLine("Package 'newtonsoft.json' version '12.0.1' does not exist!");
27+
}
28+
}
29+
1130
[Fact]
1231
public async Task Search()
1332
{

src/BaGet.Protocol/NuGetClient.cs

+42-1
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,54 @@ public NuGetClient(NuGetClientFactory clientFactory)
5353
_clientFactory = clientFactory ?? throw new ArgumentNullException(nameof(clientFactory));
5454
}
5555

56+
/// <summary>
57+
/// Check if a package exists.
58+
/// </summary>
59+
/// <param name="packageId">The package ID.</param>
60+
/// <param name="cancellationToken">A token to cancel the task.</param>
61+
/// <returns>Whether the package exists.</returns>
62+
public virtual async Task<bool> ExistsAsync(
63+
string packageId,
64+
CancellationToken cancellationToken = default)
65+
{
66+
var client = await _clientFactory.CreatePackageContentClientAsync(cancellationToken);
67+
var versions = await client.GetPackageVersionsOrNullAsync(packageId, cancellationToken);
68+
69+
return (versions != null && versions.Versions.Any());
70+
}
71+
72+
/// <summary>
73+
/// Check if a package exists.
74+
/// </summary>
75+
/// <param name="packageId">The package ID.</param>
76+
/// <param name="packageVersion">The package version.</param>
77+
/// <param name="cancellationToken">A token to cancel the task.</param>
78+
/// <returns>Whether the package exists.</returns>
79+
public virtual async Task<bool> ExistsAsync(
80+
string packageId,
81+
NuGetVersion packageVersion,
82+
CancellationToken cancellationToken = default)
83+
{
84+
var client = await _clientFactory.CreatePackageContentClientAsync(cancellationToken);
85+
var versions = await client.GetPackageVersionsOrNullAsync(packageId, cancellationToken);
86+
87+
if (versions == null)
88+
{
89+
return false;
90+
}
91+
92+
return versions
93+
.ParseVersions()
94+
.Any(v => v == packageVersion);
95+
}
96+
5697
/// <summary>
5798
/// Download a package (.nupkg), or throws if the package does not exist.
5899
/// </summary>
59100
/// <param name="packageId">The package ID.</param>
60101
/// <param name="packageVersion">The package version.</param>
61102
/// <param name="cancellationToken">A token to cancel the task.</param>
62-
/// <returns>The package's content stream. The stream may not be seekable.</returns>
103+
/// <returns>The package's content stream. The stream may be unseekable and may be unbuffered.</returns>
63104
/// <exception cref="PackageNotFoundException">
64105
/// The package could not be found.
65106
/// </exception>

0 commit comments

Comments
 (0)