Skip to content

Commit bc44e11

Browse files
authored
[Client SDK] Improve on-boarding experience (loic-sharma#312)
Improve the on-boarding experience: * Add samples * Add factory to simplify client creation * Reduce public types in `BaGet.Protocol` Addresses loic-sharma#305 and loic-sharma#311
1 parent 213c518 commit bc44e11

File tree

77 files changed

+1441
-574
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+1441
-574
lines changed

BaGet.sln

+14-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 15
4-
VisualStudioVersion = 15.0.27130.2027
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.29104.9
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BaGet", "src\BaGet\BaGet.csproj", "{284366CB-C68F-473E-908A-50A382616AE0}"
77
EndProject
@@ -23,7 +23,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BaGet.Protocol", "src\BaGet
2323
EndProject
2424
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BaGet.Protocol.Tests", "tests\BaGet.Protocol.Tests\BaGet.Protocol.Tests.csproj", "{AC764A9A-9EAF-422B-9223-D3290C3CFD79}"
2525
EndProject
26-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BaGet.AWS", "src\BaGet.AWS\BaGet.AWS.csproj", "{D067D82E-D515-44D1-A832-C79F29418DFC}"
26+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BaGet.Aws", "src\BaGet.Aws\BaGet.Aws.csproj", "{D067D82E-D515-44D1-A832-C79F29418DFC}"
2727
EndProject
2828
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{0B44364D-952B-497A-82E0-C9AAE94E0369}"
2929
ProjectSection(SolutionItems) = preProject
@@ -38,9 +38,13 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BaGet.Database.SqlServer",
3838
EndProject
3939
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BaGet.Database.MySql", "src\BaGet.Database.MySql\BaGet.Database.MySql.csproj", "{A4375529-E855-4D46-AA4F-B3FE630C3DE1}"
4040
EndProject
41-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BaGet.Database.PostgreSql", "src\BaGet.Database.PostgreSql\BaGet.Database.PostgreSql.csproj", "{F48F201A-4DEE-4D5B-9C0B-59490FE942FA}"
41+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BaGet.Database.PostgreSql", "src\BaGet.Database.PostgreSql\BaGet.Database.PostgreSql.csproj", "{F48F201A-4DEE-4D5B-9C0B-59490FE942FA}"
4242
EndProject
43-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BaGet.GCP", "src\BaGet.GCP\BaGet.GCP.csproj", "{D7D60BA0-FF7F-4B37-815C-74D487C5176E}"
43+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BaGet.Gcp", "src\BaGet.Gcp\BaGet.Gcp.csproj", "{D7D60BA0-FF7F-4B37-815C-74D487C5176E}"
44+
EndProject
45+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{DDEC0736-8169-4834-815E-B78E7CE612A4}"
46+
EndProject
47+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BaGet.Protocol.Samples.Tests", "samples\BaGet.Protocol.Samples.Tests.csproj", "{16B0D424-BB2F-4C0C-90B0-4F7955326ADF}"
4448
EndProject
4549
Global
4650
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -108,6 +112,10 @@ Global
108112
{D7D60BA0-FF7F-4B37-815C-74D487C5176E}.Debug|Any CPU.Build.0 = Debug|Any CPU
109113
{D7D60BA0-FF7F-4B37-815C-74D487C5176E}.Release|Any CPU.ActiveCfg = Release|Any CPU
110114
{D7D60BA0-FF7F-4B37-815C-74D487C5176E}.Release|Any CPU.Build.0 = Release|Any CPU
115+
{16B0D424-BB2F-4C0C-90B0-4F7955326ADF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
116+
{16B0D424-BB2F-4C0C-90B0-4F7955326ADF}.Debug|Any CPU.Build.0 = Debug|Any CPU
117+
{16B0D424-BB2F-4C0C-90B0-4F7955326ADF}.Release|Any CPU.ActiveCfg = Release|Any CPU
118+
{16B0D424-BB2F-4C0C-90B0-4F7955326ADF}.Release|Any CPU.Build.0 = Release|Any CPU
111119
EndGlobalSection
112120
GlobalSection(SolutionProperties) = preSolution
113121
HideSolutionNode = FALSE
@@ -128,6 +136,7 @@ Global
128136
{A4375529-E855-4D46-AA4F-B3FE630C3DE1} = {26A0B557-53FB-4B9A-94C4-BCCF1BDCB0CC}
129137
{F48F201A-4DEE-4D5B-9C0B-59490FE942FA} = {26A0B557-53FB-4B9A-94C4-BCCF1BDCB0CC}
130138
{D7D60BA0-FF7F-4B37-815C-74D487C5176E} = {26A0B557-53FB-4B9A-94C4-BCCF1BDCB0CC}
139+
{16B0D424-BB2F-4C0C-90B0-4F7955326ADF} = {DDEC0736-8169-4834-815E-B78E7CE612A4}
131140
EndGlobalSection
132141
GlobalSection(ExtensibilityGlobals) = postSolution
133142
SolutionGuid = {1423C027-2C90-417F-8629-2A4CF107C055}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp2.2</TargetFramework>
5+
6+
<IsPackable>false</IsPackable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
11+
<PackageReference Include="Moq" Version="4.10.0" />
12+
<PackageReference Include="xunit" Version="2.4.1" />
13+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
14+
<PrivateAssets>all</PrivateAssets>
15+
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
16+
</PackageReference>
17+
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
18+
</ItemGroup>
19+
20+
<ItemGroup>
21+
<ProjectReference Include="..\src\BaGet.Protocol\BaGet.Protocol.csproj" />
22+
</ItemGroup>
23+
24+
</Project>

samples/Sample01_Download.cs

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using NuGet.Versioning;
4+
using Xunit;
5+
6+
namespace BaGet.Protocol.Samples.Tests
7+
{
8+
public class Sample01_Download
9+
{
10+
[Fact]
11+
public async Task DownloadPackage()
12+
{
13+
// Downloads a package file (.nupkg)
14+
var client = new NuGetClient("https://api.nuget.org/v3/index.json");
15+
16+
var packageId = "Newtonsoft.Json";
17+
var packageVersion = new NuGetVersion("12.0.1");
18+
19+
using (var packageStream = await client.GetPackageStreamAsync(packageId, packageVersion))
20+
{
21+
Console.WriteLine($"Downloaded package {packageId} {packageVersion}");
22+
}
23+
}
24+
25+
[Fact]
26+
public async Task DownloadPackageManifest()
27+
{
28+
// Downloads a package manifest (.nuspec)
29+
var client = new NuGetClient("https://api.nuget.org/v3/index.json");
30+
31+
var packageId = "Newtonsoft.Json";
32+
var packageVersion = new NuGetVersion("12.0.1");
33+
34+
using (var manifestStream = await client.GetPackageManifestStreamAsync(packageId, packageVersion))
35+
{
36+
Console.WriteLine($"Downloaded package {packageId} {packageVersion}'s nuspec");
37+
}
38+
}
39+
}
40+
}

samples/Sample02_Search.cs

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Xunit;
4+
5+
namespace BaGet.Protocol.Samples.Tests
6+
{
7+
public class Sample02_Search
8+
{
9+
[Fact]
10+
public async Task Search()
11+
{
12+
// Search for packages that are relevant to "json".
13+
var client = new NuGetClient("https://api.nuget.org/v3/index.json");
14+
var response = await client.SearchAsync("json");
15+
16+
Console.WriteLine($"Found {response.TotalHits} results");
17+
18+
var index = 1;
19+
foreach (var searchResult in response.Data)
20+
{
21+
Console.WriteLine($"Result #{index}");
22+
Console.WriteLine($"Package id: {searchResult.PackageId}");
23+
Console.WriteLine($"Package version: {searchResult.Version}");
24+
Console.WriteLine($"Package downloads: {searchResult.TotalDownloads}");
25+
Console.WriteLine($"Package versions: {searchResult.Versions.Count}");
26+
Console.WriteLine();
27+
28+
index++;
29+
}
30+
}
31+
32+
[Fact]
33+
public async Task Autocomplete()
34+
{
35+
// Search for packages whose names' start with "Newt".
36+
var client = new NuGetClient("https://api.nuget.org/v3/index.json");
37+
var response = await client.AutocompleteAsync("Newt");
38+
39+
Console.WriteLine($"Found {response.TotalHits} results");
40+
41+
var index = 1;
42+
foreach (var searchResult in response.Data)
43+
{
44+
Console.WriteLine($"Result #{index}: '{searchResult}'");
45+
index++;
46+
}
47+
}
48+
}
49+
}

samples/Sample03_Metadata.cs

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using System;
2+
using System.Linq;
3+
using System.Threading;
4+
using System.Threading.Tasks;
5+
using NuGet.Versioning;
6+
using Xunit;
7+
8+
namespace BaGet.Protocol.Samples.Tests
9+
{
10+
public class Sample03_Metadata
11+
{
12+
[Fact]
13+
public async Task GetAllPackageMetadata()
14+
{
15+
// Find the metadata for all versions of a package.
16+
var client = new NuGetClient("https://api.nuget.org/v3/index.json");
17+
18+
var items = await client.GetPackageMetadataAsync("Newtonsoft.Json");
19+
if (!items.Any())
20+
{
21+
Console.WriteLine($"Package 'Newtonsoft.Json' does not exist");
22+
return;
23+
}
24+
25+
// There is an item for each version of the package.
26+
foreach (var item in items)
27+
{
28+
Console.WriteLine($"Version: {item.PackageMetadata.Version}");
29+
Console.WriteLine($"Listed: {item.PackageMetadata.Listed}");
30+
Console.WriteLine($"Tags: {item.PackageMetadata.Tags}");
31+
Console.WriteLine($"Description: {item.PackageMetadata.Description}");
32+
}
33+
}
34+
35+
[Fact]
36+
public async Task GetPackageMetadata()
37+
{
38+
// Find the metadata for a single version of a package.
39+
var client = new NuGetClient("https://api.nuget.org/v3/index.json");
40+
41+
var packageId = "Newtonsoft.Json";
42+
var packageVersion = new NuGetVersion("12.0.1");
43+
44+
var registrationItem = await client.GetPackageMetadataAsync(packageId, packageVersion);
45+
46+
Console.WriteLine($"Listed: {registrationItem.PackageMetadata.Listed}");
47+
Console.WriteLine($"Tags: {registrationItem.PackageMetadata.Tags}");
48+
Console.WriteLine($"Description: {registrationItem.PackageMetadata.Description}");
49+
}
50+
51+
[Fact]
52+
public async Task ListVersions()
53+
{
54+
// Find all versions of a package (including unlisted versions).
55+
var client = new NuGetClient("https://api.nuget.org/v3/index.json");
56+
57+
var packageVersions = await client.ListPackageVersions("Newtonsoft.Json", includeUnlisted: true);
58+
if (!packageVersions.Any())
59+
{
60+
Console.WriteLine($"Package 'Newtonsoft.Json' does not exist");
61+
return;
62+
}
63+
64+
Console.WriteLine($"Found {packageVersions.Count} versions");
65+
}
66+
}
67+
}

src/BaGet.AWS/BaGet.AWS.csproj src/BaGet.Aws/BaGet.Aws.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<PropertyGroup>
44
<TargetFrameworks>netstandard2.0;net461</TargetFrameworks>
55

6+
<PackageTags>NuGet;Amazon;Cloud</PackageTags>
67
<Description>The libraries to host BaGet on AWS.</Description>
78
</PropertyGroup>
89

src/BaGet.AWS/Configuration/S3StorageOptions.cs src/BaGet.Aws/Configuration/S3StorageOptions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System.ComponentModel.DataAnnotations;
22
using BaGet.Core.Validation;
33

4-
namespace BaGet.AWS.Configuration
4+
namespace BaGet.Aws.Configuration
55
{
66
public class S3StorageOptions
77
{

src/BaGet.AWS/Extensions/ServiceCollectionExtensions.cs src/BaGet.Aws/Extensions/ServiceCollectionExtensions.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
using Amazon;
33
using Amazon.Runtime;
44
using Amazon.S3;
5-
using BaGet.AWS.Configuration;
6-
using BaGet.AWS.Helpers;
5+
using BaGet.Aws.Configuration;
6+
using BaGet.Aws.Helpers;
77
using Microsoft.Extensions.DependencyInjection;
88
using Microsoft.Extensions.Options;
99

10-
namespace BaGet.AWS.Extensions
10+
namespace BaGet.Aws.Extensions
1111
{
1212
public static class ServiceCollectionExtensions
1313
{

src/BaGet.AWS/Helpers/AwsIamHelper.cs src/BaGet.Aws/Helpers/AwsIamHelper.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using System.Threading.Tasks;
33
using Amazon.Runtime;
44

5-
namespace BaGet.AWS.Helpers
5+
namespace BaGet.Aws.Helpers
66
{
77
public static class AwsIamHelper
88
{

src/BaGet.AWS/S3StorageService.cs src/BaGet.Aws/S3StorageService.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
using System.Threading.Tasks;
55
using Amazon.S3;
66
using Amazon.S3.Model;
7-
using BaGet.AWS.Configuration;
7+
using BaGet.Aws.Configuration;
88
using BaGet.Core.Storage;
99
using Microsoft.Extensions.Options;
1010

11-
namespace BaGet.AWS
11+
namespace BaGet.Aws
1212
{
1313
public class S3StorageService : IStorageService
1414
{

src/BaGet.Azure/BaGet.Azure.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<PropertyGroup>
44
<TargetFrameworks>netstandard2.0;net461</TargetFrameworks>
55

6+
<PackageTags>NuGet;Azure;Cloud</PackageTags>
67
<Description>The libraries to host BaGet on Azure.</Description>
78
</PropertyGroup>
89

src/BaGet.Azure/Search/AzureSearchService.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using System.Threading;
55
using System.Threading.Tasks;
6+
using BaGet.Core;
67
using BaGet.Core.Entities;
78
using BaGet.Core.Indexing;
89
using BaGet.Core.Search;
@@ -16,17 +17,17 @@ namespace BaGet.Azure.Search
1617
using QueryType = Microsoft.Azure.Search.Models.QueryType;
1718
using SearchParameters = Microsoft.Azure.Search.Models.SearchParameters;
1819

19-
public class AzureSearchService : IBaGetSearchService
20+
public class AzureSearchService : IBaGetSearchResource
2021
{
2122
private readonly BatchIndexer _indexer;
2223
private readonly SearchIndexClient _searchClient;
23-
private readonly IBaGetUrlGenerator _url;
24+
private readonly IUrlGenerator _url;
2425
private readonly IFrameworkCompatibilityService _frameworks;
2526

2627
public AzureSearchService(
2728
BatchIndexer indexer,
2829
SearchIndexClient searchClient,
29-
IBaGetUrlGenerator url,
30+
IUrlGenerator url,
3031
IFrameworkCompatibilityService frameworks)
3132
{
3233
_indexer = indexer ?? throw new ArgumentNullException(nameof(indexer));

src/BaGet.Core.Server/BaGetUrlGenerator.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
using System;
2-
using BaGet.Core.ServiceIndex;
2+
using BaGet.Core;
33
using Microsoft.AspNetCore.Http;
44
using Microsoft.AspNetCore.Routing;
55
using NuGet.Versioning;
66

77
namespace BaGet
88
{
99
// TODO: This should validate the "Host" header against known valid values
10-
public class BaGetUrlGenerator : IBaGetUrlGenerator
10+
public class BaGetUrlGenerator : IUrlGenerator
1111
{
1212
private readonly IHttpContextAccessor _httpContextAccessor;
1313
private readonly LinkGenerator _linkGenerator;

src/BaGet.Core.Server/Controllers/SearchController.cs

+2-4
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@ namespace BaGet.Controllers
99
{
1010
public class SearchController : Controller
1111
{
12-
private readonly IBaGetSearchService _searchService;
13-
private readonly IBaGetUrlGenerator _url;
12+
private readonly IBaGetSearchResource _searchService;
1413

15-
public SearchController(IBaGetSearchService searchService, IBaGetUrlGenerator url)
14+
public SearchController(IBaGetSearchResource searchService)
1615
{
1716
_searchService = searchService ?? throw new ArgumentNullException(nameof(searchService));
18-
_url = url ?? throw new ArgumentNullException(nameof(url));
1917
}
2018

2119
public async Task<ActionResult<SearchResponse>> SearchAsync(

src/BaGet.Core/Content/IBaGetPackageContentService.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace BaGet.Core.Content
1010
/// BaGet's extensions to the NuGet Package Content resource. These additions
1111
/// are not part of the official protocol.
1212
/// </summary>
13-
public interface IBaGetPackageContentService : IPackageContentService
13+
public interface IBaGetPackageContentService : IPackageContentResource
1414
{
1515
/// <summary>
1616
/// Download a package's readme, or null if the package or readme does not exist.

src/BaGet.Protocol/ServiceIndex/IUrlGenerator.cs src/BaGet.Core/IUrlGenerator.cs

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
using System.Threading.Tasks;
21
using NuGet.Versioning;
32

4-
namespace BaGet.Protocol
3+
namespace BaGet.Core
54
{
65
/// <summary>
76
/// Used to create URLs to resources in the NuGet protocol.
@@ -90,9 +89,4 @@ public interface IUrlGenerator
9089
/// <param name="version">The package's version</param>
9190
string GetPackageManifestDownloadUrl(string id, NuGetVersion version);
9291
}
93-
94-
public interface IUrlGeneratorFactory
95-
{
96-
Task<IUrlGenerator> CreateAsync();
97-
}
9892
}

0 commit comments

Comments
 (0)