Skip to content

Instantly share code, notes, and snippets.

@bjorkstromm
Created July 17, 2019 22:10
Show Gist options
  • Save bjorkstromm/7f457595ad007ec5aad0423d81aa0893 to your computer and use it in GitHub Desktop.
Save bjorkstromm/7f457595ad007ec5aad0423d81aa0893 to your computer and use it in GitHub Desktop.
Catalog ids from Registration
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
namespace ConsoleApp3
{
static class HttpContentExtensions
{
public static async Task<JObject> ReadAsJObjectAsync(this HttpContent content)
=> JObject.Parse(await content.ReadAsStringAsync());
public static async Task<JObject> ReadAsGZippedJObjectAsync(this HttpContent content)
{
using var gzipStream = new GZipStream(await content.ReadAsStreamAsync(), CompressionMode.Decompress, leaveOpen: true);
using var sr = new StreamReader(gzipStream);
using var jsonTextReader = new JsonTextReader(sr);
return await JObject.LoadAsync(jsonTextReader);
}
}
class Program
{
static async Task Main(string[] args)
{
var serviceIndex = "https://api.nuget.org/v3/index.json";
var packageId = "mysqlconnector";
//var packageId = "dotnet-depends";
var registrationResource = "RegistrationsBaseUrl/3.6.0";
using (var client = new HttpClient())
{
var response = await client.GetAsync(serviceIndex);
response.EnsureSuccessStatusCode();
var services = await response.Content.ReadAsJObjectAsync();
var registrationBaseUrl = (string)services.SelectToken($"$['resources'][?(@['@type'] == '{registrationResource}')]['@id']");
var registrationUrl = $"{registrationBaseUrl}{packageId.ToLower()}/index.json";
response = await client.GetAsync(registrationUrl);
response.EnsureSuccessStatusCode();
var registrationIndex = await response.Content.ReadAsGZippedJObjectAsync();
// Check if we can get everything from the index
var catalogEntries = registrationIndex.SelectTokens("$['items'][*]['items'][*]['catalogEntry']['@id']");
if (catalogEntries.Any())
{
foreach(var catalogEntry in catalogEntries)
{
Console.WriteLine(catalogEntry);
}
}
else // There are so many registrations, that it is split into multiple pages.
{
var registrationPages = registrationIndex.SelectTokens("$['items'][*]['@id']");
foreach (var registrationPage in registrationPages)
{
response = await client.GetAsync((string)registrationPage);
response.EnsureSuccessStatusCode();
var registration = await response.Content.ReadAsGZippedJObjectAsync();
catalogEntries = registration.SelectTokens("$['items'][*]['catalogEntry']['@id']");
foreach (var catalogEntry in catalogEntries)
{
Console.WriteLine(catalogEntry);
}
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment