1
0
mirror of https://github.com/chylex/TweetDuck.git synced 2024-10-17 09:42:45 +02:00
TweetDuck/Updates/UpdateCheckClient.cs
Daniel Chýlek 1ccefe853a
Update .NET & begin refactoring code into a core lib (#264)
* Switch to .NET Framework 4.7.2 & C# 8.0, update libraries

* Add TweetLib.Core project targeting .NET Standard 2.0

* Enable reference nullability checks for TweetLib.Core

* Move a bunch of utility classes into TweetLib.Core & refactor

* Partially move TweetDuck plugin & update system to TweetLib.Core

* Move some constants and CultureInfo setup to TweetLib.Core

* Move some configuration classes to TweetLib.Core

* Minor refactoring and warning suppression

* Add App to TweetLib.Core

* Add IAppErrorHandler w/ implementation

* Continue moving config, plugin, and update classes to TweetLib.Core

* Fix a few nullability checks

* Update installers to check for .NET Framework 4.7.2
2019-05-26 14:55:12 +02:00

84 lines
3.1 KiB
C#

using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;
using TweetDuck.Core.Utils;
using TweetLib.Core.Features.Updates;
using TweetLib.Core.Utils;
using JsonObject = System.Collections.Generic.IDictionary<string, object>;
namespace TweetDuck.Updates{
sealed class UpdateCheckClient : IUpdateCheckClient{
private const string ApiLatestRelease = "https://api.github.com/repos/chylex/TweetDuck/releases/latest";
private const string UpdaterAssetName = "TweetDuck.Update.exe";
private readonly string installerFolder;
public UpdateCheckClient(string installerFolder){
this.installerFolder = installerFolder;
}
bool IUpdateCheckClient.CanCheck => Program.Config.User.EnableUpdateCheck;
Task<UpdateInfo> IUpdateCheckClient.Check(){
TaskCompletionSource<UpdateInfo> result = new TaskCompletionSource<UpdateInfo>();
WebClient client = WebUtils.NewClient(BrowserUtils.UserAgentVanilla);
client.Headers[HttpRequestHeader.Accept] = "application/vnd.github.v3+json";
client.DownloadStringTaskAsync(ApiLatestRelease).ContinueWith(task => {
if (task.IsCanceled){
result.SetCanceled();
}
else if (task.IsFaulted){
result.SetException(ExpandWebException(task.Exception.InnerException));
}
else{
try{
result.SetResult(ParseFromJson(task.Result));
}catch(Exception e){
result.SetException(e);
}
}
});
return result.Task;
}
private UpdateInfo ParseFromJson(string json){
bool IsUpdaterAsset(JsonObject obj){
return UpdaterAssetName == (string)obj["name"];
}
string AssetDownloadUrl(JsonObject obj){
return (string)obj["browser_download_url"];
}
JsonObject root = (JsonObject)new JavaScriptSerializer().DeserializeObject(json);
string versionTag = (string)root["tag_name"];
string releaseNotes = (string)root["body"];
string downloadUrl = ((Array)root["assets"]).Cast<JsonObject>().Where(IsUpdaterAsset).Select(AssetDownloadUrl).FirstOrDefault();
return new UpdateInfo(versionTag, releaseNotes, downloadUrl, installerFolder);
}
private static Exception ExpandWebException(Exception e){
if (e is WebException we && we.Response is HttpWebResponse response){
try{
using var stream = response.GetResponseStream();
using var reader = new StreamReader(stream, Encoding.GetEncoding(response.CharacterSet ?? "utf-8"));
return new Reporter.ExpandedLogException(e, reader.ReadToEnd());
}catch{
// whatever
}
}
return e;
}
}
}