Skip to content

Commit fc5604a

Browse files
committed
Log media requests and raise on bad status
1 parent 8d39c73 commit fc5604a

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## v0.20.4 (unreleased)
2+
3+
* *(client)* Changed media download methods to log requests and to raise
4+
exceptions on non-successful status codes.
5+
16
## v0.20.3 (2023-11-10)
27

38
* *(client)* Deprecated MSC2716 methods and added new Beeper-specific batch

mautrix/api.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ def _log_request(
275275
self,
276276
method: Method,
277277
url: URL,
278-
content: str | bytes | bytearray | AsyncBody,
278+
content: str | bytes | bytearray | AsyncBody | None,
279279
orig_content,
280280
query_params: dict[str, str],
281281
headers: dict[str, str],
@@ -314,7 +314,7 @@ def _log_request(
314314
)
315315

316316
def _log_request_done(
317-
self, path: PathBuilder, req_id: int, duration: float, status: int
317+
self, path: PathBuilder | str, req_id: int, duration: float, status: int
318318
) -> None:
319319
level = 5 if path == Path.v3.sync else 10
320320
duration_str = f"{duration * 1000:.1f}ms" if duration < 1 else f"{duration:.3f}s"
@@ -334,6 +334,16 @@ def _full_path(self, path: PathBuilder | str) -> str:
334334
base_path += "/"
335335
return urllib_join(base_path, path)
336336

337+
def log_download_request(self, url: URL, query_params: dict[str, str]) -> int:
338+
req_id = _next_global_req_id()
339+
self._log_request(Method.GET, url, None, None, query_params, {}, req_id, False)
340+
return req_id
341+
342+
def log_download_request_done(
343+
self, url: URL, req_id: int, duration: float, status: int
344+
) -> None:
345+
self._log_request_done(url.path.removeprefix("/_matrix/media/"), req_id, duration, status)
346+
337347
async def request(
338348
self,
339349
method: Method,

mautrix/client/api/modules/media_repository.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,16 @@ async def download_media(self, url: ContentURI, timeout_ms: int | None = None) -
182182
query_params: dict[str, Any] = {"allow_redirect": "true"}
183183
if timeout_ms is not None:
184184
query_params["timeout_ms"] = timeout_ms
185+
req_id = self.api.log_download_request(url, query_params)
186+
start = time.monotonic()
185187
async with self.api.session.get(url, params=query_params) as response:
186-
return await response.read()
188+
try:
189+
response.raise_for_status()
190+
return await response.read()
191+
finally:
192+
self.api.log_download_request_done(
193+
url, req_id, time.monotonic() - start, response.status
194+
)
187195

188196
async def download_thumbnail(
189197
self,
@@ -227,8 +235,16 @@ async def download_thumbnail(
227235
query_params["allow_remote"] = allow_remote
228236
if timeout_ms is not None:
229237
query_params["timeout_ms"] = timeout_ms
238+
req_id = self.api.log_download_request(url, query_params)
239+
start = time.monotonic()
230240
async with self.api.session.get(url, params=query_params) as response:
231-
return await response.read()
241+
try:
242+
response.raise_for_status()
243+
return await response.read()
244+
finally:
245+
self.api.log_download_request_done(
246+
url, req_id, time.monotonic() - start, response.status
247+
)
232248

233249
async def get_url_preview(self, url: str, timestamp: int | None = None) -> MXOpenGraph:
234250
"""

0 commit comments

Comments
 (0)