add socket_internal
This commit is contained in:
parent
9b5ffcccce
commit
ca6be252ab
41
common_lib/services/socket_internal.py
Normal file
41
common_lib/services/socket_internal.py
Normal 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)}"
|
||||||
@ -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"
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user