diff --git a/common_lib/services/profile.py b/common_lib/services/profile.py index 7d2b08a..6f97c7a 100644 --- a/common_lib/services/profile.py +++ b/common_lib/services/profile.py @@ -3,7 +3,8 @@ from uuid import UUID from fastapi import HTTPException, status from typing import List, Dict from config import settings -from common_lib.utils.ssl_transport import ssl_transport +# from common_lib.utils.ssl_transport import ssl_transport +from common_lib.utils.http_client import client # async def get_profile_by_user_id(user_id: UUID, token: str) -> dict: @@ -28,21 +29,21 @@ from common_lib.utils.ssl_transport import ssl_transport async def get_profile_by_user_id(user_id: UUID, current_user: UUID, token: str) -> dict: try: - async with httpx.AsyncClient(transport=ssl_transport, timeout=5.0) as client: - response = await client.post( - f"{settings.PROFILE_SERVICE}/user_id/internal", - headers={"Authorization": f"Bearer {token}"}, - json={"user_id": str(user_id), - "current_user": str(current_user)}) + # async with httpx.AsyncClient(transport=ssl_transport, timeout=5.0) as client: + response = await client.post( + f"{settings.PROFILE_SERVICE}/user_id/internal", + headers={"Authorization": f"Bearer {token}"}, + json={"user_id": str(user_id), + "current_user": str(current_user)}) - if response.status_code != 200: - raise HTTPException( - status_code=response.status_code, - detail=f"profile_service: {response.text}" - ) + if response.status_code != 200: + raise HTTPException( + status_code=response.status_code, + detail=f"profile_service: {response.text}" + ) - wrapped = response.json() - return wrapped["data"] + wrapped = response.json() + return wrapped["data"] except httpx.RequestError as e: raise HTTPException(status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail=f"profile_service unreachable: {str(e)}") @@ -50,21 +51,21 @@ async def get_profile_by_user_id(user_id: UUID, current_user: UUID, token: str) async def get_profiles_by_user_ids(user_ids: List[UUID], token: str, user_id: UUID) -> Dict[str, dict]: try: - async with httpx.AsyncClient(transport=ssl_transport, timeout=5.0) as client: - response = await client.post( - f"{settings.PROFILE_SERVICE}/user_ids/internal", - headers={"Authorization": f"Bearer {token}"}, - json={"user_ids": [str(uid) for uid in user_ids], - "user_id": str(user_id)}) + # async with httpx.AsyncClient(transport=ssl_transport, timeout=5.0) as client: + response = await client.post( + f"{settings.PROFILE_SERVICE}/user_ids/internal", + headers={"Authorization": f"Bearer {token}"}, + json={"user_ids": [str(uid) for uid in user_ids], + "user_id": str(user_id)}) - if response.status_code != 200: - raise HTTPException( - status_code=response.status_code, - detail=f"profile_service: {response.text}" - ) + if response.status_code != 200: + raise HTTPException( + status_code=response.status_code, + detail=f"profile_service: {response.text}" + ) - wrapped = response.json() - return wrapped["data"] + wrapped = response.json() + return wrapped["data"] except httpx.RequestError as e: raise HTTPException(status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail=f"profile_service unreachable: {str(e)}") diff --git a/common_lib/utils/auth.py b/common_lib/utils/auth.py index a9b2a48..20b4c74 100644 --- a/common_lib/utils/auth.py +++ b/common_lib/utils/auth.py @@ -5,18 +5,9 @@ from typing import List from dataclasses import dataclass from config import settings -from .ssl_transport import ssl_transport +# from .ssl_transport import ssl_transport from .validators import validate_username as core_validate_username, validate_password as core_validate_password - - -limits = httpx.Limits(max_keepalive_connections=200, max_connections=1000) -timeout = httpx.Timeout(connect=5.0, read=10.0, write=5.0, pool=5.0) - -client = httpx.AsyncClient( - transport=ssl_transport, - limits=limits, - timeout=timeout -) +from common_lib.utils.http_client import client auth_scheme = HTTPBearer() diff --git a/common_lib/utils/http_client.py b/common_lib/utils/http_client.py new file mode 100644 index 0000000..a11378f --- /dev/null +++ b/common_lib/utils/http_client.py @@ -0,0 +1,15 @@ +import httpx +from .ssl_transport import ssl_transport + +# Ограничения пула соединений +limits = httpx.Limits(max_connections=1000, max_keepalive_connections=200) + +# Таймауты +timeout = httpx.Timeout(connect=5.0, read=10.0, write=5.0, pool=5.0) + +# Глобальный клиент +client = httpx.AsyncClient( + transport=ssl_transport, + limits=limits, + timeout=timeout, +) diff --git a/pyproject.toml b/pyproject.toml index 7afd08d..1f8f9a5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "common-lib" -version = "0.0.22" +version = "0.0.23" description = "Библиотека общих компонентов для микросервисов yobble" authors = [{ name = "cheykrym", email = "you@example.com" }] license = "MIT"