public async Task <UpdateCheckResult> CheckForUpdateAsync() { UpdateCheckResult result = null; if (httpClient != null) { string url = GITHUB_RELEASE_API_URL; Logger.Debug($"Sending a request to {url}"); HttpResponseMessage response = await httpClient.GetAsync(url); Logger.Debug($"Response status code: {(int)response.StatusCode} {response.StatusCode}."); Logger.Debug("Response headers:", response.Headers.ToString().TrimEnd(), response.Content.Headers.ToString().TrimEnd()); if (response.StatusCode != HttpStatusCode.OK) { throw new Exception($"GitHub API returned {(int)response.StatusCode} {response.StatusCode}."); } string responseContent = await response.Content.ReadAsStringAsync(); Logger.Debug("Response content:", responseContent); List <GitHubApiRelease> releases; try { releases = JsonConvert.DeserializeObject <List <GitHubApiRelease> >(responseContent); } catch (Exception exception) { throw new Exception("GitHub API response is not a valid JSON string.", exception); } Logger.Debug($"{releases.Count} releases have been parsed from the GitHub API response."); if (releases.Any()) { GitHubApiRelease latestRelease = releases.First(); Logger.Debug($@"Latest release is ""{latestRelease.Name}""."); if (latestRelease.Name != CURRENT_GITHUB_RELEASE_NAME && latestRelease.Name != ignoreReleaseName) { GitHubApiRelease.Asset asset = latestRelease.Assets.FirstOrDefault(assetItem => assetItem.Name == expectedAssetName); if (asset != null) { Logger.Debug($"New asset is {asset.Name} ({asset.Size} bytes), download URL = {asset.DownloadUrl}."); result = new UpdateCheckResult(latestRelease.Name, latestRelease.PublishedAt, asset.Name, asset.Size, asset.DownloadUrl); } } } } return(result); }
public async Task <UpdateCheckResult> CheckForUpdateAsync(bool ignoreSpecifiedRelease) { UpdateCheckResult result = null; if (httpClient != null) { DownloadUtils.DownloadPageResult downloadPageResult = await DownloadUtils.DownloadPageAsync(httpClient, updateUrl, CancellationToken.None); if (downloadPageResult.HttpStatusCode != HttpStatusCode.OK) { throw new Exception($"GitHub API returned {(int)downloadPageResult.HttpStatusCode} {downloadPageResult.HttpStatusCode}."); } List <GitHubApiRelease> releases; try { releases = JsonConvert.DeserializeObject <List <GitHubApiRelease> >(downloadPageResult.PageContent); } catch (Exception exception) { throw new Exception("GitHub API response is not a valid JSON string.", exception); } Logger.Debug($"{releases.Count} releases have been parsed from the GitHub API response."); if (releases.Any()) { GitHubApiRelease latestRelease = releases.First(); Logger.Debug($@"Latest release is ""{latestRelease.Name}""."); if (latestRelease.Name != CURRENT_GITHUB_RELEASE_NAME && (!ignoreSpecifiedRelease || latestRelease.Name != ignoreReleaseName)) { GitHubApiRelease.Asset asset = latestRelease.Assets.FirstOrDefault(assetItem => assetItem.Name == expectedAssetName); if (asset != null) { Logger.Debug($"New asset is {asset.Name} ({asset.Size} bytes), download URL = {asset.DownloadUrl}."); result = new UpdateCheckResult(latestRelease.Name, latestRelease.PublishedAt, asset.Name, asset.Size, asset.DownloadUrl); } } } } return(result); }