Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when accessing current war data for clans that haven't participated in wars (for a longer time) #262

Open
Anubhav1603 opened this issue Feb 4, 2025 · 3 comments

Comments

@Anubhav1603
Copy link
Contributor

I'm experiencing an issue where methods like get_current_war and get_clan_war fail if the clan hasn't participated in any wars, even when the war log is set to public. This results in an error when trying to retrieve war data for such clans.

For example, using a valid clan tag like #2PPP that hasn't fought any wars yet leads to an error when calling these methods. It would be great if there could be better handling for such cases, perhaps returning a message like "Not found" or similar when the clan doesn't have any war data.

Current Behavior:

The get_current_war and get_clan_war methods throw an error when the clan hasn't participated in any wars.
Steps to Reproduce:

  1. Use a clan tag of a clan that hasn't participated in any wars (e.g., #2PPP).
  2. Call get_current_war or get_clan_war.
  3. The request fails with an error.
@MagicTheDev
Copy link
Collaborator

#2PPP is a dead clan with zero members

https://api.clashofclans.com/v1/clans/%232PPP

@Anubhav1603
Copy link
Contributor Author

#2PPP is a dead clan with zero members

https://api.clashofclans.com/v1/clans/%232PPP

if i make a new clan and then call this endpoint it will still return the same ig

@doluk
Copy link
Collaborator

doluk commented Feb 4, 2025

the api returns for the clantag you provided:

{
  "state": "notInWar",
  "clan": {
    "badgeUrls": {
      "small": "https://api-assets.clashofclans.com/badges/70/H39b_-WLZGtZVWQ0hqTkE-Tn2AaQnQWy_Iz4yBlvL0M.png",
      "large": "https://api-assets.clashofclans.com/badges/512/H39b_-WLZGtZVWQ0hqTkE-Tn2AaQnQWy_Iz4yBlvL0M.png",
      "medium": "https://api-assets.clashofclans.com/badges/200/H39b_-WLZGtZVWQ0hqTkE-Tn2AaQnQWy_Iz4yBlvL0M.png"
    },
    "clanLevel": 0,
    "attacks": 0,
    "stars": 0,
    "destructionPercentage": 0
  },
  "opponent": {
    "badgeUrls": {
      "small": "https://api-assets.clashofclans.com/badges/70/H39b_-WLZGtZVWQ0hqTkE-Tn2AaQnQWy_Iz4yBlvL0M.png",
      "large": "https://api-assets.clashofclans.com/badges/512/H39b_-WLZGtZVWQ0hqTkE-Tn2AaQnQWy_Iz4yBlvL0M.png",
      "medium": "https://api-assets.clashofclans.com/badges/200/H39b_-WLZGtZVWQ0hqTkE-Tn2AaQnQWy_Iz4yBlvL0M.png"
    },
    "clanLevel": 0,
    "attacks": 0,
    "stars": 0,
    "destructionPercentage": 0
  }
}

with a response status of 200. This is nothing special for new clans. This can be observed for every clan when they don't start a new war and the last war isn't available anymore (normally after around 2-3 days).

Doing the same request with coc.py gives you a war object corresponding to the data, which you can see looking at the war._raw_data

{'state': 'notInWar', 'clan': {'badgeUrls': {'small': 'https://api-assets.clashofclans.com/badges/70/H39b_-WLZGtZVWQ0hqTkE-Tn2AaQnQWy_Iz4yBlvL0M.png', 'large': 'https://api-assets.clashofclans.com/badges/512/H39b_-WLZGtZVWQ0hqTkE-Tn2AaQnQWy_Iz4yBlvL0M.png', 'medium': 'https://api-assets.clashofclans.com/badges/200/H39b_-WLZGtZVWQ0hqTkE-Tn2AaQnQWy_Iz4yBlvL0M.png'}, 'clanLevel': 0, 'attacks': 0, 'stars': 0, 'destructionPercentage': 0.0}, 'opponent': {'badgeUrls': {'small': 'https://api-assets.clashofclans.com/badges/70/H39b_-WLZGtZVWQ0hqTkE-Tn2AaQnQWy_Iz4yBlvL0M.png', 'large': 'https://api-assets.clashofclans.com/badges/512/H39b_-WLZGtZVWQ0hqTkE-Tn2AaQnQWy_Iz4yBlvL0M.png', 'medium': 'https://api-assets.clashofclans.com/badges/200/H39b_-WLZGtZVWQ0hqTkE-Tn2AaQnQWy_Iz4yBlvL0M.png'}, 'clanLevel': 0, 'attacks': 0, 'stars': 0, 'destructionPercentage': 0.0}, 'status_code': 200, 'timestamp': 1738656910.596015, '_response_retry': 120, 'response_time': 110.416983}

The NotFound exception of coc.py corresponds to a 404 response code. While I agree that the way the api returns not in war, changing it simply to a 404 doesn't work. You get a 404 for requesting a non existing clantag. Subsequently your proposal would result in not being able to distinguish between those. It would also make the difference between raw api usage and coc.py bigger. And lastly this would be an extremely breaking change, we couldn't do it without releasing a new major version, which would feel off just for this.

What I normally do, is checking if the war has a preparation_start_time and a war state out of inPreparation, inWar, warEnded as there a some edge cases like the one you found where the api returns data but it isn't normal war data. I would be open to abstract this check into something like a war.is_proper_war.

@doluk doluk changed the title Error when accessing current war data for clans that haven't participated in wars Error when accessing current war data for clans that haven't participated in wars (for a longer time) Feb 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants