nova-api/api/after_request.py

38 lines
985 B
Python
Raw Normal View History

2023-10-04 23:24:55 +02:00
from db import logs, stats, users
from helpers import network
async def after_request(
2023-11-07 00:56:43 +01:00
incoming_request,
target_request: dict,
user: dict,
2023-10-16 23:34:54 +02:00
tokens: dict,
credits_cost: int,
path: str,
is_chat: bool,
model: str,
2023-10-16 23:34:54 +02:00
provider: str,
) -> None:
2023-10-12 00:03:15 +02:00
"""Runs after every request."""
if user and incoming_request:
2023-10-16 23:34:54 +02:00
await logs.log_api_request(
user=user,
incoming_request=incoming_request,
target_url=target_request['url'],
tokens=tokens,
provider=provider
)
if credits_cost and user:
await users.manager.update_by_id(user['_id'], {'$inc': {'credits': -credits_cost}})
2023-10-16 23:34:54 +02:00
ip_address = network.get_ip(incoming_request)
await stats.manager.add_date()
2023-10-12 00:03:15 +02:00
# await stats.manager.add_ip_address(ip_address)
await stats.manager.add_path(path)
await stats.manager.add_target(target_request['url'])
if is_chat:
await stats.manager.add_model(model)