PokeAPI / pokepy

A Python wrapper for PokéAPI
https://pokeapi.co
BSD 3-Clause "New" or "Revised" License
127 stars 27 forks source link

Some species returns a 404 when it should not #46

Closed GeoffreyFrogeye closed 4 years ago

GeoffreyFrogeye commented 4 years ago

An example is better than a thousands words: client.get_pokemon_species('leafeon') fails.

$ python3
>>> import pokepy
>>> import coloredlogs
>>> coloredlogs.install(level='DEBUG')
>>> client = pokepy.V2Client()
>>> client.get_pokemon_species('leafeon')
2019-11-19 19:58:47 curacao urllib3.connectionpool[942019] DEBUG Starting new HTTPS connection (1): pokeapi.co:443
2019-11-19 19:58:47 curacao urllib3.connectionpool[942019] DEBUG https://pokeapi.co:443 "GET /api/v2/pokemon-species/leafeon HTTP/1.1" 404 9
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/geoffrey/.local/lib/python3.8/site-packages/pokepy/api.py", line 97, in inner
    return func(*args, **kwargs)
  File "/home/geoffrey/.local/lib/python3.8/site-packages/pokepy/api.py", line 159, in inner
    final = func(*args, **kwargs)
  File "/home/geoffrey/.local/lib/python3.8/site-packages/pokepy/api.py", line 174, in get
    return self.call_api(
  File "/home/geoffrey/.local/lib/python3.8/site-packages/beckett/clients.py", line 99, in call_api
    return self._handle_response(response, valid_status_codes, resource)
  File "/home/geoffrey/.local/lib/python3.8/site-packages/beckett/clients.py", line 114, in _handle_response
    raise InvalidStatusCodeError(
beckett.exceptions.InvalidStatusCodeError: Received status code: 404, expected: (200, 201, 204)
>>> client.get_pokemon_species('leafeon/')
2019-11-19 19:59:05 curacao.geoffrey.frogeye.fr urllib3.connectionpool[942019] DEBUG https://pokeapi.co:443 "GET /api/v2/pokemon-species/leafeon/ HTTP/1.1" 200 4193
<Pokemon_Species - Leafeon>

Just for reference:

>>> client.get_pokemon_species('shaymin')
2019-11-19 20:00:22 curacao urllib3.connectionpool[942019] DEBUG https://pokeapi.co:443 "GET /api/v2/pokemon-species/shaymin HTTP/1.1" 200 3078
<Pokemon_Species - Shaymin>
>>> client.get_pokemon_species('shaymin/')
2019-11-19 20:00:30 curacao urllib3.connectionpool[942019] DEBUG https://pokeapi.co:443 "GET /api/v2/pokemon-species/shaymin/ HTTP/1.1" 200 3078
<Pokemon_Species - Shaymin>

(I couldn't find other examples where it was failing)

At first glance the problem seems that the API have an inconsistent behavior regarding trailing slashes. When can reproduce the same behavior with another browser that does such as curl that does not automatically add trailing slashes. But this issue seems to imply that the client must add the trailing slashes itself, which this does not.

Kronopt commented 4 years ago

I can't reproduce your issue. I tried leafeon as well as other pokemons (with and without trailing slashes) and I'm not getting a 404 error.

What python version are you using? And which pokepy version? Are you running pokepy within a virtual environment or alongside other projects?

GeoffreyFrogeye commented 4 years ago

I just tried again and I can't reproduce this again anymore, even though I did not change anything.

Even curl https://pokeapi.co:443/api/v2/pokemon/leafeon will now output the correct result where it would 404 earlier. Also I noticed that the API is way faster than before (4-5 requests per seconds rather than 1 request per second earlier). Can it be a caching thing? I noticed the API runs on CloudFlare.

It's not really relevant anymore but I tried different combinations with pokepi 8233cbf327650d96ea2940566bc92dc45903e959 and 0.6.0, python 3.7 and 3.8, with pokepi installed in my user directory along other things and in a virtualenv, and from two different IP addresses (although not far from each other). All used to fail before I started writing the issue, now all works.

So if we assume the API won't do that again there's no need to do anything. But this was a bit weird...

Kronopt commented 4 years ago

Definitely weird. Try opening an issue on the pokeapi repo.

I'll close this as it seems to be solved.