Skip to content

Commit

Permalink
Add throttling data to message and API (#456)
Browse files Browse the repository at this point in the history
* Add throttling data to message and API

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* workaround a false positive typing check

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
m-aciek and pre-commit-ci[bot] authored Aug 7, 2023
1 parent 088cfe8 commit 4d1c0a7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
19 changes: 17 additions & 2 deletions wlc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import json
import logging
from copy import copy
from typing import Any, ClassVar, Collection, Dict, Optional, Set, Tuple
from typing import Any, ClassVar, Collection, Dict, Optional, Set, Tuple, cast
from urllib.parse import urlencode, urlparse

import dateutil.parser
Expand Down Expand Up @@ -37,6 +37,18 @@ def __init__(self, message: Optional[str] = None):
class WeblateThrottlingError(WeblateException):
"""Throttling on the server."""

def __init__(self, limit: str, retry_after: str):
self.limit = limit
self.retry_after = retry_after
message_segments = [
cast(str, self.__doc__)
] # workaround for https://github.com/python/mypy/issues/15825
if limit:
message_segments.append(f"Limit is {limit} requests.")
if retry_after:
message_segments.append(f"Retry after {retry_after} seconds.")
super().__init__(" ".join(message_segments))


class WeblatePermissionError(WeblateException):
"""You don't have permission to access this object."""
Expand Down Expand Up @@ -114,7 +126,10 @@ def process_error(self, error):
status_code = error.response.status_code

if status_code == 429:
raise WeblateThrottlingError
headers = error.response.headers
raise WeblateThrottlingError(
headers.get("X-RateLimit-Limit"), headers.get("Retry-After")
)
if status_code == 404:
raise WeblateException(
"Object not found on the server "
Expand Down
6 changes: 5 additions & 1 deletion wlc/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,11 @@ def register_uris():
method=responses.POST,
json={"detail": "Can not create components"},
)
register_error("projects/throttled", 429)
register_error(
"projects/throttled",
429,
headers={"X-RateLimit-Limit": "100", "Retry-After": "81818"},
)
register_error("projects/error", 500)
register_error("projects/io", 500, callback=raise_error)
register_error("projects/bug", 500, callback=raise_error)
Expand Down
5 changes: 4 additions & 1 deletion wlc/test_wlc.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ def test_denied_json(self):

def test_throttled(self):
"""Test listing projects."""
with self.assertRaisesRegex(WeblateException, "Throttling"):
with self.assertRaisesRegex(
WeblateException,
"Throttling.*Limit is 100 requests. Retry after 81818 seconds.",
):
Weblate().get_object("throttled")

def test_error(self):
Expand Down

0 comments on commit 4d1c0a7

Please sign in to comment.