mirror of
https://github.com/JustArchiNET/ArchiSteamFarm.git
synced 2026-01-01 06:00:46 +00:00
R# fixed for @Abrynos
This commit is contained in:
@@ -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<ReleaseResponse> GetLatestRelease(bool stable = true) {
|
||||
string releaseURL = SharedInfo.GithubReleaseURL + (stable ? "/latest" : "?per_page=1");
|
||||
|
||||
if (stable) {
|
||||
return await GetReleaseFromURL(releaseURL).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
List<ReleaseResponse> response = await GetReleasesFromURL(releaseURL).ConfigureAwait(false);
|
||||
return response?.FirstOrDefault();
|
||||
}
|
||||
|
||||
internal static async Task<ReleaseResponse> 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<List<ReleaseResponse>> 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<List<ReleaseResponse>> 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<ReleaseResponse> GetLatestRelease(bool stable = true) {
|
||||
string releaseURL = SharedInfo.GithubReleaseURL + (stable ? "/latest" : "?per_page=1");
|
||||
|
||||
if (stable) {
|
||||
return await GetReleaseFromURL(releaseURL).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
List<ReleaseResponse> 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<ReleaseResponse> 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<ReleaseResponse> GetReleaseFromURL(string releaseURL) {
|
||||
if (string.IsNullOrEmpty(nameof(releaseURL))) {
|
||||
ASF.ArchiLogger.LogNullError(nameof(releaseURL));
|
||||
@@ -104,12 +99,7 @@ namespace ArchiSteamFarm {
|
||||
}
|
||||
|
||||
WebBrowser.ObjectResponse<ReleaseResponse> objectResponse = await Program.WebBrowser.UrlGetToJsonObject<ReleaseResponse>(releaseURL).ConfigureAwait(false);
|
||||
if (objectResponse == null) {
|
||||
ASF.ArchiLogger.LogNullError(nameof(objectResponse));
|
||||
return null;
|
||||
}
|
||||
|
||||
return objectResponse.Content;
|
||||
return objectResponse?.Content;
|
||||
}
|
||||
|
||||
private static async Task<List<ReleaseResponse>> GetReleasesFromURL(string releaseURL) {
|
||||
@@ -119,46 +109,22 @@ namespace ArchiSteamFarm {
|
||||
}
|
||||
|
||||
WebBrowser.ObjectResponse<List<ReleaseResponse>> objectResponse = await Program.WebBrowser.UrlGetToJsonObject<List<ReleaseResponse>>(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<Asset> 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() { }
|
||||
|
||||
@@ -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<ActionResult<GenericResponse<GitHubReleaseResponse>>> GetRelease(string version) {
|
||||
if (string.IsNullOrEmpty(version)) {
|
||||
return BadRequest(new GenericResponse<GitHubReleaseResponse>(false, string.Format(Strings.ErrorIsEmpty, nameof(version))));
|
||||
}
|
||||
|
||||
GitHub.ReleaseResponse releaseResponse = await GitHub.GetRelease(version).ConfigureAwait(false);
|
||||
if (releaseResponse == null) {
|
||||
return BadRequest(new GenericResponse<GitHubReleaseResponse>(false, string.Format(Strings.ErrorRequestFailedTooManyTimes, WebBrowser.MaxTries)));
|
||||
}
|
||||
|
||||
return Ok(new GenericResponse<GitHubReleaseResponse>(new GitHubReleaseResponse(releaseResponse)));
|
||||
}
|
||||
|
||||
[HttpGet("Releases")]
|
||||
public async Task<ActionResult<GenericResponse<IEnumerable<GitHubReleaseResponse>>>> GetReleases([FromQuery] byte count = 10) {
|
||||
if (count == 0) {
|
||||
return BadRequest(new GenericResponse<IEnumerable<GitHubReleaseResponse>>(false, string.Format(Strings.ErrorIsEmpty, nameof(count))));
|
||||
}
|
||||
|
||||
List<GitHub.ReleaseResponse> response = await GitHub.GetReleases(count).ConfigureAwait(false);
|
||||
if (response == null || response.Count == 0) {
|
||||
return BadRequest(new GenericResponse<IEnumerable<GitHub.ReleaseResponse>>(false, string.Format(Strings.ErrorRequestFailedTooManyTimes, WebBrowser.MaxTries)));
|
||||
}
|
||||
|
||||
IEnumerable<GitHubReleaseResponse> result = response.Select(singleResponse => new GitHubReleaseResponse(singleResponse));
|
||||
return Ok(new GenericResponse<IEnumerable<GitHubReleaseResponse>>(result));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -57,6 +57,35 @@ namespace ArchiSteamFarm.IPC.Controllers.Api {
|
||||
return Ok(new GenericResponse<HashSet<string>>(result));
|
||||
}
|
||||
|
||||
[HttpGet("GitHub/Releases")]
|
||||
public async Task<ActionResult<GenericResponse<IEnumerable<GitHubReleaseResponse>>>> GitHubReleasesGet([FromQuery] byte count = 10) {
|
||||
if (count == 0) {
|
||||
return BadRequest(new GenericResponse<IEnumerable<GitHubReleaseResponse>>(false, string.Format(Strings.ErrorIsEmpty, nameof(count))));
|
||||
}
|
||||
|
||||
List<GitHub.ReleaseResponse> response = await GitHub.GetReleases(count).ConfigureAwait(false);
|
||||
if ((response == null) || (response.Count == 0)) {
|
||||
return BadRequest(new GenericResponse<IEnumerable<GitHub.ReleaseResponse>>(false, string.Format(Strings.ErrorRequestFailedTooManyTimes, WebBrowser.MaxTries)));
|
||||
}
|
||||
|
||||
IEnumerable<GitHubReleaseResponse> result = response.Select(singleResponse => new GitHubReleaseResponse(singleResponse));
|
||||
return Ok(new GenericResponse<IEnumerable<GitHubReleaseResponse>>(result));
|
||||
}
|
||||
|
||||
[HttpGet("GitHub/Releases/{version:required}")]
|
||||
public async Task<ActionResult<GenericResponse<GitHubReleaseResponse>>> GitHubReleasesGet(string version) {
|
||||
if (string.IsNullOrEmpty(version)) {
|
||||
return BadRequest(new GenericResponse<GitHubReleaseResponse>(false, string.Format(Strings.ErrorIsEmpty, nameof(version))));
|
||||
}
|
||||
|
||||
GitHub.ReleaseResponse releaseResponse = await GitHub.GetRelease(version).ConfigureAwait(false);
|
||||
if (releaseResponse == null) {
|
||||
return BadRequest(new GenericResponse<GitHubReleaseResponse>(false, string.Format(Strings.ErrorRequestFailedTooManyTimes, WebBrowser.MaxTries)));
|
||||
}
|
||||
|
||||
return Ok(new GenericResponse<GitHubReleaseResponse>(new GitHubReleaseResponse(releaseResponse)));
|
||||
}
|
||||
|
||||
[HttpGet("MarkdownToText")]
|
||||
public ActionResult<GenericResponse<string>> MarkdownToTextGet([FromQuery] string text) {
|
||||
if (string.IsNullOrEmpty(text)) {
|
||||
|
||||
@@ -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));
|
||||
|
||||
Reference in New Issue
Block a user