From d002090bdd5e6ffbbbc1b91f5c9efccfc748609e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matou=C5=A1=20Volf?= Date: Thu, 9 Jun 2022 13:48:21 +0200 Subject: [PATCH] Removed unnecessary `try-catch` blocks --- ClashRoyale.cs | 146 ++++++++++++++++++++++--------------------------- 1 file changed, 66 insertions(+), 80 deletions(-) diff --git a/ClashRoyale.cs b/ClashRoyale.cs index d8961fb..ef3a430 100644 --- a/ClashRoyale.cs +++ b/ClashRoyale.cs @@ -82,28 +82,21 @@ public static bool UseProxyServers private static string GetData(string url) { - try - { - HttpResponseMessage response = httpClient.Send(new HttpRequestMessage(HttpMethod.Get, url)); - StreamReader streamReader = new(response.Content.ReadAsStream()); - string data = streamReader.ReadToEnd(); + HttpResponseMessage response = httpClient.Send(new HttpRequestMessage(HttpMethod.Get, url)); + StreamReader streamReader = new(response.Content.ReadAsStream()); + string data = streamReader.ReadToEnd(); - if (response.StatusCode == HttpStatusCode.Forbidden) - { - throw new InvalidKeyException(); - } - - if (response.StatusCode != HttpStatusCode.OK) - { - return null; - } - - return data; + if (response.StatusCode == HttpStatusCode.Forbidden) + { + throw new InvalidKeyException(); } - catch + + if (response.StatusCode != HttpStatusCode.OK) { - throw; + return null; } + + return data; } internal static DateTime? GetDateTimeFromJson(dynamic json) @@ -268,80 +261,73 @@ public static Clan GetClanByTag(string tag) /// public static Clan[] GetClansBySearch(string name = null, int locationID = 0, int minMembers = 0, int maxMembers = 50, int minScore = 0) { - try - { - string url = clansSearchBaseURL; + string url = clansSearchBaseURL; - if (name is not null) - { - url += "name=" + name + "&"; - } - if (locationID != 0) - { - url += "locationId=" + locationID + "&"; - } - if (minMembers != 0) - { - url += "minMembers=" + minMembers + "&"; - } - if (maxMembers != 50) - { - url += "maxMembers=" + maxMembers + "&"; - } - if (minScore != 0) - { - url += "minScore=" + minScore + "&"; - } - - if (url == clansSearchBaseURL) - { - throw new ArgumentException("At least 1 Clan property must be specified."); - } + if (name is not null) + { + url += "name=" + name + "&"; + } + if (locationID != 0) + { + url += "locationId=" + locationID + "&"; + } + if (minMembers != 0) + { + url += "minMembers=" + minMembers + "&"; + } + if (maxMembers != 50) + { + url += "maxMembers=" + maxMembers + "&"; + } + if (minScore != 0) + { + url += "minScore=" + minScore + "&"; + } - if (name.Length < 3) - { - throw new ArgumentOutOfRangeException(nameof(name), "The Clan's name must be at least 3 characters long."); - } + if (url == clansSearchBaseURL) + { + throw new ArgumentException("At least 1 Clan property must be specified."); + } - if (minMembers < 0) - { - throw new ArgumentOutOfRangeException(nameof(minMembers), "The Clan's minimum member count must be greater than or equal to 0."); - } + if (name.Length < 3) + { + throw new ArgumentOutOfRangeException(nameof(name), "The Clan's name must be at least 3 characters long."); + } - if (minMembers > 50) - { - throw new ArgumentOutOfRangeException(nameof(minMembers), "The Clan's minimum member count must be lower than or equal to 50."); - } + if (minMembers < 0) + { + throw new ArgumentOutOfRangeException(nameof(minMembers), "The Clan's minimum member count must be greater than or equal to 0."); + } - if (maxMembers < 0) - { - throw new ArgumentOutOfRangeException(nameof(maxMembers), "The Clan's maximum member count must be greater than or equal to 0."); - } + if (minMembers > 50) + { + throw new ArgumentOutOfRangeException(nameof(minMembers), "The Clan's minimum member count must be lower than or equal to 50."); + } - if (maxMembers > 50) - { - throw new ArgumentOutOfRangeException(nameof(maxMembers), "The Clan's maximum member count must be lower than or equal to 50."); - } + if (maxMembers < 0) + { + throw new ArgumentOutOfRangeException(nameof(maxMembers), "The Clan's maximum member count must be greater than or equal to 0."); + } - if (minScore < 0) - { - throw new ArgumentOutOfRangeException(nameof(minScore), "The Clan's minimum score must be greater than or equal to 0."); - } + if (maxMembers > 50) + { + throw new ArgumentOutOfRangeException(nameof(maxMembers), "The Clan's maximum member count must be lower than or equal to 50."); + } - string clansData = GetData(url); + if (minScore < 0) + { + throw new ArgumentOutOfRangeException(nameof(minScore), "The Clan's minimum score must be greater than or equal to 0."); + } - if (clansData is null) - { - return null; - } - dynamic clansObject = JObject.Parse(clansData); + string clansData = GetData(url); - return ClashRoyale.GetObjectsFromJson(clansObject.items); - } - catch + if (clansData is null) { - throw; + return null; } + dynamic clansObject = JObject.Parse(clansData); + + return ClashRoyale.GetObjectsFromJson(clansObject.items); } ///