Last active
February 2, 2024 00:59
-
-
Save camnewnham/e999dcf324cff251f8838701852fa07b to your computer and use it in GitHub Desktop.
JSON classes for Rhino Package Manager (Yak) API
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
using Newtonsoft.Json; | |
using System.Collections.Generic; | |
using System.Net.Http; | |
using System.Threading.Tasks; | |
namespace CN | |
{ | |
public static class YakApi | |
{ | |
private static HttpClient httpClient = new HttpClient(); | |
public static async Task<ExtendedPackageInformation[]> GetVersions(string name) | |
{ | |
HttpResponseMessage response = await httpClient.GetAsync($"https://yak.rhino3d.com/versions/{name}"); | |
if (response.IsSuccessStatusCode) | |
{ | |
string data = await response.Content.ReadAsStringAsync(); | |
ExtendedPackageInformation[] result = JsonConvert.DeserializeObject<ExtendedPackageInformation[]>(data); | |
if (result == null) | |
{ | |
throw new HttpRequestException($"Unable to deserialize request to {nameof(ExtendedPackageInformation)}"); | |
} | |
return result; | |
} | |
else | |
{ | |
throw new HttpRequestException($"Non-success status code {response.StatusCode}"); | |
} | |
} | |
public static async Task<PackageInformation> GetPackage(string name) | |
{ | |
HttpResponseMessage response = await httpClient.GetAsync($"https://yak.rhino3d.com/packages/{name}"); | |
if (response.IsSuccessStatusCode) | |
{ | |
string data = await response.Content.ReadAsStringAsync(); | |
PackageInformation result = JsonConvert.DeserializeObject<PackageInformation>(data); | |
if (result == null) | |
{ | |
throw new HttpRequestException($"Unable to deserialize request to {nameof(PackageInformation)}"); | |
} | |
return result; | |
} | |
else | |
{ | |
throw new HttpRequestException($"Non-success status code {response.StatusCode}"); | |
} | |
} | |
/// <summary> | |
/// Package information returned from https://yak.rhino3d.com/packages/{name} | |
/// </summary> | |
public class PackageInformation | |
{ | |
[JsonProperty("authors")] | |
public string Authors { get; set; } | |
[JsonProperty("download_count")] | |
public int DownloadCount { get; set; } | |
[JsonProperty("name")] | |
public string Name { get; set; } | |
[JsonProperty("url")] | |
public string Url { get; set; } | |
[JsonProperty("version")] | |
public string Version { get; set; } | |
} | |
/// <summary> | |
/// Package information returned from https://yak.rhino3d.com/versions/{name} | |
/// </summary> | |
public class ExtendedPackageInformation : PackageInformation | |
{ | |
[JsonProperty("created_at")] | |
public string CreatedAt { get; set; } | |
[JsonProperty("dependencies")] | |
public List<object> Dependencies { get; set; } | |
[JsonProperty("description")] | |
public string Description { get; set; } | |
[JsonProperty("distributions")] | |
public List<Distribution> Distributions { get; set; } | |
[JsonProperty("download_url")] | |
public object DownloadUrl { get; set; } | |
[JsonProperty("downloads")] | |
public DownloadStatistics Downloads { get; set; } | |
[JsonProperty("homepage_url")] | |
public string HomepageUrl { get; set; } | |
[JsonProperty("icon_url")] | |
public string IconUrl { get; set; } | |
[JsonProperty("keywords")] | |
public List<string> Keywords { get; set; } | |
[JsonProperty("prerelease")] | |
public bool Prerelease { get; set; } | |
} | |
public class Distribution | |
{ | |
[JsonProperty("created_at")] | |
public string CreatedAt { get; set; } | |
[JsonProperty("filename")] | |
public string Filename { get; set; } | |
[JsonProperty("platform")] | |
public string Platform { get; set; } | |
[JsonProperty("rhino_version")] | |
public string RhinoVersion { get; set; } | |
[JsonProperty("size")] | |
public object Size { get; set; } | |
[JsonProperty("url")] | |
public string Url { get; set; } | |
} | |
public class DownloadStatistics | |
{ | |
[JsonProperty("last_day")] | |
public int LastDay { get; set; } | |
[JsonProperty("last_month")] | |
public int LastMonth { get; set; } | |
[JsonProperty("last_week")] | |
public int LastWeek { get; set; } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment