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

zulip.Client.call_endpoint: Add retry_on_rate_limit_error. #705

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions zulip/zulip/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,7 @@ def call_endpoint(
method: str = "POST",
request: Optional[Dict[str, Any]] = None,
longpolling: bool = False,
retry_on_rate_limit_error: bool = False,
files: Optional[List[IO[Any]]] = None,
timeout: Optional[float] = None,
) -> Dict[str, Any]:
Expand All @@ -707,14 +708,27 @@ def call_endpoint(
if v is not None:
marshalled_request[k] = v
versioned_url = API_VERSTRING + (url if url is not None else "")
return self.do_api_query(
marshalled_request,
versioned_url,
method=method,
longpolling=longpolling,
files=files,
timeout=timeout,
)

while True:
result = self.do_api_query(
marshalled_request,
versioned_url,
method=method,
longpolling=longpolling,
files=files,
timeout=timeout,
)
if (
not retry_on_rate_limit_error
or result["result"] == "success"
or "retry-after" not in result
):
break
wait_before_retry_secs: float = result["retry-after"]
logger.warning("Hit API rate limit, waiting for %f seconds...", wait_before_retry_secs)
time.sleep(wait_before_retry_secs)

return result

def call_on_each_event(
self,
Expand Down