From 697952edede647fa7f9a4b534f32a6416b9ac89e Mon Sep 17 00:00:00 2001 From: JustArchi Date: Sat, 6 Jul 2019 17:56:11 +0200 Subject: [PATCH] Closes #1314 --- .../IPC/Controllers/Api/CommandController.cs | 39 +++++++++++++-- ArchiSteamFarm/IPC/Requests/CommandRequest.cs | 49 +++++++++++++++++++ 2 files changed, 83 insertions(+), 5 deletions(-) create mode 100644 ArchiSteamFarm/IPC/Requests/CommandRequest.cs diff --git a/ArchiSteamFarm/IPC/Controllers/Api/CommandController.cs b/ArchiSteamFarm/IPC/Controllers/Api/CommandController.cs index b8be77da2..4e1d5379b 100644 --- a/ArchiSteamFarm/IPC/Controllers/Api/CommandController.cs +++ b/ArchiSteamFarm/IPC/Controllers/Api/CommandController.cs @@ -23,6 +23,7 @@ using System; using System.Linq; using System.Net; using System.Threading.Tasks; +using ArchiSteamFarm.IPC.Requests; using ArchiSteamFarm.IPC.Responses; using ArchiSteamFarm.Localization; using Microsoft.AspNetCore.Mvc; @@ -37,14 +38,19 @@ namespace ArchiSteamFarm.IPC.Controllers.Api { /// This API endpoint is supposed to be entirely replaced by ASF actions available under /Api/ASF/{action} and /Api/Bot/{bot}/{action}. /// You should use "given bot" commands when executing this endpoint, omitting targets of the command will cause the command to be executed on first defined bot /// - [HttpPost("{command:required}")] + [Consumes("application/json")] + [HttpPost] [ProducesResponseType(typeof(GenericResponse), (int) HttpStatusCode.OK)] [ProducesResponseType(typeof(GenericResponse), (int) HttpStatusCode.BadRequest)] - public async Task> CommandPost(string command) { - if (string.IsNullOrEmpty(command)) { - ASF.ArchiLogger.LogNullError(nameof(command)); + public async Task> CommandPost([FromBody] CommandRequest request) { + if (request == null) { + ASF.ArchiLogger.LogNullError(nameof(request)); - return BadRequest(new GenericResponse(false, string.Format(Strings.ErrorIsEmpty, nameof(command)))); + return BadRequest(new GenericResponse(false, string.Format(Strings.ErrorIsEmpty, nameof(request)))); + } + + if (string.IsNullOrEmpty(request.Command)) { + return BadRequest(new GenericResponse(false, string.Format(Strings.ErrorIsEmpty, nameof(request.Command)))); } if (ASF.GlobalConfig.SteamOwnerID == 0) { @@ -57,6 +63,8 @@ namespace ArchiSteamFarm.IPC.Controllers.Api { return BadRequest(new GenericResponse(false, Strings.ErrorNoBotsDefined)); } + string command = request.Command; + if (!string.IsNullOrEmpty(ASF.GlobalConfig.CommandPrefix) && command.StartsWith(ASF.GlobalConfig.CommandPrefix, StringComparison.Ordinal)) { command = command.Substring(ASF.GlobalConfig.CommandPrefix.Length); @@ -69,5 +77,26 @@ namespace ArchiSteamFarm.IPC.Controllers.Api { return Ok(new GenericResponse(response)); } + + /// + /// Executes a command. + /// + /// + /// This API endpoint is supposed to be entirely replaced by ASF actions available under /Api/ASF/{action} and /Api/Bot/{bot}/{action}. + /// You should use "given bot" commands when executing this endpoint, omitting targets of the command will cause the command to be executed on first defined bot + /// + [HttpPost("{command:required}")] + [Obsolete] + [ProducesResponseType(typeof(GenericResponse), (int) HttpStatusCode.OK)] + [ProducesResponseType(typeof(GenericResponse), (int) HttpStatusCode.BadRequest)] + public async Task> ObsoleteCommandPost(string command) { + if (string.IsNullOrEmpty(command)) { + ASF.ArchiLogger.LogNullError(nameof(command)); + + return BadRequest(new GenericResponse(false, string.Format(Strings.ErrorIsEmpty, nameof(command)))); + } + + return await CommandPost(new CommandRequest(command)).ConfigureAwait(false); + } } } diff --git a/ArchiSteamFarm/IPC/Requests/CommandRequest.cs b/ArchiSteamFarm/IPC/Requests/CommandRequest.cs new file mode 100644 index 000000000..ee59fcf19 --- /dev/null +++ b/ArchiSteamFarm/IPC/Requests/CommandRequest.cs @@ -0,0 +1,49 @@ +// _ _ _ ____ _ _____ +// / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___ +// / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \ +// / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | | +// /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_| +// | +// Copyright 2015-2019 Ɓ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.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; +using JetBrains.Annotations; +using Newtonsoft.Json; + +namespace ArchiSteamFarm.IPC.Requests { + [SuppressMessage("ReSharper", "ClassCannotBeInstantiated")] + public sealed class CommandRequest { + /// + /// Specifies the command that will be executed by ASF. + /// + [JsonProperty(Required = Required.Always)] + [Required] + public readonly string Command; + + internal CommandRequest([NotNull] string command) { + if (string.IsNullOrEmpty(command)) { + throw new ArgumentNullException(nameof(command)); + } + + Command = command; + } + + [JsonConstructor] + private CommandRequest() { } + } +}