-
Notifications
You must be signed in to change notification settings - Fork 2
/
AdoptiumAPI.cs
195 lines (157 loc) · 8.07 KB
/
AdoptiumAPI.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace AJ_UpdateWatcher
{
static public class AdoptiumAPI_ParameterEnumeration
{
static public List<string> JVMs = new List<string>() { "hotspot", "openj9" };
static public List<string> ImageTypes = new List<string>() { "jre", "jdk" };
static public List<string> HeapSizes = new List<string>() { "normal", "large" };
static public List<string> Archs = new List<string>() { "x64", "x32" };
}
static public class AdoptiumAPI_MostRecentVerbs
{
static public readonly string MostRecentVerb = "Most recent";
static public readonly string MostRecentLTSVerb = "Most recent LTS";
}
public class AdoptiumAPI_AvailableReleases
{
public List<string> Releases;
public List<string> LTSReleases;
public string MostRecentFeatureRelease;
public string MostRecentLTSRelease;
}
static class AdoptiumAPI
{
public const string baseDOMAIN = "api.adoptopenjdk.net";
public const string baseURL = "https://api.adoptopenjdk.net/v3/";
static public List<string> GetReleases()
{
Tuple<List<string>, List<string>> a = GetAllReleases();
return a.Item1;
}
static public Tuple<List<string>, List<string>> GetAllReleases()
{
AdoptiumAPI_AvailableReleases all_releases = GetAvailableReleases();
List<string> releases = all_releases.Releases;
List<string> LTS_releases = all_releases.LTSReleases;
return new Tuple<List<string>, List<string>>( releases, LTS_releases);
}
static public AdoptiumAPI_AvailableReleases GetAvailableReleases()
{
const string URL = baseURL + "info/available_releases";
List<string> releases = new List<string>();
List<string> LTS_releases = new List<string>();
string MostRecentFeatureRelease = "";
string MostRecentLTSRelease = "";
try
{
HttpClientHandler hch = new HttpClientHandler();
hch.Proxy = null;
hch.UseProxy = false;
var httpClient = new HttpClient(hch);
httpClient.DefaultRequestHeaders.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json"));
var response = httpClient.GetStringAsync(new Uri(URL)).Result;
JObject joResponse = JObject.Parse(response);
JArray array = (JArray)joResponse["available_releases"];
foreach (var a in array)
releases.Add(a.Value<string>());
JArray arrayLTS = (JArray)joResponse["available_lts_releases"];
foreach (var a in arrayLTS)
LTS_releases.Add(a.Value<string>());
MostRecentFeatureRelease = (string)joResponse["most_recent_feature_release"];
MostRecentLTSRelease = (string)joResponse["most_recent_lts"];
}
catch (Exception ex)
{
var ie = ex;
while (ie.InnerException != null) ie = ie.InnerException;
var error_message = $"GetAvailableReleases[{URL}]: {ex.Message} => {ie.Message}";
MessageBox.Show(
$"Unable to get list of {Branding.TargetProduct} releases. Make sure you are connected to the internet and {baseDOMAIN} is online." + Environment.NewLine + Environment.NewLine + Environment.NewLine +
$"Tech details:{Environment.NewLine}{error_message}"
, $"{Branding.TargetProduct} API Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
AdoptiumAPI_AvailableReleases info = new AdoptiumAPI_AvailableReleases();
info.Releases = releases;
info.LTSReleases = LTS_releases;
info.MostRecentFeatureRelease = MostRecentFeatureRelease;
info.MostRecentLTSRelease = MostRecentLTSRelease;
return info;
}
static public AdoptiumReleaseVersion GetLatestVersion(string version, string implementation, string desired_image_type, string desired_heap, out string error_message_out, string desired_arch = "x64", string desired_os = "windows")
{
string URL = baseURL + "assets/latest/" + version + "/" + implementation;
AdoptiumReleaseVersion latest = new AdoptiumReleaseVersion();
error_message_out = "";
try
{
HttpClientHandler hch = new HttpClientHandler();
hch.Proxy = null;
hch.UseProxy = false;
var httpClient = new HttpClient(hch);
Debug.WriteLine($"Querying API: {URL}");
httpClient.DefaultRequestHeaders.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json"));
var response = httpClient.GetStringAsync(new Uri(URL)).Result;
JArray a = JArray.Parse(response);
//MessageBox.Show(a.ToString());
foreach (JObject o in a.Children<JObject>())
{
string arch = (string)o["binary"]["architecture"];
string image_type = (string)o["binary"]["image_type"];
string heap_size = (string)o["binary"]["heap_size"];
string os = (string)o["binary"]["os"];
if (
arch == desired_arch &&
image_type == desired_image_type &&
os == desired_os &&
heap_size == desired_heap
)
{
string version_major = (string)o["version"]["major"];
string version_minor = (string)o["version"]["minor"];
string version_security = (string)o["version"]["security"];
string version_build = (string)o["version"]["build"];
string version_string = (string)o["version"]["openjdk_version"];
string version_release = (string)o["release_name"];
string zip_url = (string)o["binary"]["package"]["link"];
string msi_url = (o["binary"]["installer"] != null && o["binary"]["installer"]["link"] != null) ?
(string)o["binary"]["installer"]["link"] : null;
//MessageBox.Show(o.ToString());
latest.Major = version_major;
latest.Minor = version_minor;
latest.Security = version_security;
latest.Build = version_build;
latest.VersionString = version_string;
latest.ReleaseName = version_release;
latest.MSIURL = msi_url;
latest.ZIPURL = zip_url;
latest.ImageType = image_type;
latest.Found = true;
break;
}
}
if (!latest.Found)
error_message_out = $"Nothing in API response ({version}/{implementation}) matches your set of release parameters ({desired_os}/{desired_arch}/{desired_image_type}/{desired_heap} heap).";
}
catch (Exception ex)
{
var ie = ex;
while (ie.InnerException != null) ie = ie.InnerException;
error_message_out += $"GetLatestVersion[{URL}]: {ex.Message}" + (ie.InnerException != null ? $" => {ie.Message}" : "");
//if (latest.)
Debug.WriteLine(error_message_out);
//MessageBox.Show("There was an error: " + ex.Message, "Adoptium API Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
return latest;
}
}
}