From 4700ed27063a69bf3b77ef397a1a094461ced100 Mon Sep 17 00:00:00 2001 From: JustArchi Date: Mon, 4 Jan 2021 17:42:31 +0100 Subject: [PATCH] Further code cleanup --- .../CatAPI.cs | 2 +- .../SteamTokenDumperPlugin.cs | 12 +- ArchiSteamFarm/ASF.cs | 2 +- ArchiSteamFarm/ArchiWebHandler.cs | 52 +++--- ArchiSteamFarm/GitHub.cs | 8 +- .../IPC/Controllers/Api/WWWController.cs | 2 +- ArchiSteamFarm/Statistics.cs | 4 +- ArchiSteamFarm/WebBrowser.cs | 148 ++---------------- 8 files changed, 60 insertions(+), 170 deletions(-) diff --git a/ArchiSteamFarm.CustomPlugins.ExamplePlugin/CatAPI.cs b/ArchiSteamFarm.CustomPlugins.ExamplePlugin/CatAPI.cs index 812e3ba2c..23c9d9eb8 100644 --- a/ArchiSteamFarm.CustomPlugins.ExamplePlugin/CatAPI.cs +++ b/ArchiSteamFarm.CustomPlugins.ExamplePlugin/CatAPI.cs @@ -40,7 +40,7 @@ namespace ArchiSteamFarm.CustomPlugins.ExamplePlugin { WebBrowser.ObjectResponse? response = await webBrowser.UrlGetToJsonObject(request).ConfigureAwait(false); - if (response?.Content == null) { + if (response == null) { return null; } diff --git a/ArchiSteamFarm.OfficialPlugins.SteamTokenDumper/SteamTokenDumperPlugin.cs b/ArchiSteamFarm.OfficialPlugins.SteamTokenDumper/SteamTokenDumperPlugin.cs index 0bcf305a4..a71ea07c3 100644 --- a/ArchiSteamFarm.OfficialPlugins.SteamTokenDumper/SteamTokenDumperPlugin.cs +++ b/ArchiSteamFarm.OfficialPlugins.SteamTokenDumper/SteamTokenDumperPlugin.cs @@ -462,13 +462,19 @@ namespace ArchiSteamFarm.OfficialPlugins.SteamTokenDumper { WebBrowser.ObjectResponse? response = await ASF.WebBrowser.UrlPostToJsonObject(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)); diff --git a/ArchiSteamFarm/ASF.cs b/ArchiSteamFarm/ASF.cs index 057776a19..484e44983 100644 --- a/ArchiSteamFarm/ASF.cs +++ b/ArchiSteamFarm/ASF.cs @@ -328,7 +328,7 @@ namespace ArchiSteamFarm { progressReporter.ProgressChanged -= OnProgressChanged; } - if (response?.Content == null) { + if (response == null) { return null; } diff --git a/ArchiSteamFarm/ArchiWebHandler.cs b/ArchiSteamFarm/ArchiWebHandler.cs index ff73b2eff..9dcbd6097 100644 --- a/ArchiSteamFarm/ArchiWebHandler.cs +++ b/ArchiSteamFarm/ArchiWebHandler.cs @@ -157,7 +157,7 @@ namespace ArchiSteamFarm { try { WebBrowser.ObjectResponse? response = await UrlGetToJsonObjectWithSession(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? response = await UrlPostToJsonObjectWithSession(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 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 ChangePrivacySettings(Steam.UserPrivacy userPrivacy) { @@ -1390,7 +1390,7 @@ namespace ArchiSteamFarm { WebBrowser.ObjectResponse? response = await UrlPostToJsonObjectWithSession(SteamCommunityURL, request, data: data).ConfigureAwait(false); - if (response?.Content == null) { + if (response == null) { return false; } @@ -1472,7 +1472,7 @@ namespace ArchiSteamFarm { WebBrowser.ObjectResponse? response = await UrlPostToJsonObjectWithSession(SteamStoreURL, request, data: data).ConfigureAwait(false); - return response?.Content?.Queue; + return response?.Content.Queue; } internal async Task?> 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? response = await UrlGetToJsonObjectWithSession(SteamCommunityURL, request).ConfigureAwait(false); - return response?.Content?.Success; + return response?.Content.Success; } internal async Task HandleConfirmations(string deviceID, string confirmationHash, uint time, IReadOnlyCollection confirmations, bool accept) { @@ -2116,7 +2116,7 @@ namespace ArchiSteamFarm { WebBrowser.ObjectResponse? response = await UrlPostToJsonObjectWithSession(SteamCommunityURL, request, data: data).ConfigureAwait(false); - return response?.Content?.Success; + return response?.Content.Success; } internal async Task Init(ulong steamID, EUniverse universe, string webAPIUserNonce, string? parentalCode = null) { @@ -2317,15 +2317,15 @@ namespace ArchiSteamFarm { // Extra entry for sessionID Dictionary data = new(2, StringComparer.Ordinal) { { "wallet_code", key } }; - WebBrowser.ObjectResponse? responseValidateCode = await UrlPostToJsonObjectWithSession(SteamStoreURL, requestValidateCode, data: data).ConfigureAwait(false); + WebBrowser.ObjectResponse? response = await UrlPostToJsonObjectWithSession(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? response = await UrlPostToJsonObjectWithSession(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; } diff --git a/ArchiSteamFarm/GitHub.cs b/ArchiSteamFarm/GitHub.cs index 8f7353fa0..f388410be 100644 --- a/ArchiSteamFarm/GitHub.cs +++ b/ArchiSteamFarm/GitHub.cs @@ -79,9 +79,9 @@ namespace ArchiSteamFarm { throw new InvalidOperationException(nameof(ASF.WebBrowser)); } - WebBrowser.ObjectResponse? objectResponse = await ASF.WebBrowser.UrlGetToJsonObject(releaseURL).ConfigureAwait(false); + WebBrowser.ObjectResponse? response = await ASF.WebBrowser.UrlGetToJsonObject(releaseURL).ConfigureAwait(false); - return objectResponse?.Content; + return response?.Content; } private static async Task?> GetReleasesFromURL(string releaseURL) { @@ -93,9 +93,9 @@ namespace ArchiSteamFarm { throw new InvalidOperationException(nameof(ASF.WebBrowser)); } - WebBrowser.ObjectResponse>? objectResponse = await ASF.WebBrowser.UrlGetToJsonObject>(releaseURL).ConfigureAwait(false); + WebBrowser.ObjectResponse>? response = await ASF.WebBrowser.UrlGetToJsonObject>(releaseURL).ConfigureAwait(false); - return objectResponse?.Content; + return response?.Content; } [SuppressMessage("ReSharper", "ClassCannotBeInstantiated")] diff --git a/ArchiSteamFarm/IPC/Controllers/Api/WWWController.cs b/ArchiSteamFarm/IPC/Controllers/Api/WWWController.cs index b1152a1c4..be480f71b 100644 --- a/ArchiSteamFarm/IPC/Controllers/Api/WWWController.cs +++ b/ArchiSteamFarm/IPC/Controllers/Api/WWWController.cs @@ -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(urlResponse.Content)) : StatusCode((int) HttpStatusCode.ServiceUnavailable, new GenericResponse(false, string.Format(CultureInfo.CurrentCulture, Strings.ErrorRequestFailedTooManyTimes, WebBrowser.MaxTries))); + return urlResponse != null ? Ok(new GenericResponse(urlResponse.Content)) : StatusCode((int) HttpStatusCode.ServiceUnavailable, new GenericResponse(false, string.Format(CultureInfo.CurrentCulture, Strings.ErrorRequestFailedTooManyTimes, WebBrowser.MaxTries))); } } } diff --git a/ArchiSteamFarm/Statistics.cs b/ArchiSteamFarm/Statistics.cs index 363f78937..dffd87d5e 100644 --- a/ArchiSteamFarm/Statistics.cs +++ b/ArchiSteamFarm/Statistics.cs @@ -242,9 +242,9 @@ namespace ArchiSteamFarm { private async Task?> GetListedUsers() { const string request = URL + "/Api/Bots"; - WebBrowser.ObjectResponse>? objectResponse = await Bot.ArchiWebHandler.WebBrowser.UrlGetToJsonObject>(request).ConfigureAwait(false); + WebBrowser.ObjectResponse>? response = await Bot.ArchiWebHandler.WebBrowser.UrlGetToJsonObject>(request).ConfigureAwait(false); - return objectResponse?.Content; + return response?.Content; } private async Task IsEligibleForListing() { diff --git a/ArchiSteamFarm/WebBrowser.cs b/ArchiSteamFarm/WebBrowser.cs index 44164d931..a9f0d908c 100644 --- a/ArchiSteamFarm/WebBrowser.cs +++ b/ArchiSteamFarm/WebBrowser.cs @@ -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(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(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 Create(StreamResponse streamResponse) { @@ -1069,28 +961,22 @@ namespace ArchiSteamFarm { } } - public sealed class ObjectResponse : BasicResponse where T : class { + public sealed class ObjectResponse : 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]