Skip to content

Commit

Permalink
Removed unnecessary try-catch blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
matous-volf committed Jun 9, 2022
1 parent 1d994a5 commit d002090
Showing 1 changed file with 66 additions and 80 deletions.
146 changes: 66 additions & 80 deletions ClashRoyale.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -268,80 +261,73 @@ public static Clan GetClanByTag(string tag)
/// </returns>
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<Clan>(clansObject.items);
}
catch
if (clansData is null)
{
throw;
return null;
}
dynamic clansObject = JObject.Parse(clansData);

return ClashRoyale.GetObjectsFromJson<Clan>(clansObject.items);
}

/// <summary>
Expand Down

0 comments on commit d002090

Please sign in to comment.