Skip to content
Snippets Groups Projects

Draft: Resolve "Introduce logging"

Open Jonathan Weth requested to merge 23-introduce-logging into master
2 files
+ 28
1
Compare changes
  • Side-by-side
  • Inline
Files
2
 
import logging
import time
import time
from json import JSONDecodeError
from json import JSONDecodeError
from typing import Any, Optional
from typing import Any, Optional
@@ -28,6 +29,7 @@ def get_headers() -> dict[str, str]:
@@ -28,6 +29,7 @@ def get_headers() -> dict[str, str]:
def do_matrix_request(method: str, url: str, body: Optional[dict] = None) -> dict[str, Any]:
def do_matrix_request(method: str, url: str, body: Optional[dict] = None) -> dict[str, Any]:
"""Do a HTTP request to the Matrix Client Server API."""
"""Do a HTTP request to the Matrix Client Server API."""
 
logging.debug(f"Matrix request: {method} {url} {body}")
while True:
while True:
res = requests.request(method=method, url=build_url(url), headers=get_headers(), json=body) # noqa: S113
res = requests.request(method=method, url=build_url(url), headers=get_headers(), json=body) # noqa: S113
@@ -41,8 +43,11 @@ def do_matrix_request(method: str, url: str, body: Optional[dict] = None) -> dic
@@ -41,8 +43,11 @@ def do_matrix_request(method: str, url: str, body: Optional[dict] = None) -> dic
# If rate limit exceeded, wait and retry
# If rate limit exceeded, wait and retry
if data.get("errcode", "") == "M_LIMIT_EXCEEDED":
if data.get("errcode", "") == "M_LIMIT_EXCEEDED":
time.sleep(data["retry_after_ms"] / 1000)
wait_time = data["retry_after_ms"] / 1000
 
logging.debug(f"Rate limit exceeded, waiting {wait_time} seconds")
 
time.sleep(wait_time)
else:
else:
 
logging.error(f"Matrix request failed: {data}")
raise MatrixException(data)
raise MatrixException(data)
return data
return data
Loading