Skip to content
Snippets Groups Projects
Verified Commit b9b30782 authored by Jonathan Weth's avatar Jonathan Weth :keyboard:
Browse files

Restructure do_matrix_request to simplify it

parent 2a429deb
No related branches found
No related tags found
1 merge request!2Resolve "Review tasks"
Pipeline #55922 failed
......@@ -28,18 +28,19 @@ def do_matrix_request(method: str, url: str, body: Optional[dict] = None) -> Dic
"""Do a HTTP request to the Matrix Client Server API."""
while True:
res = requests.request(method=method, url=build_url(url), headers=get_headers(), json=body)
if res.status_code != requests.codes.ok:
try:
data = res.json()
except JSONDecodeError:
raise MatrixException(res.text)
# If rate limit exceeded, wait and retry
if data.get("errcode", "") == "M_LIMIT_EXCEEDED":
time.sleep(data["retry_after_ms"] / 1000)
else:
raise MatrixException(data)
else:
try:
data = res.json()
except JSONDecodeError:
raise MatrixException(res.text) from JSONDecodeError
if res.status_code == requests.codes.ok:
break
return res.json()
# If rate limit exceeded, wait and retry
if data.get("errcode", "") == "M_LIMIT_EXCEEDED":
time.sleep(data["retry_after_ms"] / 1000)
else:
raise MatrixException(data)
return data
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment