diff --git a/ArchiSteamFarm.CustomPlugins.ExamplePlugin/CatAPI.cs b/ArchiSteamFarm.CustomPlugins.ExamplePlugin/CatAPI.cs index 4fdc631e0..cc49e86dc 100644 --- a/ArchiSteamFarm.CustomPlugins.ExamplePlugin/CatAPI.cs +++ b/ArchiSteamFarm.CustomPlugins.ExamplePlugin/CatAPI.cs @@ -34,32 +34,13 @@ namespace ArchiSteamFarm.CustomPlugins.ExamplePlugin; internal static class CatAPI { private const string URL = "https://aws.random.cat"; - internal static async Task GetRandomCatURL(WebBrowser webBrowser) { + internal static async Task GetRandomCatURL(WebBrowser webBrowser) { ArgumentNullException.ThrowIfNull(webBrowser); Uri request = new($"{URL}/meow"); ObjectResponse? response = await webBrowser.UrlGetToJsonObject(request).ConfigureAwait(false); - if (response?.Content == null) { - return null; - } - - if (string.IsNullOrEmpty(response.Content.Link)) { - throw new InvalidOperationException(nameof(response.Content.Link)); - } - - return Uri.EscapeDataString(response.Content.Link); + return response?.Content?.URL; } - -#pragma warning disable CA1812 // False positive, the class is used during json deserialization - [SuppressMessage("ReSharper", "ClassCannotBeInstantiated")] - private sealed class MeowResponse { - [JsonProperty("file", Required = Required.Always)] - internal readonly string Link = ""; - - [JsonConstructor] - private MeowResponse() { } - } -#pragma warning restore CA1812 // False positive, the class is used during json deserialization } diff --git a/ArchiSteamFarm.CustomPlugins.ExamplePlugin/CatController.cs b/ArchiSteamFarm.CustomPlugins.ExamplePlugin/CatController.cs index 065434e79..f2f52a49a 100644 --- a/ArchiSteamFarm.CustomPlugins.ExamplePlugin/CatController.cs +++ b/ArchiSteamFarm.CustomPlugins.ExamplePlugin/CatController.cs @@ -38,15 +38,15 @@ public sealed class CatController : ArchiController { /// Fetches URL of a random cat picture. /// [HttpGet] - [ProducesResponseType(typeof(GenericResponse), (int) HttpStatusCode.OK)] + [ProducesResponseType(typeof(GenericResponse), (int) HttpStatusCode.OK)] [ProducesResponseType(typeof(GenericResponse), (int) HttpStatusCode.ServiceUnavailable)] public async Task> CatGet() { if (ASF.WebBrowser == null) { throw new InvalidOperationException(nameof(ASF.WebBrowser)); } - string? link = await CatAPI.GetRandomCatURL(ASF.WebBrowser).ConfigureAwait(false); + Uri? url = await CatAPI.GetRandomCatURL(ASF.WebBrowser).ConfigureAwait(false); - return !string.IsNullOrEmpty(link) ? Ok(new GenericResponse(link)) : StatusCode((int) HttpStatusCode.ServiceUnavailable, new GenericResponse(false)); + return url != null ? Ok(new GenericResponse(url)) : StatusCode((int) HttpStatusCode.ServiceUnavailable, new GenericResponse(false)); } } diff --git a/ArchiSteamFarm.CustomPlugins.ExamplePlugin/ExamplePlugin.cs b/ArchiSteamFarm.CustomPlugins.ExamplePlugin/ExamplePlugin.cs index 8110e6ce3..90358a6c6 100644 --- a/ArchiSteamFarm.CustomPlugins.ExamplePlugin/ExamplePlugin.cs +++ b/ArchiSteamFarm.CustomPlugins.ExamplePlugin/ExamplePlugin.cs @@ -92,9 +92,9 @@ internal sealed class ExamplePlugin : IASF, IBot, IBotCommand2, IBotConnection, case "CAT" when access >= EAccess.FamilySharing: // Notice how we can decide whether to use bot's AWH WebBrowser or ASF's one. For Steam-related requests, AWH's one should always be used, for third-party requests like those it doesn't really matter // Still, it makes sense to pass AWH's one, so in case you get some errors or alike, you know from which bot instance they come from. It's similar to using Bot's ArchiLogger compared to ASF's one - string? randomCatURL = await CatAPI.GetRandomCatURL(bot.ArchiWebHandler.WebBrowser).ConfigureAwait(false); + Uri? randomCatURL = await CatAPI.GetRandomCatURL(bot.ArchiWebHandler.WebBrowser).ConfigureAwait(false); - return !string.IsNullOrEmpty(randomCatURL) ? randomCatURL : "God damn it, we're out of cats, care to notify my master? Thanks!"; + return randomCatURL != null ? randomCatURL.ToString() : "God damn it, we're out of cats, care to notify my master? Thanks!"; default: return null; } diff --git a/ArchiSteamFarm.CustomPlugins.ExamplePlugin/MeowResponse.cs b/ArchiSteamFarm.CustomPlugins.ExamplePlugin/MeowResponse.cs new file mode 100644 index 000000000..3583af93c --- /dev/null +++ b/ArchiSteamFarm.CustomPlugins.ExamplePlugin/MeowResponse.cs @@ -0,0 +1,37 @@ +// _ _ _ ____ _ _____ +// / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___ +// / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \ +// / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | | +// /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_| +// | +// Copyright 2015-2022 Ɓ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 System.Diagnostics.CodeAnalysis; +using Newtonsoft.Json; + +namespace ArchiSteamFarm.CustomPlugins.ExamplePlugin; + +#pragma warning disable CA1812 // False positive, the class is used during json deserialization +[SuppressMessage("ReSharper", "ClassCannotBeInstantiated")] +internal sealed class MeowResponse { + [JsonProperty("file", Required = Required.Always)] + internal readonly Uri URL = null!; + + [JsonConstructor] + private MeowResponse() { } +} +#pragma warning restore CA1812 // False positive, the class is used during json deserialization