Add GET /Api/Structure

This commit is contained in:
JustArchi
2017-12-10 00:01:39 +01:00
parent 35e75cbc1c
commit 1574af29dd
3 changed files with 47 additions and 6 deletions

View File

@@ -138,9 +138,6 @@ namespace ArchiSteamFarm {
[JsonProperty] [JsonProperty]
internal string SteamPassword { get; set; } internal string SteamPassword { get; set; }
// This constructor is used only by deserializer
private BotConfig() { }
public bool ShouldSerializeSteamLogin() => false; public bool ShouldSerializeSteamLogin() => false;
public bool ShouldSerializeSteamParentalPIN() => false; public bool ShouldSerializeSteamParentalPIN() => false;
public bool ShouldSerializeSteamPassword() => false; public bool ShouldSerializeSteamPassword() => false;

View File

@@ -119,9 +119,6 @@ namespace ArchiSteamFarm {
[JsonProperty(Required = Required.DisallowNull)] [JsonProperty(Required = Required.DisallowNull)]
internal ProtocolTypes SteamProtocols { get; private set; } = ProtocolTypes.Tcp; internal ProtocolTypes SteamProtocols { get; private set; } = ProtocolTypes.Tcp;
// This constructor is used only by deserializer
private GlobalConfig() { }
internal static GlobalConfig Load(string filePath) { internal static GlobalConfig Load(string filePath) {
if (string.IsNullOrEmpty(filePath)) { if (string.IsNullOrEmpty(filePath)) {
ASF.ArchiLogger.LogNullError(nameof(filePath)); ASF.ArchiLogger.LogNullError(nameof(filePath));

View File

@@ -115,6 +115,8 @@ namespace ArchiSteamFarm {
case "Command": case "Command":
case "Command/": case "Command/":
return await HandleApiCommand(request, response, arguments, ++argumentsIndex).ConfigureAwait(false); return await HandleApiCommand(request, response, arguments, ++argumentsIndex).ConfigureAwait(false);
case "Structure/":
return await HandleApiStructure(request, response, arguments, ++argumentsIndex).ConfigureAwait(false);
default: default:
return false; return false;
} }
@@ -321,6 +323,51 @@ namespace ArchiSteamFarm {
return true; return true;
} }
private static async Task<bool> 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<bool> 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<bool> HandleAuthenticatedRequest(HttpListenerRequest request, HttpListenerResponse response) { private static async Task<bool> HandleAuthenticatedRequest(HttpListenerRequest request, HttpListenerResponse response) {
if ((request == null) || (response == null)) { if ((request == null) || (response == null)) {
ASF.ArchiLogger.LogNullError(nameof(request) + " || " + nameof(response)); ASF.ArchiLogger.LogNullError(nameof(request) + " || " + nameof(response));