Skip to content

Commit 13f49ab

Browse files
committed
Use async httpx for customhttphandler
1 parent fb45b37 commit 13f49ab

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

app/config.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from pathlib import Path
44
from typing import Any, Optional, Type
55

6+
import anyio
7+
import httpx
68
from git import Repo
79
from pydantic import (
810
DirectoryPath,
@@ -33,6 +35,20 @@ def mapLogRecord(self, record: logging.LogRecord) -> dict[str, str]:
3335
"content": f"API Exception\n\n```\n{self.format(record)}\n```",
3436
}
3537

38+
async def _send(self, payload):
39+
try:
40+
async with httpx.AsyncClient(timeout=5.0) as client:
41+
await client.post(self.url, json=payload)
42+
except Exception as e:
43+
logging.getLogger("fgoapi.webhook").warning(f"Webhook send failed: {e}")
44+
45+
def emit(self, record):
46+
payload = self.mapLogRecord(record)
47+
try:
48+
anyio.from_thread.run(self._send, payload)
49+
except RuntimeError:
50+
anyio.run(self._send, payload)
51+
3652

3753
uvicorn_logger = logging.getLogger("uvicorn.access")
3854
logger = logging.getLogger("fgoapi")

0 commit comments

Comments
 (0)