forked from loic-sharma/BaGet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Client SDK] Add catalog client (loic-sharma#321)
Add the low level catalog resource client. Part of loic-sharma#308
- Loading branch information
1 parent
fa5ddb7
commit eea6a82
Showing
26 changed files
with
924 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using System; | ||
using System.Net.Http; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace BaGet.Protocol.Internal | ||
{ | ||
public class CatalogClient : ICatalogResource | ||
{ | ||
private readonly HttpClient _httpClient; | ||
private readonly string _catalogUrl; | ||
|
||
public CatalogClient(HttpClient httpClient, string catalogUrl) | ||
{ | ||
_httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient)); | ||
_catalogUrl = catalogUrl ?? throw new ArgumentNullException(nameof(catalogUrl)); | ||
} | ||
|
||
public async Task<CatalogIndex> GetIndexAsync(CancellationToken cancellationToken = default) | ||
{ | ||
var response = await _httpClient.DeserializeUrlAsync<CatalogIndex>(_catalogUrl, cancellationToken); | ||
|
||
return response.GetResultOrThrow(); | ||
} | ||
|
||
public async Task<CatalogPage> GetPageAsync(string pageUrl, CancellationToken cancellationToken = default) | ||
{ | ||
var response = await _httpClient.DeserializeUrlAsync<CatalogPage>(pageUrl, cancellationToken); | ||
|
||
return response.GetResultOrThrow(); | ||
} | ||
|
||
public async Task<PackageDeleteCatalogLeaf> GetPackageDeleteLeafAsync(string leafUrl, CancellationToken cancellationToken = default) | ||
{ | ||
return await GetAndValidateLeafAsync<PackageDeleteCatalogLeaf>( | ||
CatalogLeafType.PackageDelete, | ||
leafUrl, | ||
cancellationToken); | ||
} | ||
|
||
public async Task<PackageDetailsCatalogLeaf> GetPackageDetailsLeafAsync(string leafUrl, CancellationToken cancellationToken = default) | ||
{ | ||
return await GetAndValidateLeafAsync<PackageDetailsCatalogLeaf>( | ||
CatalogLeafType.PackageDetails, | ||
leafUrl, | ||
cancellationToken); | ||
} | ||
|
||
private async Task<T> GetAndValidateLeafAsync<T>( | ||
CatalogLeafType type, | ||
string leafUrl, | ||
CancellationToken cancellationToken) where T : CatalogLeaf | ||
{ | ||
var result = await _httpClient.DeserializeUrlAsync<T>(leafUrl, cancellationToken); | ||
var leaf = result.GetResultOrThrow(); | ||
|
||
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Newtonsoft.Json; | ||
|
||
namespace BaGet.Protocol | ||
{ | ||
/// <summary> | ||
/// The catalog index is the entry point for the catalog resource. | ||
/// Use this to discover catalog pages, which in turn can be used to discover catalog leafs. | ||
/// See: https://docs.microsoft.com/en-us/nuget/api/catalog-resource#catalog-index | ||
/// Based off: https://github.com/NuGet/NuGet.Services.Metadata/blob/64af0b59c5a79e0143f0808b39946df9f16cb2e7/src/NuGet.Protocol.Catalog/Models/CatalogIndex.cs | ||
/// </summary> | ||
public class CatalogIndex | ||
{ | ||
[JsonProperty("commitTimeStamp")] | ||
public DateTimeOffset CommitTimestamp { get; set; } | ||
|
||
[JsonProperty("count")] | ||
public int Count { get; set; } | ||
|
||
[JsonProperty("items")] | ||
public List<CatalogPageItem> Items { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System; | ||
using BaGet.Protocol.Internal; | ||
using Newtonsoft.Json; | ||
|
||
namespace BaGet.Protocol | ||
{ | ||
/// <summary> | ||
/// A catalog leaf. Represents a single package event. | ||
/// Leafs can be discovered from a <see cref="CatalogPage"/>. | ||
/// See: https://docs.microsoft.com/en-us/nuget/api/catalog-resource#catalog-leaf | ||
/// Based off: https://github.com/NuGet/NuGet.Services.Metadata/blob/64af0b59c5a79e0143f0808b39946df9f16cb2e7/src/NuGet.Protocol.Catalog/Models/CatalogLeaf.cs | ||
/// </summary> | ||
public class CatalogLeaf : ICatalogLeafItem | ||
{ | ||
[JsonProperty("@id")] | ||
public string Url { get; set; } | ||
|
||
[JsonProperty("@type")] | ||
[JsonConverter(typeof(CatalogLeafTypeConverter))] | ||
public CatalogLeafType Type { get; set; } | ||
|
||
[JsonProperty("catalog:commitId")] | ||
public string CommitId { get; set; } | ||
|
||
[JsonProperty("catalog:commitTimeStamp")] | ||
public DateTimeOffset CommitTimestamp { get; set; } | ||
|
||
[JsonProperty("id")] | ||
public string PackageId { get; set; } | ||
|
||
[JsonProperty("published")] | ||
public DateTimeOffset Published { get; set; } | ||
|
||
[JsonProperty("version")] | ||
public string PackageVersion { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System; | ||
using BaGet.Protocol.Internal; | ||
using Newtonsoft.Json; | ||
|
||
namespace BaGet.Protocol | ||
{ | ||
/// <summary> | ||
/// An item in a <see cref="CatalogPage"/> that references a <see cref="CatalogLeaf"/>. | ||
/// See: https://docs.microsoft.com/en-us/nuget/api/catalog-resource#catalog-item-object-in-a-page | ||
/// Based off: https://github.com/NuGet/NuGet.Services.Metadata/blob/64af0b59c5a79e0143f0808b39946df9f16cb2e7/src/NuGet.Protocol.Catalog/Models/CatalogLeafItem.cs | ||
/// </summary> | ||
public class CatalogLeafItem : ICatalogLeafItem | ||
{ | ||
[JsonProperty("@id")] | ||
public string Url { get; set; } | ||
|
||
[JsonProperty("@type")] | ||
[JsonConverter(typeof(CatalogLeafItemTypeConverter))] | ||
public CatalogLeafType Type { get; set; } | ||
|
||
[JsonProperty("commitTimeStamp")] | ||
public DateTimeOffset CommitTimestamp { get; set; } | ||
|
||
[JsonProperty("nuget:id")] | ||
public string PackageId { get; set; } | ||
|
||
[JsonProperty("nuget:version")] | ||
public string PackageVersion { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
namespace BaGet.Protocol | ||
{ | ||
/// <summary> | ||
/// The type of a <see cref="CatalogLeaf"/>. | ||
/// Based off: https://github.com/NuGet/NuGet.Services.Metadata/blob/64af0b59c5a79e0143f0808b39946df9f16cb2e7/src/NuGet.Protocol.Catalog/Models/CatalogLeafType.cs | ||
/// </summary> | ||
public enum CatalogLeafType | ||
{ | ||
/// <summary> | ||
/// The <see cref="CatalogLeaf"/> represents the snapshot of a package's metadata. | ||
/// </summary> | ||
PackageDetails = 1, | ||
|
||
/// <summary> | ||
/// The <see cref="CatalogLeaf"/> represents a package that was deleted. | ||
/// </summary> | ||
PackageDelete = 2, | ||
} | ||
} |
Oops, something went wrong.