From afdcd62bf2a063c141ab290825173aa2598dd644 Mon Sep 17 00:00:00 2001 From: JustArchi Date: Sun, 18 Mar 2018 00:12:08 +0100 Subject: [PATCH] Generalize some JSON Steam responses --- ArchiSteamFarm/ArchiWebHandler.cs | 6 ++-- ArchiSteamFarm/Json/Steam.cs | 60 +++++++++++++++++++------------ 2 files changed, 40 insertions(+), 26 deletions(-) diff --git a/ArchiSteamFarm/ArchiWebHandler.cs b/ArchiSteamFarm/ArchiWebHandler.cs index 3ea097d92..ecfe88744 100644 --- a/ArchiSteamFarm/ArchiWebHandler.cs +++ b/ArchiSteamFarm/ArchiWebHandler.cs @@ -760,7 +760,7 @@ namespace ArchiSteamFarm { string request = "/mobileconf/ajaxop?op=" + (accept ? "allow" : "cancel") + "&p=" + deviceID + "&a=" + SteamID + "&k=" + WebUtility.UrlEncode(confirmationHash) + "&t=" + time + "&m=android&tag=conf&cid=" + confirmationID + "&ck=" + confirmationKey; - Steam.ConfirmationResponse response = await UrlGetToJsonObjectWithSession(SteamCommunityURL, request).ConfigureAwait(false); + Steam.BooleanResponse response = await UrlGetToJsonObjectWithSession(SteamCommunityURL, request).ConfigureAwait(false); return response?.Success; } @@ -788,7 +788,7 @@ namespace ArchiSteamFarm { data.Add(new KeyValuePair("ck[]", confirmation.Key.ToString())); } - Steam.ConfirmationResponse response = await UrlPostToJsonObjectWithSession(SteamCommunityURL, request, data).ConfigureAwait(false); + Steam.BooleanResponse response = await UrlPostToJsonObjectWithSession(SteamCommunityURL, request, data).ConfigureAwait(false); return response?.Success; } @@ -1056,7 +1056,7 @@ namespace ArchiSteamFarm { { "communityitemid", itemID.ToString() } }; - Steam.GenericResponse response = await UrlPostToJsonObjectWithSession(SteamCommunityURL, request, data).ConfigureAwait(false); + Steam.EResultResponse response = await UrlPostToJsonObjectWithSession(SteamCommunityURL, request, data).ConfigureAwait(false); return response?.Result == EResult.OK; } diff --git a/ArchiSteamFarm/Json/Steam.cs b/ArchiSteamFarm/Json/Steam.cs index 31f8a36f5..a1ef7a730 100644 --- a/ArchiSteamFarm/Json/Steam.cs +++ b/ArchiSteamFarm/Json/Steam.cs @@ -23,6 +23,7 @@ using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Linq; +using ArchiSteamFarm.Localization; using HtmlAgilityPack; using Newtonsoft.Json; using SteamKit2; @@ -155,10 +156,16 @@ namespace ArchiSteamFarm.Json { } [SuppressMessage("ReSharper", "ClassCannotBeInstantiated")] - internal sealed class ConfirmationDetails { + internal class BooleanResponse { [JsonProperty(PropertyName = "success", Required = Required.Always)] internal readonly bool Success; + // Deserialized from JSON + protected BooleanResponse() { } + } + + [SuppressMessage("ReSharper", "ClassCannotBeInstantiated")] + internal sealed class ConfirmationDetails : BooleanResponse { internal ulong OtherSteamID64 { get { if (_OtherSteamID64 != 0) { @@ -334,25 +341,16 @@ namespace ArchiSteamFarm.Json { } [SuppressMessage("ReSharper", "ClassCannotBeInstantiated")] - internal sealed class ConfirmationResponse { - [JsonProperty(PropertyName = "success", Required = Required.Always)] - internal readonly bool Success; - - // Deserialized from JSON - private ConfirmationResponse() { } - } - - [SuppressMessage("ReSharper", "ClassCannotBeInstantiated")] - internal sealed class GenericResponse { + internal class EResultResponse { [JsonProperty(PropertyName = "success", Required = Required.Always)] internal readonly EResult Result; // Deserialized from JSON - private GenericResponse() { } + protected EResultResponse() { } } [SuppressMessage("ReSharper", "ClassCannotBeInstantiated")] - internal sealed class InventoryResponse { + internal sealed class InventoryResponse : NonZeroResponse { [JsonProperty(PropertyName = "assets", Required = Required.DisallowNull)] internal readonly HashSet Assets; @@ -367,7 +365,6 @@ namespace ArchiSteamFarm.Json { internal ulong LastAssetID { get; private set; } internal bool MoreItems { get; private set; } - internal bool Success { get; private set; } [JsonProperty(PropertyName = "last_assetid", Required = Required.DisallowNull)] private string LastAssetIDString { @@ -391,11 +388,6 @@ namespace ArchiSteamFarm.Json { set => MoreItems = value > 0; } - [JsonProperty(PropertyName = "success", Required = Required.Always)] - private byte SuccessNumber { - set => Success = value > 0; - } - // Deserialized from JSON private InventoryResponse() { } @@ -449,12 +441,34 @@ namespace ArchiSteamFarm.Json { } [SuppressMessage("ReSharper", "ClassCannotBeInstantiated")] - internal sealed class RedeemWalletResponse { - [JsonProperty(PropertyName = "detail", Required = Required.DisallowNull)] - internal readonly EPurchaseResultDetail? PurchaseResultDetail; + internal class NonZeroResponse { + internal bool Success { get; private set; } [JsonProperty(PropertyName = "success", Required = Required.Always)] - internal readonly EResult Result; + private byte SuccessNumber { + set { + switch (value) { + case 0: + Success = false; + break; + case 1: + Success = true; + break; + default: + ASF.ArchiLogger.LogGenericError(string.Format(Strings.WarningUnknownValuePleaseReport, nameof(value), value)); + return; + } + } + } + + // Deserialized from JSON + protected NonZeroResponse() { } + } + + [SuppressMessage("ReSharper", "ClassCannotBeInstantiated")] + internal sealed class RedeemWalletResponse : EResultResponse { + [JsonProperty(PropertyName = "detail", Required = Required.DisallowNull)] + internal readonly EPurchaseResultDetail? PurchaseResultDetail; // Deserialized from JSON private RedeemWalletResponse() { }