Skip to content

Commit

Permalink
Fixed version number issue and added better error message for Github …
Browse files Browse the repository at this point in the history
…API problems.
  • Loading branch information
Friendly0Fire committed Sep 29, 2021
1 parent e57edc7 commit ea81b46
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 25 deletions.
9 changes: 3 additions & 6 deletions application/GW2 Addon Manager/Backend/Addons/ApprovedList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ public ApprovedList(IConfigurationManager configManager)
public void FetchListFromRepo()
{
const string tempFileName = "addonlist";
var client = new WebClient();
client.Headers.Add("User-Agent", "Gw2 Addon Manager");
var client = UpdateHelpers.GetClient();

var raw = client.DownloadString(RepoUrl + "/branches");
var raw = client.DownloadStringFromGithubAPI(RepoUrl + "/branches");
var result = JsonConvert.DeserializeObject<BranchInfo[]>(raw);
var master = result.Single(r => r.Name == "master").Commit.Sha;

Expand All @@ -43,9 +42,7 @@ public void FetchListFromRepo()
File.Delete(tempFileName);

//fetching new version
client = new WebClient();
client.Headers.Add("User-Agent", "Gw2 Addon Manager");
client.DownloadFile(RepoUrl + "/zipball", tempFileName);
client.DownloadFileFromGithubAPI(RepoUrl + "/zipball", tempFileName);
ZipFile.ExtractToDirectory(tempFileName, AddonFolder);
var downloaded = Directory.EnumerateDirectories(AddonFolder).First();
foreach (var entry in Directory.EnumerateFileSystemEntries(downloaded))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ public async Task downloadLatestRelease()
viewModel.UpdateAvailable = $"{StaticText.Downloading} {latestInfo.tag_name}";

Directory.CreateDirectory(update_folder);
WebClient client = new WebClient();
client.Headers.Add("User-Agent", "request");
var client = UpdateHelpers.GetClient();
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(selfUpdate_DownloadProgress);
client.DownloadFileCompleted += new AsyncCompletedEventHandler(selfUpdate_DownloadCompleted);
await client.DownloadFileTaskAsync(new System.Uri(downloadUrl), Path.Combine(update_folder, update_name));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ public async Task HandleLoaderUpdate()
private async Task Download(string url)
{
viewModel.ProgBarLabel = "Downloading Addon Loader";
var client = new WebClient();
client.Headers.Add("User-Agent", "request");
var client = UpdateHelpers.GetClient();

fileName = Path.Combine(Path.GetTempPath(), Path.GetFileName(url));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ public async Task Update()
/// </summary>
private async Task GitCheckUpdate()
{
var client = new WebClient();
client.Headers.Add("User-Agent", "request");
var client = UpdateHelpers.GetClient();

dynamic release_info = UpdateHelpers.GitReleaseInfo(addon_info.host_url);
latestVersion = release_info.tag_name;
Expand All @@ -74,7 +73,7 @@ private async Task GitCheckUpdate()

private async Task StandaloneCheckUpdate()
{
var client = new WebClient();
var client = UpdateHelpers.GetClient();
string downloadURL = addon_info.host_url;

if (addon_info.version_url != null)
Expand Down
41 changes: 30 additions & 11 deletions application/GW2 Addon Manager/Backend/Updating/UpdateHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,45 @@

namespace GW2_Addon_Manager
{
class UpdateHelpers
static class UpdateHelpers
{
public static dynamic GitReleaseInfo(string gitUrl)
public static WebClient GetClient()
{
var client = new WebClient();
client.Headers.Add("User-Agent", "request");
try
{
string release_info_json = client.DownloadString(gitUrl);
return JsonConvert.DeserializeObject(release_info_json);
client.Headers.Add("User-Agent", "Gw2 Addon Manager");

return client;
}

public static string DownloadStringFromGithubAPI(this WebClient wc, string url)
{
try {
return wc.DownloadString(url);
}
catch (WebException)
{
//TODO: Add this catch to API calls made at application startup as well
MessageBox.Show("Github Servers returned an error; please try again in a few minutes.", "Github API Error", MessageBoxButton.OK, MessageBoxImage.Error);
SelfUpdate.startUpdater();
MessageBox.Show("Github servers returned an error; please try again in a few minutes.", "Github API Error", MessageBoxButton.OK, MessageBoxImage.Error);
Application.Current.Shutdown();
return null;
return "";
}
}

public static void DownloadFileFromGithubAPI(this WebClient wc, string url, string destPath)
{
try {
wc.DownloadFile(url, destPath);
}
catch (WebException) {
MessageBox.Show("Github servers returned an error; please try again in a few minutes.", "Github API Error", MessageBoxButton.OK, MessageBoxImage.Error);
Application.Current.Shutdown();
}
}

public static dynamic GitReleaseInfo(string gitUrl)
{
var client = UpdateHelpers.GetClient();
string release_info_json = client.DownloadStringFromGithubAPI(gitUrl);
return JsonConvert.DeserializeObject(release_info_json);

}

Expand Down
2 changes: 1 addition & 1 deletion application/GW2 Addon Manager/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.3.0.2")]
[assembly: AssemblyVersion("1.4.2.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

0 comments on commit ea81b46

Please sign in to comment.