add socket_internal

This commit is contained in:
unknown 2025-10-20 22:30:52 +03:00
parent 9b5ffcccce
commit ca6be252ab
2 changed files with 42 additions and 1 deletions

View File

@ -0,0 +1,41 @@
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()
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 wrapped["data"]
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)}"

View File

@ -1,6 +1,6 @@
[project] [project]
name = "common-lib" name = "common-lib"
version = "0.0.25" version = "0.0.26"
description = "Библиотека общих компонентов для микросервисов yobble" description = "Библиотека общих компонентов для микросервисов yobble"
authors = [{ name = "cheykrym", email = "you@example.com" }] authors = [{ name = "cheykrym", email = "you@example.com" }]
license = "MIT" license = "MIT"