Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
matous-volf committed Jun 9, 2022
2 parents 4010546 + 70d44b3 commit 1d994a5
Showing 1 changed file with 80 additions and 66 deletions.
146 changes: 80 additions & 66 deletions ClashRoyale.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,21 +82,28 @@ public static bool UseProxyServers

private static string GetData(string url)
{
HttpResponseMessage response = httpClient.Send(new HttpRequestMessage(HttpMethod.Get, url));
StreamReader streamReader = new(response.Content.ReadAsStream());
string data = streamReader.ReadToEnd();

if (response.StatusCode == HttpStatusCode.Forbidden)
try
{
throw new InvalidKeyException();
}
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)
if (response.StatusCode != HttpStatusCode.OK)
{
return null;
}

return data;
}
catch
{
return null;
throw;
}

return data;
}

internal static DateTime? GetDateTimeFromJson(dynamic json)
Expand Down Expand Up @@ -261,73 +268,80 @@ 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)
{
string url = clansSearchBaseURL;

if (name is not null)
{
url += "name=" + name + "&";
}
if (locationID != 0)
{
url += "locationId=" + locationID + "&";
}
if (minMembers != 0)
try
{
url += "minMembers=" + minMembers + "&";
}
if (maxMembers != 50)
{
url += "maxMembers=" + maxMembers + "&";
}
if (minScore != 0)
{
url += "minScore=" + minScore + "&";
}
string url = clansSearchBaseURL;

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.");
}

if (minScore < 0)
{
throw new ArgumentOutOfRangeException(nameof(minScore), "The Clan's minimum score must be greater than or equal to 0.");
}

string clansData = GetData(url);
string clansData = GetData(url);

if (clansData is null)
{
return null;
}
dynamic clansObject = JObject.Parse(clansData);

if (clansData is null)
return ClashRoyale.GetObjectsFromJson<Clan>(clansObject.items);
}
catch
{
return null;
throw;
}
dynamic clansObject = JObject.Parse(clansData);

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

/// <summary>
Expand Down

0 comments on commit 1d994a5

Please sign in to comment.