Skip to content
This repository has been archived by the owner on Mar 16, 2021. It is now read-only.

Commit

Permalink
Copy NuGet.Protocol.Catalog from Samples repository (#405)
Browse files Browse the repository at this point in the history
  • Loading branch information
joelverhagen committed Nov 14, 2018
1 parent b87f57b commit d136f6e
Show file tree
Hide file tree
Showing 35 changed files with 2,087 additions and 3 deletions.
30 changes: 30 additions & 0 deletions NuGet.Services.Metadata.sln
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CatalogMetadataTests", "tes
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "V3PerPackage", "src\V3PerPackage\V3PerPackage.csproj", "{E76E73FA-4462-4F07-94C0-8B9CC413F696}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NuGet.Protocol.Catalog", "src\NuGet.Protocol.Catalog\NuGet.Protocol.Catalog.csproj", "{D44C2E89-2D98-44BD-8712-8CCBE4E67C9C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NuGet.Protocol.Catalog.Tests", "tests\NuGet.Protocol.Catalog.Tests\NuGet.Protocol.Catalog.Tests.csproj", "{1F3BC053-796C-4A35-88F4-955A0F142197}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -190,6 +194,30 @@ Global
{E76E73FA-4462-4F07-94C0-8B9CC413F696}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{E76E73FA-4462-4F07-94C0-8B9CC413F696}.Release|x64.ActiveCfg = Release|Any CPU
{E76E73FA-4462-4F07-94C0-8B9CC413F696}.Release|x64.Build.0 = Release|Any CPU
{D44C2E89-2D98-44BD-8712-8CCBE4E67C9C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D44C2E89-2D98-44BD-8712-8CCBE4E67C9C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D44C2E89-2D98-44BD-8712-8CCBE4E67C9C}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{D44C2E89-2D98-44BD-8712-8CCBE4E67C9C}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{D44C2E89-2D98-44BD-8712-8CCBE4E67C9C}.Debug|x64.ActiveCfg = Debug|Any CPU
{D44C2E89-2D98-44BD-8712-8CCBE4E67C9C}.Debug|x64.Build.0 = Debug|Any CPU
{D44C2E89-2D98-44BD-8712-8CCBE4E67C9C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D44C2E89-2D98-44BD-8712-8CCBE4E67C9C}.Release|Any CPU.Build.0 = Release|Any CPU
{D44C2E89-2D98-44BD-8712-8CCBE4E67C9C}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{D44C2E89-2D98-44BD-8712-8CCBE4E67C9C}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{D44C2E89-2D98-44BD-8712-8CCBE4E67C9C}.Release|x64.ActiveCfg = Release|Any CPU
{D44C2E89-2D98-44BD-8712-8CCBE4E67C9C}.Release|x64.Build.0 = Release|Any CPU
{1F3BC053-796C-4A35-88F4-955A0F142197}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1F3BC053-796C-4A35-88F4-955A0F142197}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1F3BC053-796C-4A35-88F4-955A0F142197}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{1F3BC053-796C-4A35-88F4-955A0F142197}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{1F3BC053-796C-4A35-88F4-955A0F142197}.Debug|x64.ActiveCfg = Debug|Any CPU
{1F3BC053-796C-4A35-88F4-955A0F142197}.Debug|x64.Build.0 = Debug|Any CPU
{1F3BC053-796C-4A35-88F4-955A0F142197}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1F3BC053-796C-4A35-88F4-955A0F142197}.Release|Any CPU.Build.0 = Release|Any CPU
{1F3BC053-796C-4A35-88F4-955A0F142197}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{1F3BC053-796C-4A35-88F4-955A0F142197}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{1F3BC053-796C-4A35-88F4-955A0F142197}.Release|x64.ActiveCfg = Release|Any CPU
{1F3BC053-796C-4A35-88F4-955A0F142197}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -207,6 +235,8 @@ Global
{1745A383-D0BE-484B-81EB-27B20F6AC6C5} = {5DE01C58-D5F7-482F-8256-A8333064384C}
{34AABA7F-1FF7-4F4B-B1DB-D07AD4505DA4} = {F1C83FD9-A498-483E-ADFA-B55D82A14965}
{E76E73FA-4462-4F07-94C0-8B9CC413F696} = {C86C6DEE-84E1-4E4E-8868-6755D7A8E0E4}
{D44C2E89-2D98-44BD-8712-8CCBE4E67C9C} = {5DE01C58-D5F7-482F-8256-A8333064384C}
{1F3BC053-796C-4A35-88F4-955A0F142197} = {F1C83FD9-A498-483E-ADFA-B55D82A14965}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {D3AB83E9-02B4-4FFA-A2D0-637F0B97E626}
Expand Down
6 changes: 4 additions & 2 deletions build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ Invoke-BuildStep 'Set version metadata in AssemblyInfo.cs' {
"src\Catalog\Properties\AssemblyInfo.g.cs", `
"src\NuGet.ApplicationInsights.Owin\Properties\AssemblyInfo.g.cs", `
"src\Ng\Properties\AssemblyInfo.g.cs", `
"src\NuGet.Services.Metadata.Catalog.Monitoring\Properties\AssemblyInfo.g.cs"
"src\NuGet.Services.Metadata.Catalog.Monitoring\Properties\AssemblyInfo.g.cs", `
"src\NuGet.Protocol.Catalog\Properties\AssemblyInfo.g.cs"

Foreach ($assemblyInfo in $assemblyInfos) {
Set-VersionInfo -Path (Join-Path $PSScriptRoot $assemblyInfo) -Version $SimpleVersion -Branch $Branch -Commit $CommitSHA
Expand All @@ -98,7 +99,8 @@ Invoke-BuildStep 'Creating artifacts' {
"src\NuGet.Indexing\NuGet.Indexing.csproj", `
"src\Catalog\NuGet.Services.Metadata.Catalog.csproj", `
"src\NuGet.ApplicationInsights.Owin\NuGet.ApplicationInsights.Owin.csproj", `
"src\NuGet.Services.Metadata.Catalog.Monitoring\NuGet.Services.Metadata.Catalog.Monitoring.csproj"
"src\NuGet.Services.Metadata.Catalog.Monitoring\NuGet.Services.Metadata.Catalog.Monitoring.csproj", `
"src\NuGet.Protocol.Catalog\NuGet.Protocol.Catalog.csproj"

$csprojPackages | ForEach-Object {
New-ProjectPackage (Join-Path $PSScriptRoot $_) -Configuration $Configuration -Symbols -BuildNumber $BuildNumber -Version $SemanticVersion -Branch $Branch
Expand Down
113 changes: 113 additions & 0 deletions src/NuGet.Protocol.Catalog/CatalogClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;

namespace NuGet.Protocol.Catalog
{
public class CatalogClient : ICatalogClient
{
private static readonly JsonSerializer JsonSerializer = CatalogJsonSerialization.Serializer;
private readonly HttpClient _httpClient;
private readonly ILogger<CatalogClient> _logger;

public CatalogClient(HttpClient httpClient, ILogger<CatalogClient> logger)
{
_httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}

public Task<CatalogIndex> GetIndexAsync(string indexUrl)
{
return DeserializeUrlAsync<CatalogIndex>(indexUrl);
}

public Task<CatalogPage> GetPageAsync(string pageUrl)
{
return DeserializeUrlAsync<CatalogPage>(pageUrl);
}

public async Task<CatalogLeaf> GetLeafAsync(string leafUrl)
{
// Buffer all of the JSON so we can parse twice. Once to determine the leaf type and once to deserialize
// the entire thing to the proper leaf type.
_logger.LogDebug("Downloading {leafUrl} as a byte array.", leafUrl);
var jsonBytes = await _httpClient.GetByteArrayAsync(leafUrl);
var untypedLeaf = DeserializeBytes<CatalogLeaf>(jsonBytes);

switch (untypedLeaf.Type)
{
case CatalogLeafType.PackageDetails:
return DeserializeBytes<PackageDetailsCatalogLeaf>(jsonBytes);
case CatalogLeafType.PackageDelete:
return DeserializeBytes<PackageDeleteCatalogLeaf>(jsonBytes);
default:
throw new NotSupportedException($"The catalog leaf type '{untypedLeaf.Type}' is not supported.");
}
}

private async Task<CatalogLeaf> GetLeafAsync(CatalogLeafType type, string leafUrl)
{
switch (type)
{
case CatalogLeafType.PackageDetails:
return await GetPackageDetailsLeafAsync(leafUrl);
case CatalogLeafType.PackageDelete:
return await GetPackageDeleteLeafAsync(leafUrl);
default:
throw new NotSupportedException($"The catalog leaf type '{type}' is not supported.");
}
}

public Task<PackageDeleteCatalogLeaf> GetPackageDeleteLeafAsync(string leafUrl)
{
return GetAndValidateLeafAsync<PackageDeleteCatalogLeaf>(CatalogLeafType.PackageDelete, leafUrl);
}

public Task<PackageDetailsCatalogLeaf> GetPackageDetailsLeafAsync(string leafUrl)
{
return GetAndValidateLeafAsync<PackageDetailsCatalogLeaf>(CatalogLeafType.PackageDetails, leafUrl);
}

private async Task<T> GetAndValidateLeafAsync<T>(CatalogLeafType type, string leafUrl) where T : CatalogLeaf
{
var leaf = await DeserializeUrlAsync<T>(leafUrl);

if (leaf.Type != type)
{
throw new ArgumentException(
$"The leaf type found in the document does not match the expected '{type}' type.",
nameof(type));
}

return leaf;
}

private T DeserializeBytes<T>(byte[] jsonBytes)
{
using (var stream = new MemoryStream(jsonBytes))
using (var textReader = new StreamReader(stream))
using (var jsonReader = new JsonTextReader(textReader))
{
return JsonSerializer.Deserialize<T>(jsonReader);
}
}

private async Task<T> DeserializeUrlAsync<T>(string documentUrl)
{
_logger.LogDebug("Downloading {documentUrl} as a stream.", documentUrl);

using (var stream = await _httpClient.GetStreamAsync(documentUrl))
using (var textReader = new StreamReader(stream))
using (var jsonReader = new JsonTextReader(textReader))
{
return JsonSerializer.Deserialize<T>(jsonReader);
}
}
}
}
Loading

0 comments on commit d136f6e

Please sign in to comment.