From 96e6cd2a164e21b84812a3ace2e8860104585f26 Mon Sep 17 00:00:00 2001 From: JustArchi Date: Sat, 22 Sep 2018 01:25:56 +0200 Subject: [PATCH] R# fixed for @Abrynos --- ArchiSteamFarm/GitHub.cs | 138 ++++++++---------- .../IPC/Controllers/Api/GitHubController.cs | 41 ------ .../IPC/Controllers/Api/WWWController.cs | 29 ++++ .../IPC/Responses/GitHubReleaseResponse.cs | 24 ++- 4 files changed, 111 insertions(+), 121 deletions(-) delete mode 100644 ArchiSteamFarm/IPC/Controllers/Api/GitHubController.cs diff --git a/ArchiSteamFarm/GitHub.cs b/ArchiSteamFarm/GitHub.cs index a76f4df2b..2b8c7d4fe 100644 --- a/ArchiSteamFarm/GitHub.cs +++ b/ArchiSteamFarm/GitHub.cs @@ -19,20 +19,51 @@ // See the License for the specific language governing permissions and // limitations under the License. -using Markdig; -using Markdig.Renderers; -using Markdig.Syntax; -using Markdig.Syntax.Inlines; -using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.IO; using System.Linq; using System.Threading.Tasks; +using Markdig; +using Markdig.Renderers; +using Markdig.Syntax; +using Markdig.Syntax.Inlines; +using Newtonsoft.Json; namespace ArchiSteamFarm { internal static class GitHub { + internal static async Task GetLatestRelease(bool stable = true) { + string releaseURL = SharedInfo.GithubReleaseURL + (stable ? "/latest" : "?per_page=1"); + + if (stable) { + return await GetReleaseFromURL(releaseURL).ConfigureAwait(false); + } + + List response = await GetReleasesFromURL(releaseURL).ConfigureAwait(false); + return response?.FirstOrDefault(); + } + + internal static async Task GetRelease(string version) { + if (string.IsNullOrEmpty(version)) { + ASF.ArchiLogger.LogNullError(nameof(version)); + return null; + } + + return await GetReleaseFromURL(SharedInfo.GithubReleaseURL + "/tags/" + version).ConfigureAwait(false); + } + + internal static async Task> GetReleases(byte count) { + if (count == 0) { + ASF.ArchiLogger.LogNullError(nameof(count)); + return null; + } + + string releaseURL = SharedInfo.GithubReleaseURL + "?per_page=" + count; + + return await GetReleasesFromURL(releaseURL).ConfigureAwait(false); + } + private static MarkdownDocument ExtractChangelogFromBody(string markdownText) { if (string.IsNullOrEmpty(markdownText)) { ASF.ArchiLogger.LogNullError(nameof(markdownText)); @@ -61,42 +92,6 @@ namespace ArchiSteamFarm { return markdownDocument; } - internal static async Task> GetReleases(byte count) { - if (count == 0) { - ASF.ArchiLogger.LogNullError(nameof(count)); - return null; - } - - string releaseURL = SharedInfo.GithubReleaseURL + "?per_page=" + count; - - return await GetReleasesFromURL(releaseURL).ConfigureAwait(false); - } - - internal static async Task GetLatestRelease(bool stable = true) { - string releaseURL = SharedInfo.GithubReleaseURL + (stable ? "/latest" : "?per_page=1"); - - if (stable) { - return await GetReleaseFromURL(releaseURL).ConfigureAwait(false); - } - - List response = await GetReleasesFromURL(releaseURL).ConfigureAwait(false); - if (response == null || response.Count == 0) { - ASF.ArchiLogger.LogNullError(nameof(response)); - return null; - } - - return response.FirstOrDefault(); - } - - internal static async Task GetRelease(string version) { - if (string.IsNullOrEmpty(version)) { - ASF.ArchiLogger.LogNullError(nameof(version)); - return null; - } - - return await GetReleaseFromURL(SharedInfo.GithubReleaseURL + "/tags/" + version).ConfigureAwait(false); - } - private static async Task GetReleaseFromURL(string releaseURL) { if (string.IsNullOrEmpty(nameof(releaseURL))) { ASF.ArchiLogger.LogNullError(nameof(releaseURL)); @@ -104,12 +99,7 @@ namespace ArchiSteamFarm { } WebBrowser.ObjectResponse objectResponse = await Program.WebBrowser.UrlGetToJsonObject(releaseURL).ConfigureAwait(false); - if (objectResponse == null) { - ASF.ArchiLogger.LogNullError(nameof(objectResponse)); - return null; - } - - return objectResponse.Content; + return objectResponse?.Content; } private static async Task> GetReleasesFromURL(string releaseURL) { @@ -119,46 +109,22 @@ namespace ArchiSteamFarm { } WebBrowser.ObjectResponse> objectResponse = await Program.WebBrowser.UrlGetToJsonObject>(releaseURL).ConfigureAwait(false); - if ((objectResponse?.Content == null) || (objectResponse.Content.Count == 0)) { - return null; - } - - return objectResponse.Content; + return objectResponse?.Content; } - [SuppressMessage("ReSharper", "ClassCannotBeInstantiated")] internal sealed class ReleaseResponse { [JsonProperty(PropertyName = "assets", Required = Required.Always)] internal readonly HashSet Assets; - [JsonProperty(PropertyName = "tag_name", Required = Required.Always)] - internal readonly string Tag; - -#pragma warning disable 649 - [JsonProperty(PropertyName = "body", Required = Required.Always)] - private readonly string MarkdownBody; -#pragma warning restore 649 + [JsonProperty(PropertyName = "prerelease", Required = Required.Always)] + internal readonly bool IsPreRelease; [JsonProperty(PropertyName = "published_at", Required = Required.Always)] internal readonly DateTime PublishedAt; - [JsonProperty(PropertyName = "prerelease", Required = Required.Always)] - internal readonly bool IsPreRelease; - - private MarkdownDocument _Changelog; - - private MarkdownDocument Changelog { - get { - if (_Changelog != null) { - return _Changelog; - } - - return _Changelog = ExtractChangelogFromBody(MarkdownBody); - } - } - - private string _ChangelogHTML; + [JsonProperty(PropertyName = "tag_name", Required = Required.Always)] + internal readonly string Tag; internal string ChangelogHTML { get { @@ -181,8 +147,6 @@ namespace ArchiSteamFarm { } } - internal string _ChangelogPlainText; - internal string ChangelogPlainText { get { if (_ChangelogPlainText != null) { @@ -208,6 +172,24 @@ namespace ArchiSteamFarm { } } +#pragma warning disable 649 + [JsonProperty(PropertyName = "body", Required = Required.Always)] + private readonly string MarkdownBody; +#pragma warning restore 649 + + private MarkdownDocument Changelog { + get { + if (_Changelog != null) { + return _Changelog; + } + + return _Changelog = ExtractChangelogFromBody(MarkdownBody); + } + } + + private MarkdownDocument _Changelog; + private string _ChangelogHTML; + private string _ChangelogPlainText; // Deserialized from JSON private ReleaseResponse() { } diff --git a/ArchiSteamFarm/IPC/Controllers/Api/GitHubController.cs b/ArchiSteamFarm/IPC/Controllers/Api/GitHubController.cs deleted file mode 100644 index e4f3dd80f..000000000 --- a/ArchiSteamFarm/IPC/Controllers/Api/GitHubController.cs +++ /dev/null @@ -1,41 +0,0 @@ -using ArchiSteamFarm.IPC.Responses; -using ArchiSteamFarm.Localization; -using Microsoft.AspNetCore.Mvc; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - -namespace ArchiSteamFarm.IPC.Controllers.Api { - [ApiController] - [Route("Api/WWW/GitHub")] - public sealed class GitHubController : ControllerBase { - [HttpGet("Releases/{version:required}")] - public async Task>> GetRelease(string version) { - if (string.IsNullOrEmpty(version)) { - return BadRequest(new GenericResponse(false, string.Format(Strings.ErrorIsEmpty, nameof(version)))); - } - - GitHub.ReleaseResponse releaseResponse = await GitHub.GetRelease(version).ConfigureAwait(false); - if (releaseResponse == null) { - return BadRequest(new GenericResponse(false, string.Format(Strings.ErrorRequestFailedTooManyTimes, WebBrowser.MaxTries))); - } - - return Ok(new GenericResponse(new GitHubReleaseResponse(releaseResponse))); - } - - [HttpGet("Releases")] - public async Task>>> GetReleases([FromQuery] byte count = 10) { - if (count == 0) { - return BadRequest(new GenericResponse>(false, string.Format(Strings.ErrorIsEmpty, nameof(count)))); - } - - List response = await GitHub.GetReleases(count).ConfigureAwait(false); - if (response == null || response.Count == 0) { - return BadRequest(new GenericResponse>(false, string.Format(Strings.ErrorRequestFailedTooManyTimes, WebBrowser.MaxTries))); - } - - IEnumerable result = response.Select(singleResponse => new GitHubReleaseResponse(singleResponse)); - return Ok(new GenericResponse>(result)); - } - } -} diff --git a/ArchiSteamFarm/IPC/Controllers/Api/WWWController.cs b/ArchiSteamFarm/IPC/Controllers/Api/WWWController.cs index c265e6982..0f6a3c192 100644 --- a/ArchiSteamFarm/IPC/Controllers/Api/WWWController.cs +++ b/ArchiSteamFarm/IPC/Controllers/Api/WWWController.cs @@ -57,6 +57,35 @@ namespace ArchiSteamFarm.IPC.Controllers.Api { return Ok(new GenericResponse>(result)); } + [HttpGet("GitHub/Releases")] + public async Task>>> GitHubReleasesGet([FromQuery] byte count = 10) { + if (count == 0) { + return BadRequest(new GenericResponse>(false, string.Format(Strings.ErrorIsEmpty, nameof(count)))); + } + + List response = await GitHub.GetReleases(count).ConfigureAwait(false); + if ((response == null) || (response.Count == 0)) { + return BadRequest(new GenericResponse>(false, string.Format(Strings.ErrorRequestFailedTooManyTimes, WebBrowser.MaxTries))); + } + + IEnumerable result = response.Select(singleResponse => new GitHubReleaseResponse(singleResponse)); + return Ok(new GenericResponse>(result)); + } + + [HttpGet("GitHub/Releases/{version:required}")] + public async Task>> GitHubReleasesGet(string version) { + if (string.IsNullOrEmpty(version)) { + return BadRequest(new GenericResponse(false, string.Format(Strings.ErrorIsEmpty, nameof(version)))); + } + + GitHub.ReleaseResponse releaseResponse = await GitHub.GetRelease(version).ConfigureAwait(false); + if (releaseResponse == null) { + return BadRequest(new GenericResponse(false, string.Format(Strings.ErrorRequestFailedTooManyTimes, WebBrowser.MaxTries))); + } + + return Ok(new GenericResponse(new GitHubReleaseResponse(releaseResponse))); + } + [HttpGet("MarkdownToText")] public ActionResult> MarkdownToTextGet([FromQuery] string text) { if (string.IsNullOrEmpty(text)) { diff --git a/ArchiSteamFarm/IPC/Responses/GitHubReleaseResponse.cs b/ArchiSteamFarm/IPC/Responses/GitHubReleaseResponse.cs index 8ce19b4fa..9d29cf78b 100644 --- a/ArchiSteamFarm/IPC/Responses/GitHubReleaseResponse.cs +++ b/ArchiSteamFarm/IPC/Responses/GitHubReleaseResponse.cs @@ -1,5 +1,26 @@ -using Newtonsoft.Json; +// _ _ _ ____ _ _____ +// / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___ +// / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \ +// / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | | +// /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_| +// +// Copyright 2015-2018 Łukasz "JustArchi" Domeradzki +// Contact: JustArchi@JustArchi.net +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + using System; +using Newtonsoft.Json; namespace ArchiSteamFarm.IPC.Responses { public sealed class GitHubReleaseResponse { @@ -15,7 +36,6 @@ namespace ArchiSteamFarm.IPC.Responses { [JsonProperty] private readonly string Version; - internal GitHubReleaseResponse(GitHub.ReleaseResponse releaseResponse) { if (releaseResponse == null) { throw new ArgumentNullException(nameof(releaseResponse));