This repository was archived by the owner on Jun 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathGitHubAPI.cs
More file actions
56 lines (49 loc) · 1.77 KB
/
GitHubAPI.cs
File metadata and controls
56 lines (49 loc) · 1.77 KB
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
using System;
using System.Linq;
using System.Net;
namespace AstroModLoader
{
public static class GitHubAPI
{
public static string CombineURI(params string[] uris)
{
string output = "";
foreach (string uriBit in uris)
{
output += uriBit.Trim('/') + "/";
}
return output.TrimEnd('/');
}
public static string GetLatestVersionURL(string repo)
{
return CombineURI("https://github.com", repo, "releases", "latest");
}
public static Version GetLatestVersionFromGitHub(string repo)
{
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(GetLatestVersionURL(repo));
request.Method = "GET";
request.AllowAutoRedirect = false;
request.ContentType = "application/json; charset=utf-8";
request.UserAgent = AMLUtils.UserAgent;
string newURL = null;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
newURL = response.Headers["location"];
}
if (string.IsNullOrEmpty(newURL)) return null;
string[] splitURL = newURL.Split('/');
string finalVersionBit = splitURL[splitURL.Length - 1];
if (finalVersionBit[0] == 'v') finalVersionBit = finalVersionBit.Substring(1);
Version.TryParse(finalVersionBit, out Version foundVersion);
return foundVersion;
}
catch (Exception ex)
{
if (ex is WebException || ex is FormatException) return null;
throw;
}
}
}
}