From ca6be252ab9fa87a0accf6a78b9262fb142c294d Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 20 Oct 2025 22:30:52 +0300 Subject: [PATCH] add socket_internal --- common_lib/services/socket_internal.py | 41 ++++++++++++++++++++++++++ pyproject.toml | 2 +- 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 common_lib/services/socket_internal.py diff --git a/common_lib/services/socket_internal.py b/common_lib/services/socket_internal.py new file mode 100644 index 0000000..6ce93b0 --- /dev/null +++ b/common_lib/services/socket_internal.py @@ -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)}" diff --git a/pyproject.toml b/pyproject.toml index acd1eba..a395c00 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "common-lib" -version = "0.0.25" +version = "0.0.26" description = "Библиотека общих компонентов для микросервисов yobble" authors = [{ name = "cheykrym", email = "you@example.com" }] license = "MIT"