42 lines
1.3 KiB
Python
42 lines
1.3 KiB
Python
import httpx
|
|
from uuid import UUID
|
|
from fastapi import HTTPException, status
|
|
from typing import Dict, Any
|
|
from pydantic import BaseModel
|
|
from config import settings
|
|
from common_lib.utils.http_client import client
|
|
|
|
|
|
async def send_internal_socket_msg(
|
|
user_id: UUID,
|
|
message: Dict[str, Any] | BaseModel,
|
|
event: str,
|
|
with_httpexception = False
|
|
) -> dict:
|
|
if isinstance(message, BaseModel):
|
|
message = message.model_dump(mode="json")
|
|
|
|
try:
|
|
response = await client.post(
|
|
f"{settings.SOCKET_SERVICE}/internal/send_to_user",
|
|
json={"user_id": str(user_id),
|
|
"message": message,
|
|
"event": event
|
|
})
|
|
|
|
if response.status_code != 200:
|
|
if with_httpexception:
|
|
raise HTTPException(
|
|
status_code=response.status_code,
|
|
detail=f"socket_service: {response.text}"
|
|
)
|
|
return response.status_code, f"socket_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"socket_service unreachable: {str(e)}")
|
|
return 503, f"socket_service unreachable: {str(e)}"
|