From 1574af29dd4921014dd9fc7e40c7252a9b14d67f Mon Sep 17 00:00:00 2001 From: JustArchi Date: Sun, 10 Dec 2017 00:01:39 +0100 Subject: [PATCH] Add GET /Api/Structure --- ArchiSteamFarm/BotConfig.cs | 3 --- ArchiSteamFarm/GlobalConfig.cs | 3 --- ArchiSteamFarm/IPC.cs | 47 ++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 6 deletions(-) diff --git a/ArchiSteamFarm/BotConfig.cs b/ArchiSteamFarm/BotConfig.cs index 71eb376f2..3d912c898 100644 --- a/ArchiSteamFarm/BotConfig.cs +++ b/ArchiSteamFarm/BotConfig.cs @@ -138,9 +138,6 @@ namespace ArchiSteamFarm { [JsonProperty] internal string SteamPassword { get; set; } - // This constructor is used only by deserializer - private BotConfig() { } - public bool ShouldSerializeSteamLogin() => false; public bool ShouldSerializeSteamParentalPIN() => false; public bool ShouldSerializeSteamPassword() => false; diff --git a/ArchiSteamFarm/GlobalConfig.cs b/ArchiSteamFarm/GlobalConfig.cs index 68957f953..00253ebf8 100644 --- a/ArchiSteamFarm/GlobalConfig.cs +++ b/ArchiSteamFarm/GlobalConfig.cs @@ -119,9 +119,6 @@ namespace ArchiSteamFarm { [JsonProperty(Required = Required.DisallowNull)] internal ProtocolTypes SteamProtocols { get; private set; } = ProtocolTypes.Tcp; - // This constructor is used only by deserializer - private GlobalConfig() { } - internal static GlobalConfig Load(string filePath) { if (string.IsNullOrEmpty(filePath)) { ASF.ArchiLogger.LogNullError(nameof(filePath)); diff --git a/ArchiSteamFarm/IPC.cs b/ArchiSteamFarm/IPC.cs index 5075606b0..c78cfb7c5 100644 --- a/ArchiSteamFarm/IPC.cs +++ b/ArchiSteamFarm/IPC.cs @@ -115,6 +115,8 @@ namespace ArchiSteamFarm { case "Command": case "Command/": return await HandleApiCommand(request, response, arguments, ++argumentsIndex).ConfigureAwait(false); + case "Structure/": + return await HandleApiStructure(request, response, arguments, ++argumentsIndex).ConfigureAwait(false); default: return false; } @@ -321,6 +323,51 @@ namespace ArchiSteamFarm { return true; } + private static async Task HandleApiStructure(HttpListenerRequest request, HttpListenerResponse response, string[] arguments, byte argumentsIndex) { + if ((request == null) || (response == null) || (arguments == null) || (argumentsIndex == 0)) { + ASF.ArchiLogger.LogNullError(nameof(request) + " || " + nameof(response) + " || " + nameof(arguments) + " || " + nameof(argumentsIndex)); + return false; + } + + switch (request.HttpMethod) { + case HttpMethods.Get: + return await HandleApiStructureGet(request, response, arguments, argumentsIndex).ConfigureAwait(false); + default: + await ResponseStatusCode(request, response, HttpStatusCode.MethodNotAllowed).ConfigureAwait(false); + return true; + } + } + + private static async Task HandleApiStructureGet(HttpListenerRequest request, HttpListenerResponse response, string[] arguments, byte argumentsIndex) { + if ((request == null) || (response == null) || (arguments == null) || (argumentsIndex == 0)) { + ASF.ArchiLogger.LogNullError(nameof(request) + " || " + nameof(response) + " || " + nameof(arguments) + " || " + nameof(argumentsIndex)); + return false; + } + + if (arguments.Length <= argumentsIndex) { + return false; + } + + string structure = WebUtility.UrlDecode(arguments[argumentsIndex]); + + object obj; + + switch (structure) { + case nameof(BotConfig): + obj = new BotConfig(); + break; + case nameof(GlobalConfig): + obj = new GlobalConfig(); + break; + default: + await ResponseJsonObject(request, response, new GenericResponse(false, string.Format(Strings.ErrorIsInvalid, nameof(structure))), HttpStatusCode.NotAcceptable).ConfigureAwait(false); + return true; + } + + await ResponseJsonObject(request, response, new GenericResponse(true, "OK", obj)).ConfigureAwait(false); + return true; + } + private static async Task HandleAuthenticatedRequest(HttpListenerRequest request, HttpListenerResponse response) { if ((request == null) || (response == null)) { ASF.ArchiLogger.LogNullError(nameof(request) + " || " + nameof(response));