Skip to content

Commit

Permalink
Merge pull request #13 from KevinBanana/Handle-lichess-offline-error
Browse files Browse the repository at this point in the history
Handle lichess offline error
  • Loading branch information
cyanfish committed May 18, 2021
2 parents 035f5c3 + 741a2cc commit 8deea5d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -102,3 +102,6 @@ venv.bak/

# mypy
.mypy_cache/

# IDE
.idea
12 changes: 12 additions & 0 deletions lichess/api.py
Expand Up @@ -71,10 +71,15 @@ def call(self, path, params=None, post_data=None, auth=None, format=lichess.form
resp = requests.post(url, params=params, data=post_data, headers=headers, cookies=cookies, stream=stream)
else:
resp = requests.get(url, params, headers=headers, cookies=cookies, stream=stream)

if resp.status_code == 429:
self.on_rate_limit(url, retry_count)
time.sleep(60)
retry_count += 1
elif resp.status_code == 502 or resp.status_code == 503:
self.on_api_down(retry_count)
time.sleep(60)
retry_count += 1
else:
break

Expand All @@ -91,6 +96,13 @@ def on_rate_limit(self, url, retry_count):
if self.max_retries != -1 and retry_count >= self.max_retries:
raise ApiError('Max retries exceeded')

def on_api_down(self, retry_count):
"""A handler called when HTTP 502 or HTTP 503 is received.
Raises an exception when :data:`~lichess.api.DefaultApiClient.max_retries` is exceeded.
"""
if self.max_retries != -1 and retry_count >= self.max_retries:
raise ApiError('Max retries exceeded')

default_client = DefaultApiClient()
"""The client object used to communicate with the lichess API.
Expand Down

0 comments on commit 8deea5d

Please sign in to comment.