Generalize some JSON Steam responses

This commit is contained in:
JustArchi
2018-03-18 00:12:08 +01:00
parent b76ecf5779
commit afdcd62bf2
2 changed files with 40 additions and 26 deletions

View File

@@ -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<Steam.ConfirmationResponse>(SteamCommunityURL, request).ConfigureAwait(false);
Steam.BooleanResponse response = await UrlGetToJsonObjectWithSession<Steam.BooleanResponse>(SteamCommunityURL, request).ConfigureAwait(false);
return response?.Success;
}
@@ -788,7 +788,7 @@ namespace ArchiSteamFarm {
data.Add(new KeyValuePair<string, string>("ck[]", confirmation.Key.ToString()));
}
Steam.ConfirmationResponse response = await UrlPostToJsonObjectWithSession<Steam.ConfirmationResponse>(SteamCommunityURL, request, data).ConfigureAwait(false);
Steam.BooleanResponse response = await UrlPostToJsonObjectWithSession<Steam.BooleanResponse>(SteamCommunityURL, request, data).ConfigureAwait(false);
return response?.Success;
}
@@ -1056,7 +1056,7 @@ namespace ArchiSteamFarm {
{ "communityitemid", itemID.ToString() }
};
Steam.GenericResponse response = await UrlPostToJsonObjectWithSession<Steam.GenericResponse>(SteamCommunityURL, request, data).ConfigureAwait(false);
Steam.EResultResponse response = await UrlPostToJsonObjectWithSession<Steam.EResultResponse>(SteamCommunityURL, request, data).ConfigureAwait(false);
return response?.Result == EResult.OK;
}

View File

@@ -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<Asset> 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() { }