-
Notifications
You must be signed in to change notification settings - Fork 9
/
PackagesJsonSchema.cs
78 lines (64 loc) · 2.82 KB
/
PackagesJsonSchema.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using System.Text.Json;
using System.Text.Json.Serialization;
using Serilog;
namespace CreateMatrix;
// https://updates.networkoptix.com/{product}/{build}/packages.json
// https://updates.networkoptix.com/metavms/35134/packages.json
// https://updates.networkoptix.com/default/35270/packages.json
// https://updates.networkoptix.com/digitalwatchdog/35271/packages.json
public class Variant
{
[JsonPropertyName("name")]
public string Name { get; set; } = "";
}
public class Package
{
[JsonPropertyName("component")]
public string Component { get; set; } = "";
[JsonPropertyName("platform")]
public string PlatformName { get; set; } = "";
[JsonPropertyName("file")]
public string File { get; set; } = "";
[JsonPropertyName("variants")]
public List<Variant> Variants { get; set; } = [];
public bool IsX64Server()
{
// Test for Server and x64 and Ubuntu
return Component.Equals("server", StringComparison.OrdinalIgnoreCase) &&
PlatformName.Equals("linux_x64", StringComparison.OrdinalIgnoreCase) &&
Variants.Any(variant => variant.Name.Equals("ubuntu", StringComparison.OrdinalIgnoreCase));
}
public bool IsArm64Server()
{
// Test for Server and Arm64 and Ubuntu
return Component.Equals("server", StringComparison.OrdinalIgnoreCase) &&
PlatformName.Equals("linux_arm64", StringComparison.OrdinalIgnoreCase) &&
Variants.Any(variant => variant.Name.Equals("ubuntu", StringComparison.OrdinalIgnoreCase));
}
}
public class PackagesJsonSchema
{
[JsonPropertyName("packages")]
public List<Package> Packages { get; set; } = [];
private static PackagesJsonSchema FromJson(string jsonString)
{
var jsonSchema = JsonSerializer.Deserialize<PackagesJsonSchema>(jsonString, MatrixJsonSchema.JsonReadOptions);
ArgumentNullException.ThrowIfNull(jsonSchema);
return jsonSchema;
}
public static List<Package> GetPackages(HttpClient httpClient, string releaseName, int buildNumber)
{
// Load packages JSON
// https://updates.networkoptix.com/{product}/{build}/packages.json
Uri packagesUri = new($"https://updates.networkoptix.com/{releaseName}/{buildNumber}/packages.json");
Log.Logger.Information("Getting package information from {Uri}", packagesUri);
var jsonString = httpClient.GetStringAsync(packagesUri).Result;
// Deserialize JSON
var packagesSchema = FromJson(jsonString);
ArgumentNullException.ThrowIfNull(packagesSchema);
ArgumentNullException.ThrowIfNull(packagesSchema.Packages);
ArgumentOutOfRangeException.ThrowIfZero(packagesSchema.Packages.Count);
// Return packages
return packagesSchema.Packages;
}
}