Init
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
import json
|
||||
import uuid
|
||||
|
||||
from datetime import datetime, UTC
|
||||
from starlette.responses import StreamingResponse
|
||||
|
||||
from .db.db import get_db
|
||||
from .models import ApiLog
|
||||
|
||||
|
||||
def write_log(req: Request, res: StreamingResponse, req_body : dict, res_body: str, process_time : float):
|
||||
db = next(get_db())
|
||||
|
||||
try:
|
||||
res_body = json.loads(res_body)
|
||||
except Exception:
|
||||
res_body = None
|
||||
|
||||
client_ip = req.client.host
|
||||
if client_ip == 'testclient':
|
||||
client_ip = '127.0.0.1'
|
||||
|
||||
log = ApiLog(
|
||||
api_key = uuid.UUID(req.headers.get("x-api-key")) if req.headers.get("x-api-key") else None,
|
||||
ip_address = client_ip,
|
||||
path = req.url.path,
|
||||
method = req.method,
|
||||
status_code = res.status_code,
|
||||
request_body = req_body,
|
||||
response_body = res_body,
|
||||
query_params = dict(req.query_params),
|
||||
path_params = req.path_params,
|
||||
process_time = process_time,
|
||||
created_at = datetime.now(UTC)
|
||||
)
|
||||
|
||||
db.add(log)
|
||||
db.commit()
|
||||
db.close()
|
||||
Reference in New Issue
Block a user