common_lib/common_lib/services/chat_internal.py
2025-12-02 23:02:38 +03:00

39 lines
1.3 KiB
Python

import httpx
from uuid import UUID
from fastapi import HTTPException, status
from config import settings
from common_lib.utils.http_client import client
async def send_internal_message(
sender: str,
user_id: UUID,
content: str,
broker_customization_msg: str,
with_httpexception = False
) -> dict:
try:
response = await client.post(
f"{settings.CHAT_PRIVATE_SERVICE}/internal/send",
json={"sender": str(sender),
"user_id": str(user_id),
"content": str(content),
"broker_customization_msg": str(broker_customization_msg),
})
if response.status_code != 200:
if with_httpexception:
raise HTTPException(
status_code=response.status_code,
detail=f"chat_private_service: {response.text}"
)
return response.status_code, f"chat_private_service: {response.text}"
wrapped = response.json()
return 200, wrapped
except httpx.RequestError as e:
if with_httpexception:
raise HTTPException(status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail=f"chat_private_service unreachable: {str(e)}")
return 503, f"chat_private_service unreachable: {str(e)}"