Further code cleanup

This commit is contained in:
JustArchi 2021-01-04 17:42:31 +01:00
parent 3d39583fa2
commit 4700ed2706
8 changed files with 60 additions and 170 deletions

View file

@ -40,7 +40,7 @@ namespace ArchiSteamFarm.CustomPlugins.ExamplePlugin {
WebBrowser.ObjectResponse<MeowResponse>? response = await webBrowser.UrlGetToJsonObject<MeowResponse>(request).ConfigureAwait(false);
if (response?.Content == null) {
if (response == null) {
return null;
}

View file

@ -462,13 +462,19 @@ namespace ArchiSteamFarm.OfficialPlugins.SteamTokenDumper {
WebBrowser.ObjectResponse<ResponseData>? response = await ASF.WebBrowser.UrlPostToJsonObject<ResponseData, RequestData>(request, data: requestData, requestOptions: WebBrowser.ERequestOptions.ReturnClientErrors).ConfigureAwait(false);
if ((response?.Content == null) || response.StatusCode.IsClientErrorCode()) {
if (response == null) {
ASF.ArchiLogger.LogGenericWarning(Strings.WarningFailed);
return;
}
if (response.StatusCode.IsClientErrorCode()) {
ASF.ArchiLogger.LogGenericWarning(string.Format(CultureInfo.CurrentCulture, Strings.WarningFailedWithError, response.StatusCode));
#if NETFRAMEWORK
if (response?.StatusCode == (HttpStatusCode) 429) {
if (response.StatusCode == (HttpStatusCode) 429) {
#else
if (response?.StatusCode == HttpStatusCode.TooManyRequests) {
if (response.StatusCode == HttpStatusCode.TooManyRequests) {
#endif
TimeSpan startIn = TimeSpan.FromMinutes(Utilities.RandomNext(SharedInfo.MinimumMinutesBeforeFirstUpload, SharedInfo.MaximumMinutesBeforeFirstUpload));

View file

@ -328,7 +328,7 @@ namespace ArchiSteamFarm {
progressReporter.ProgressChanged -= OnProgressChanged;
}
if (response?.Content == null) {
if (response == null) {
return null;
}

View file

@ -157,7 +157,7 @@ namespace ArchiSteamFarm {
try {
WebBrowser.ObjectResponse<Steam.InventoryResponse>? response = await UrlGetToJsonObjectWithSession<Steam.InventoryResponse>(SteamCommunityURL, request + (startAssetID > 0 ? "&start_assetid=" + startAssetID : "")).ConfigureAwait(false);
if (response?.Content == null) {
if (response == null) {
throw new HttpRequestException(string.Format(CultureInfo.CurrentCulture, Strings.ErrorObjectIsNull, nameof(response)));
}
@ -243,7 +243,7 @@ namespace ArchiSteamFarm {
WebBrowser.XmlDocumentResponse? response = await UrlGetToXmlDocumentWithSession(SteamCommunityURL, request, checkSessionPreemptively: false).ConfigureAwait(false);
using XmlNodeList? xmlNodeList = response?.Content?.SelectNodes("gamesList/games/game");
using XmlNodeList? xmlNodeList = response?.Content.SelectNodes("gamesList/games/game");
if ((xmlNodeList == null) || (xmlNodeList.Count == 0)) {
return null;
@ -453,7 +453,7 @@ namespace ArchiSteamFarm {
}
if (response.StatusCode.IsServerErrorCode()) {
if (string.IsNullOrEmpty(response.Content?.ErrorText)) {
if (string.IsNullOrEmpty(response.Content.ErrorText)) {
// This is a generic server error without a reason, try again
response = null;
@ -467,7 +467,7 @@ namespace ArchiSteamFarm {
}
}
if (response?.Content == null) {
if (response == null) {
return (false, mobileTradeOfferIDs);
}
@ -1295,7 +1295,7 @@ namespace ArchiSteamFarm {
WebBrowser.ObjectResponse<Steam.EResultResponse>? response = await UrlPostToJsonObjectWithSession<Steam.EResultResponse>(SteamStoreURL, request, data: data).ConfigureAwait(false);
if (response?.Content == null) {
if (response == null) {
return false;
}
@ -1332,7 +1332,7 @@ namespace ArchiSteamFarm {
}
if (response.StatusCode.IsServerErrorCode()) {
if (string.IsNullOrEmpty(response.Content?.ErrorText)) {
if (string.IsNullOrEmpty(response.Content.ErrorText)) {
// This is a generic server error without a reason, try again
response = null;
@ -1346,7 +1346,7 @@ namespace ArchiSteamFarm {
}
}
return response?.Content != null ? (true, response.Content.RequiresMobileConfirmation) : (false, false);
return response != null ? (true, response.Content.RequiresMobileConfirmation) : (false, false);
}
internal async Task<bool> AddFreeLicense(uint subID) {
@ -1364,7 +1364,7 @@ namespace ArchiSteamFarm {
using WebBrowser.HtmlDocumentResponse? response = await UrlPostToHtmlDocumentWithSession(SteamStoreURL, request, data: data).ConfigureAwait(false);
return response?.Content?.SelectSingleNode("//div[@class='add_free_content_success_area']") != null;
return response?.Content.SelectSingleNode("//div[@class='add_free_content_success_area']") != null;
}
internal async Task<bool> ChangePrivacySettings(Steam.UserPrivacy userPrivacy) {
@ -1390,7 +1390,7 @@ namespace ArchiSteamFarm {
WebBrowser.ObjectResponse<Steam.EResultResponse>? response = await UrlPostToJsonObjectWithSession<Steam.EResultResponse>(SteamCommunityURL, request, data: data).ConfigureAwait(false);
if (response?.Content == null) {
if (response == null) {
return false;
}
@ -1472,7 +1472,7 @@ namespace ArchiSteamFarm {
WebBrowser.ObjectResponse<Steam.NewDiscoveryQueueResponse>? response = await UrlPostToJsonObjectWithSession<Steam.NewDiscoveryQueueResponse>(SteamStoreURL, request, data: data).ConfigureAwait(false);
return response?.Content?.Queue;
return response?.Content.Queue;
}
internal async Task<HashSet<Steam.TradeOffer>?> GetActiveTradeOffers() {
@ -1776,7 +1776,7 @@ namespace ArchiSteamFarm {
using WebBrowser.HtmlDocumentResponse? response = await UrlGetToHtmlDocumentWithSession(SteamStoreURL, request).ConfigureAwait(false);
if (response?.Content == null) {
if (response == null) {
return null;
}
@ -1826,7 +1826,7 @@ namespace ArchiSteamFarm {
using WebBrowser.HtmlDocumentResponse? response = await UrlGetToHtmlDocumentWithSession(SteamStoreURL, request).ConfigureAwait(false);
if (response?.Content == null) {
if (response == null) {
return null;
}
@ -1919,7 +1919,7 @@ namespace ArchiSteamFarm {
using WebBrowser.HtmlDocumentResponse? response = await UrlGetToHtmlDocumentWithSession(SteamCommunityURL, request).ConfigureAwait(false);
IElement? htmlNode = response?.Content?.SelectSingleNode("//div[@class='pagecontent']/script");
IElement? htmlNode = response?.Content.SelectSingleNode("//div[@class='pagecontent']/script");
if (htmlNode == null) {
// Trade can be no longer valid
@ -2062,7 +2062,7 @@ namespace ArchiSteamFarm {
WebBrowser.ObjectResponse<Steam.BooleanResponse>? response = await UrlGetToJsonObjectWithSession<Steam.BooleanResponse>(SteamCommunityURL, request).ConfigureAwait(false);
return response?.Content?.Success;
return response?.Content.Success;
}
internal async Task<bool?> HandleConfirmations(string deviceID, string confirmationHash, uint time, IReadOnlyCollection<MobileAuthenticator.Confirmation> confirmations, bool accept) {
@ -2116,7 +2116,7 @@ namespace ArchiSteamFarm {
WebBrowser.ObjectResponse<Steam.BooleanResponse>? response = await UrlPostToJsonObjectWithSession<Steam.BooleanResponse>(SteamCommunityURL, request, data: data).ConfigureAwait(false);
return response?.Content?.Success;
return response?.Content.Success;
}
internal async Task<bool> Init(ulong steamID, EUniverse universe, string webAPIUserNonce, string? parentalCode = null) {
@ -2317,15 +2317,15 @@ namespace ArchiSteamFarm {
// Extra entry for sessionID
Dictionary<string, string> data = new(2, StringComparer.Ordinal) { { "wallet_code", key } };
WebBrowser.ObjectResponse<Steam.RedeemWalletResponse>? responseValidateCode = await UrlPostToJsonObjectWithSession<Steam.RedeemWalletResponse>(SteamStoreURL, requestValidateCode, data: data).ConfigureAwait(false);
WebBrowser.ObjectResponse<Steam.RedeemWalletResponse>? response = await UrlPostToJsonObjectWithSession<Steam.RedeemWalletResponse>(SteamStoreURL, requestValidateCode, data: data).ConfigureAwait(false);
if (responseValidateCode?.Content == null) {
if (response == null) {
return null;
}
// We can not trust EResult response, because it is OK even in the case of error, so changing it to Fail in this case
if ((responseValidateCode.Content.Result != EResult.OK) || (responseValidateCode.Content.PurchaseResultDetail != EPurchaseResultDetail.NoDetail)) {
return (responseValidateCode.Content.Result == EResult.OK ? EResult.Fail : responseValidateCode.Content.Result, responseValidateCode.Content.PurchaseResultDetail);
if ((response.Content.Result != EResult.OK) || (response.Content.PurchaseResultDetail != EPurchaseResultDetail.NoDetail)) {
return (response.Content.Result == EResult.OK ? EResult.Fail : response.Content.Result, response.Content.PurchaseResultDetail);
}
return (EResult.OK, EPurchaseResultDetail.NoDetail);
@ -2358,7 +2358,7 @@ namespace ArchiSteamFarm {
WebBrowser.ObjectResponse<Steam.EResultResponse>? response = await UrlPostToJsonObjectWithSession<Steam.EResultResponse>(SteamCommunityURL, request, data: data).ConfigureAwait(false);
return response?.Content?.Result == EResult.OK;
return response?.Content.Result == EResult.OK;
}
private async Task<(ESteamApiKeyState State, string? Key)> GetApiKeyState() {
@ -2366,10 +2366,16 @@ namespace ArchiSteamFarm {
using WebBrowser.HtmlDocumentResponse? response = await UrlGetToHtmlDocumentWithSession(SteamCommunityURL, request).ConfigureAwait(false);
IElement? titleNode = response?.Content?.SelectSingleNode("//div[@id='mainContents']/h2");
if (response == null) {
return (ESteamApiKeyState.Timeout, null);
}
IElement? titleNode = response.Content.SelectSingleNode("//div[@id='mainContents']/h2");
if (titleNode == null) {
return (ESteamApiKeyState.Timeout, null);
Bot.ArchiLogger.LogNullError(nameof(titleNode));
return (ESteamApiKeyState.Error, null);
}
string title = titleNode.TextContent;
@ -2473,7 +2479,7 @@ namespace ArchiSteamFarm {
WebBrowser.BasicResponse? response = await WebLimitRequest(host, async () => await WebBrowser.UrlHead(host + request).ConfigureAwait(false)).ConfigureAwait(false);
if (response?.FinalUri == null) {
if (response == null) {
return null;
}

View file

@ -79,9 +79,9 @@ namespace ArchiSteamFarm {
throw new InvalidOperationException(nameof(ASF.WebBrowser));
}
WebBrowser.ObjectResponse<ReleaseResponse>? objectResponse = await ASF.WebBrowser.UrlGetToJsonObject<ReleaseResponse>(releaseURL).ConfigureAwait(false);
WebBrowser.ObjectResponse<ReleaseResponse>? response = await ASF.WebBrowser.UrlGetToJsonObject<ReleaseResponse>(releaseURL).ConfigureAwait(false);
return objectResponse?.Content;
return response?.Content;
}
private static async Task<ImmutableList<ReleaseResponse>?> GetReleasesFromURL(string releaseURL) {
@ -93,9 +93,9 @@ namespace ArchiSteamFarm {
throw new InvalidOperationException(nameof(ASF.WebBrowser));
}
WebBrowser.ObjectResponse<ImmutableList<ReleaseResponse>>? objectResponse = await ASF.WebBrowser.UrlGetToJsonObject<ImmutableList<ReleaseResponse>>(releaseURL).ConfigureAwait(false);
WebBrowser.ObjectResponse<ImmutableList<ReleaseResponse>>? response = await ASF.WebBrowser.UrlGetToJsonObject<ImmutableList<ReleaseResponse>>(releaseURL).ConfigureAwait(false);
return objectResponse?.Content;
return response?.Content;
}
[SuppressMessage("ReSharper", "ClassCannotBeInstantiated")]

View file

@ -144,7 +144,7 @@ namespace ArchiSteamFarm.IPC.Controllers.Api {
WebBrowser.StringResponse? urlResponse = await ASF.WebBrowser.UrlGetToString(request.URL!).ConfigureAwait(false);
return urlResponse?.Content != null ? Ok(new GenericResponse<string>(urlResponse.Content)) : StatusCode((int) HttpStatusCode.ServiceUnavailable, new GenericResponse(false, string.Format(CultureInfo.CurrentCulture, Strings.ErrorRequestFailedTooManyTimes, WebBrowser.MaxTries)));
return urlResponse != null ? Ok(new GenericResponse<string>(urlResponse.Content)) : StatusCode((int) HttpStatusCode.ServiceUnavailable, new GenericResponse(false, string.Format(CultureInfo.CurrentCulture, Strings.ErrorRequestFailedTooManyTimes, WebBrowser.MaxTries)));
}
}
}

View file

@ -242,9 +242,9 @@ namespace ArchiSteamFarm {
private async Task<ImmutableHashSet<ListedUser>?> GetListedUsers() {
const string request = URL + "/Api/Bots";
WebBrowser.ObjectResponse<ImmutableHashSet<ListedUser>>? objectResponse = await Bot.ArchiWebHandler.WebBrowser.UrlGetToJsonObject<ImmutableHashSet<ListedUser>>(request).ConfigureAwait(false);
WebBrowser.ObjectResponse<ImmutableHashSet<ListedUser>>? response = await Bot.ArchiWebHandler.WebBrowser.UrlGetToJsonObject<ImmutableHashSet<ListedUser>>(request).ConfigureAwait(false);
return objectResponse?.Content;
return response?.Content;
}
private async Task<bool?> IsEligibleForListing() {

View file

@ -126,32 +126,16 @@ namespace ArchiSteamFarm {
continue;
}
bool handleError = false;
if (response.StatusCode.IsClientErrorCode()) {
if (!requestOptions.HasFlag(ERequestOptions.ReturnClientErrors)) {
// We're not handling this error, do not try again
break;
}
handleError = true;
} else if (response.StatusCode.IsServerErrorCode()) {
if (!requestOptions.HasFlag(ERequestOptions.ReturnServerErrors)) {
// We're not handling this error, try again
continue;
}
handleError = true;
}
if (response.Content == null) {
if (handleError) {
// We're handling this error, the content isn't available, deal with it
return new BinaryResponse(response);
}
// The content isn't available and it's not an error, try again
continue;
}
progressReporter?.Report(0);
@ -236,32 +220,16 @@ namespace ArchiSteamFarm {
continue;
}
bool handleError = false;
if (response.StatusCode.IsClientErrorCode()) {
if (!requestOptions.HasFlag(ERequestOptions.ReturnClientErrors)) {
// We're not handling this error, do not try again
break;
}
handleError = true;
} else if (response.StatusCode.IsServerErrorCode()) {
if (!requestOptions.HasFlag(ERequestOptions.ReturnServerErrors)) {
// We're not handling this error, try again
continue;
}
handleError = true;
}
if (response.Content == null) {
if (handleError) {
// We're handling this error, the content isn't available, deal with it
return new HtmlDocumentResponse(response);
}
// The content isn't available and it's not an error, try again
continue;
}
try {
@ -297,32 +265,16 @@ namespace ArchiSteamFarm {
continue;
}
bool handleError = false;
if (response.StatusCode.IsClientErrorCode()) {
if (!requestOptions.HasFlag(ERequestOptions.ReturnClientErrors)) {
// We're not handling this error, do not try again
break;
}
handleError = true;
} else if (response.StatusCode.IsServerErrorCode()) {
if (!requestOptions.HasFlag(ERequestOptions.ReturnServerErrors)) {
// We're not handling this error, try again
continue;
}
handleError = true;
}
if (response.Content == null) {
if (handleError) {
// We're handling this error, the content isn't available, deal with it
return new ObjectResponse<T>(response);
}
// The content isn't available and it's not an error, try again
continue;
}
T? obj;
@ -457,32 +409,16 @@ namespace ArchiSteamFarm {
continue;
}
bool handleError = false;
if (response.StatusCode.IsClientErrorCode()) {
if (!requestOptions.HasFlag(ERequestOptions.ReturnClientErrors)) {
// We're not handling this error, do not try again
break;
}
handleError = true;
} else if (response.StatusCode.IsServerErrorCode()) {
if (!requestOptions.HasFlag(ERequestOptions.ReturnServerErrors)) {
// We're not handling this error, try again
continue;
}
handleError = true;
}
if (response.Content == null) {
if (handleError) {
// We're handling this error, the content isn't available, deal with it
return new XmlDocumentResponse(response);
}
// The content isn't available and it's not an error, try again
continue;
}
XmlDocument xmlDocument = new();
@ -616,32 +552,16 @@ namespace ArchiSteamFarm {
continue;
}
bool handleError = false;
if (response.StatusCode.IsClientErrorCode()) {
if (!requestOptions.HasFlag(ERequestOptions.ReturnClientErrors)) {
// We're not handling this error, do not try again
break;
}
handleError = true;
} else if (response.StatusCode.IsServerErrorCode()) {
if (!requestOptions.HasFlag(ERequestOptions.ReturnServerErrors)) {
// We're not handling this error, try again
continue;
}
handleError = true;
}
if (response.Content == null) {
if (handleError) {
// We're handling this error, the content isn't available, deal with it
return new HtmlDocumentResponse(response);
}
// The content isn't available and it's not an error, try again
continue;
}
try {
@ -677,32 +597,16 @@ namespace ArchiSteamFarm {
continue;
}
bool handleError = false;
if (response.StatusCode.IsClientErrorCode()) {
if (!requestOptions.HasFlag(ERequestOptions.ReturnClientErrors)) {
// We're not handling this error, do not try again
break;
}
handleError = true;
} else if (response.StatusCode.IsServerErrorCode()) {
if (!requestOptions.HasFlag(ERequestOptions.ReturnServerErrors)) {
// We're not handling this error, try again
continue;
}
handleError = true;
}
if (response.Content == null) {
if (handleError) {
// We're handling this error, the content isn't available, deal with it
return new ObjectResponse<TResult>(response);
}
// The content isn't available and it's not an error, try again
continue;
}
TResult? obj;
@ -1012,34 +916,22 @@ namespace ArchiSteamFarm {
public sealed class BinaryResponse : BasicResponse {
[PublicAPI]
public byte[]? Content { get; }
public byte[] Content { get; }
public BinaryResponse(BasicResponse basicResponse, byte[] content) : this(basicResponse) {
public BinaryResponse(BasicResponse basicResponse, byte[] content) : base(basicResponse) {
if (basicResponse == null) {
throw new ArgumentNullException(nameof(basicResponse));
}
Content = content ?? throw new ArgumentNullException(nameof(content));
}
public BinaryResponse(BasicResponse basicResponse) : base(basicResponse) {
if (basicResponse == null) {
throw new ArgumentNullException(nameof(basicResponse));
}
}
}
public sealed class HtmlDocumentResponse : BasicResponse, IDisposable {
[PublicAPI]
public IDocument? Content { get; }
public IDocument Content { get; }
public HtmlDocumentResponse(BasicResponse basicResponse) : base(basicResponse) {
if (basicResponse == null) {
throw new ArgumentNullException(nameof(basicResponse));
}
}
private HtmlDocumentResponse(BasicResponse basicResponse, IDocument content) : this(basicResponse) {
private HtmlDocumentResponse(BasicResponse basicResponse, IDocument content) : base(basicResponse) {
if (basicResponse == null) {
throw new ArgumentNullException(nameof(basicResponse));
}
@ -1047,7 +939,7 @@ namespace ArchiSteamFarm {
Content = content ?? throw new ArgumentNullException(nameof(content));
}
public void Dispose() => Content?.Dispose();
public void Dispose() => Content.Dispose();
[PublicAPI]
public static async Task<HtmlDocumentResponse?> Create(StreamResponse streamResponse) {
@ -1069,28 +961,22 @@ namespace ArchiSteamFarm {
}
}
public sealed class ObjectResponse<T> : BasicResponse where T : class {
public sealed class ObjectResponse<T> : BasicResponse {
[PublicAPI]
public T? Content { get; }
public T Content { get; }
public ObjectResponse(BasicResponse basicResponse, T content) : this(basicResponse) {
public ObjectResponse(BasicResponse basicResponse, T content) : base(basicResponse) {
if (basicResponse == null) {
throw new ArgumentNullException(nameof(basicResponse));
}
Content = content ?? throw new ArgumentNullException(nameof(content));
}
public ObjectResponse(BasicResponse basicResponse) : base(basicResponse) {
if (basicResponse == null) {
throw new ArgumentNullException(nameof(basicResponse));
}
}
}
public sealed class StreamResponse : BasicResponse, IAsyncDisposable {
[PublicAPI]
public Stream? Content { get; }
public Stream Content { get; }
[PublicAPI]
public long Length { get; }
@ -1105,9 +991,7 @@ namespace ArchiSteamFarm {
}
public async ValueTask DisposeAsync() {
if (Content != null) {
await Content.DisposeAsync().ConfigureAwait(false);
}
await Content.DisposeAsync().ConfigureAwait(false);
ResponseMessage.Dispose();
}
@ -1115,7 +999,7 @@ namespace ArchiSteamFarm {
public sealed class StringResponse : BasicResponse {
[PublicAPI]
public string? Content { get; }
public string Content { get; }
internal StringResponse(HttpResponseMessage httpResponseMessage, string content) : base(httpResponseMessage) {
if (httpResponseMessage == null) {
@ -1128,21 +1012,15 @@ namespace ArchiSteamFarm {
public sealed class XmlDocumentResponse : BasicResponse {
[PublicAPI]
public XmlDocument? Content { get; }
public XmlDocument Content { get; }
public XmlDocumentResponse(BasicResponse basicResponse, XmlDocument content) : this(basicResponse) {
public XmlDocumentResponse(BasicResponse basicResponse, XmlDocument content) : base(basicResponse) {
if (basicResponse == null) {
throw new ArgumentNullException(nameof(basicResponse));
}
Content = content ?? throw new ArgumentNullException(nameof(content));
}
public XmlDocumentResponse(BasicResponse basicResponse) : base(basicResponse) {
if (basicResponse == null) {
throw new ArgumentNullException(nameof(basicResponse));
}
}
}
[Flags]