rhgrant10 / berserk

Python client for the lichess API
https://berserk.readthedocs.io
Other
142 stars 36 forks source link

JSONDecodeError in get_rating_history #11

Closed mykter closed 4 years ago

mykter commented 4 years ago

Description

When retrieving the rating history for a user, which works for many users, hit the following error. You can reproduce with user IDs A23012011 and AboodSalah.

What I Did

    history = client.users.get_rating_history(user)
  File "/home/mike/.local/lib/python3.8/site-packages/berserk/clients.py", line 298, in get_rating_history
    return self._r.get(path, converter=models.RatingHistory.convert)
  File "/home/mike/.local/lib/python3.8/site-packages/berserk/session.py", line 60, in get
    return self.request('GET', *args, **kwargs)
  File "/home/mike/.local/lib/python3.8/site-packages/berserk/session.py", line 56, in request
    return fmt.handle(response, is_stream=is_stream, converter=converter)
  File "/home/mike/.local/lib/python3.8/site-packages/berserk/formats.py", line 35, in handle
    return converter(self.parse(response))
  File "/home/mike/.local/lib/python3.8/site-packages/berserk/formats.py", line 76, in parse
    return response.json(cls=self.decoder)
  File "/home/mike/.local/lib/python3.8/site-packages/requests/models.py", line 898, in json
    return complexjson.loads(self.text, **kwargs)
  File "/usr/lib/python3.8/json/__init__.py", line 370, in loads
    return cls(**kw).decode(s)
  File "/usr/lib/python3.8/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.8/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
rhgrant10 commented 4 years ago

Thanks for reporting this. I'll make some time to investigate it this weekend.

rhgrant10 commented 4 years ago

I looked into this and it turns out the API returns an empty response body if there is no rating history for the user. I'll reach out to the lichess devs to make sure this is intentional and then also handle that situation more gracefully than an obtuse exception.

rhgrant10 commented 4 years ago

They agreed it was a bug. Should be fixed in production soon since it just got committed: https://github.com/ornicar/lila/commit/bea1b527885e06678eb0701540870a2b1a3857f1.

mykter commented 4 years ago

Nice!

rhgrant10 commented 4 years ago

Closing this as fixed!

$ curl -s -H "Accept: application/json" \
          -H "Authorization: Bearer $LICHESS_TOKEN" \
          https://lichess.org/api/user/AboodSalah/rating-history
[]