diff --git a/ArchiSteamFarm/ASF.cs b/ArchiSteamFarm/ASF.cs index ac0243d20..fe4547f4c 100644 --- a/ArchiSteamFarm/ASF.cs +++ b/ArchiSteamFarm/ASF.cs @@ -30,7 +30,6 @@ using System.Reflection; using System.Threading; using System.Threading.Tasks; using ArchiSteamFarm.JSON; -using Newtonsoft.Json; namespace ArchiSteamFarm { internal static class ASF { @@ -75,29 +74,16 @@ namespace ArchiSteamFarm { Logging.LogGenericInfo("Checking new version..."); - string response = await Program.WebBrowser.UrlGetToContentRetry(releaseURL).ConfigureAwait(false); - if (string.IsNullOrEmpty(response)) { - Logging.LogGenericWarning("Could not check latest version!"); - return; - } - GitHub.ReleaseResponse releaseResponse; + if (Program.GlobalConfig.UpdateChannel == GlobalConfig.EUpdateChannel.Stable) { - try { - releaseResponse = JsonConvert.DeserializeObject(response); - } catch (JsonException e) { - Logging.LogGenericException(e); + releaseResponse = await Program.WebBrowser.UrlGetToJsonResultRetry(releaseURL).ConfigureAwait(false); + if (releaseResponse == null) { + Logging.LogGenericWarning("Could not check latest version!"); return; } } else { - List releases; - try { - releases = JsonConvert.DeserializeObject>(response); - } catch (JsonException e) { - Logging.LogGenericException(e); - return; - } - + List releases = await Program.WebBrowser.UrlGetToJsonResultRetry>(releaseURL).ConfigureAwait(false); if ((releases == null) || (releases.Count == 0)) { Logging.LogGenericWarning("Could not check latest version!"); return; diff --git a/ArchiSteamFarm/ArchiWebHandler.cs b/ArchiSteamFarm/ArchiWebHandler.cs index 9a740c440..21cb32f70 100644 --- a/ArchiSteamFarm/ArchiWebHandler.cs +++ b/ArchiSteamFarm/ArchiWebHandler.cs @@ -322,22 +322,8 @@ namespace ArchiSteamFarm { string request = SteamCommunityURL + "/mobileconf/details/" + confirmation.ID + "?l=english&p=" + deviceID + "&a=" + SteamID + "&k=" + WebUtility.UrlEncode(confirmationHash) + "&t=" + time + "&m=android&tag=conf"; - string json = await WebBrowser.UrlGetToContentRetry(request).ConfigureAwait(false); - if (string.IsNullOrEmpty(json)) { - return null; - } - - Steam.ConfirmationDetails response; - - try { - response = JsonConvert.DeserializeObject(json); - } catch (JsonException e) { - Logging.LogGenericException(e, Bot.BotName); - return null; - } - + Steam.ConfirmationDetails response = await WebBrowser.UrlGetToJsonResultRetry(request).ConfigureAwait(false); if (response == null) { - Logging.LogNullError(nameof(response), Bot.BotName); return null; } @@ -357,26 +343,8 @@ namespace ArchiSteamFarm { string request = SteamCommunityURL + "/mobileconf/ajaxop?op=" + (accept ? "allow" : "cancel") + "&p=" + deviceID + "&a=" + SteamID + "&k=" + WebUtility.UrlEncode(confirmationHash) + "&t=" + time + "&m=android&tag=conf&cid=" + confirmationID + "&ck=" + confirmationKey; - string json = await WebBrowser.UrlGetToContentRetry(request).ConfigureAwait(false); - if (string.IsNullOrEmpty(json)) { - return null; - } - - Steam.ConfirmationResponse response; - - try { - response = JsonConvert.DeserializeObject(json); - } catch (JsonException e) { - Logging.LogGenericException(e, Bot.BotName); - return null; - } - - if (response != null) { - return response.Success; - } - - Logging.LogNullError(nameof(response), Bot.BotName); - return null; + Steam.ConfirmationResponse response = await WebBrowser.UrlGetToJsonResultRetry(request).ConfigureAwait(false); + return response?.Success; } internal async Task HandleConfirmations(string deviceID, string confirmationHash, uint time, HashSet confirmations, bool accept) { @@ -406,26 +374,8 @@ namespace ArchiSteamFarm { data.Add(new KeyValuePair("ck[]", confirmation.Key.ToString())); } - string json = await WebBrowser.UrlPostToContentRetry(request, data).ConfigureAwait(false); - if (string.IsNullOrEmpty(json)) { - return null; - } - - Steam.ConfirmationResponse response; - - try { - response = JsonConvert.DeserializeObject(json); - } catch (JsonException e) { - Logging.LogGenericException(e, Bot.BotName); - return null; - } - - if (response != null) { - return response.Success; - } - - Logging.LogNullError(nameof(response), Bot.BotName); - return null; + Steam.ConfirmationResponse response = await WebBrowser.UrlPostToJsonResultRetry(request, data).ConfigureAwait(false); + return response?.Success; } internal async Task> GetOwnedGames() { diff --git a/ArchiSteamFarm/WebBrowser.cs b/ArchiSteamFarm/WebBrowser.cs index 380f22867..2c02d91da 100644 --- a/ArchiSteamFarm/WebBrowser.cs +++ b/ArchiSteamFarm/WebBrowser.cs @@ -147,25 +147,6 @@ namespace ArchiSteamFarm { return null; } - internal async Task UrlGetToContentRetry(string request, string referer = null) { - if (string.IsNullOrEmpty(request)) { - Logging.LogNullError(nameof(request), Identifier); - return null; - } - - string result = null; - for (byte i = 0; (i < MaxRetries) && string.IsNullOrEmpty(result); i++) { - result = await UrlGetToContent(request, referer).ConfigureAwait(false); - } - - if (!string.IsNullOrEmpty(result)) { - return result; - } - - Logging.LogGenericWarning("Request failed even after " + MaxRetries + " tries", Identifier); - return null; - } - internal async Task UrlGetToHtmlDocumentRetry(string request, string referer = null) { if (string.IsNullOrEmpty(request)) { Logging.LogNullError(nameof(request), Identifier); @@ -204,6 +185,25 @@ namespace ArchiSteamFarm { return null; } + internal async Task UrlGetToJsonResultRetry(string request, string referer = null) { + if (string.IsNullOrEmpty(request)) { + Logging.LogNullError(nameof(request), Identifier); + return default(T); + } + + string json = await UrlGetToContentRetry(request, referer).ConfigureAwait(false); + if (string.IsNullOrEmpty(json)) { + return default(T); + } + + try { + return JsonConvert.DeserializeObject(json); + } catch (JsonException e) { + Logging.LogGenericException(e, Identifier); + return default(T); + } + } + internal async Task UrlGetToXMLRetry(string request, string referer = null) { if (string.IsNullOrEmpty(request)) { Logging.LogNullError(nameof(request), Identifier); @@ -242,23 +242,23 @@ namespace ArchiSteamFarm { return false; } - internal async Task UrlPostToContentRetry(string request, ICollection> data = null, string referer = null) { + internal async Task UrlPostToJsonResultRetry(string request, ICollection> data = null, string referer = null) { if (string.IsNullOrEmpty(request)) { Logging.LogNullError(nameof(request), Identifier); - return null; + return default(T); } - string result = null; - for (byte i = 0; (i < MaxRetries) && string.IsNullOrEmpty(result); i++) { - result = await UrlPostToContent(request, data, referer).ConfigureAwait(false); + string json = await UrlPostToContentRetry(request, data, referer).ConfigureAwait(false); + if (string.IsNullOrEmpty(json)) { + return default(T); } - if (!string.IsNullOrEmpty(result)) { - return result; + try { + return JsonConvert.DeserializeObject(json); + } catch (JsonException e) { + Logging.LogGenericException(e, Identifier); + return default(T); } - - Logging.LogGenericWarning("Request failed even after " + MaxRetries + " tries", Identifier); - return null; } private async Task UrlGetToBytes(string request, string referer = null) { @@ -291,6 +291,25 @@ namespace ArchiSteamFarm { } } + private async Task UrlGetToContentRetry(string request, string referer = null) { + if (string.IsNullOrEmpty(request)) { + Logging.LogNullError(nameof(request), Identifier); + return null; + } + + string result = null; + for (byte i = 0; (i < MaxRetries) && string.IsNullOrEmpty(result); i++) { + result = await UrlGetToContent(request, referer).ConfigureAwait(false); + } + + if (!string.IsNullOrEmpty(result)) { + return result; + } + + Logging.LogGenericWarning("Request failed even after " + MaxRetries + " tries", Identifier); + return null; + } + private async Task UrlGetToHtmlDocument(string request, string referer = null) { if (string.IsNullOrEmpty(request)) { Logging.LogNullError(nameof(request), Identifier); @@ -419,6 +438,25 @@ namespace ArchiSteamFarm { } } + private async Task UrlPostToContentRetry(string request, ICollection> data = null, string referer = null) { + if (string.IsNullOrEmpty(request)) { + Logging.LogNullError(nameof(request), Identifier); + return null; + } + + string result = null; + for (byte i = 0; (i < MaxRetries) && string.IsNullOrEmpty(result); i++) { + result = await UrlPostToContent(request, data, referer).ConfigureAwait(false); + } + + if (!string.IsNullOrEmpty(result)) { + return result; + } + + Logging.LogGenericWarning("Request failed even after " + MaxRetries + " tries", Identifier); + return null; + } + private async Task UrlPostToResponse(string request, IEnumerable> data = null, string referer = null) { if (!string.IsNullOrEmpty(request)) { return await UrlRequest(request, HttpMethod.Post, data, referer).ConfigureAwait(false);