fix http
This commit is contained in:
parent
c261b07ff1
commit
f46f3904e5
@ -3,7 +3,8 @@ from uuid import UUID
|
|||||||
from fastapi import HTTPException, status
|
from fastapi import HTTPException, status
|
||||||
from typing import List, Dict
|
from typing import List, Dict
|
||||||
from config import settings
|
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:
|
# 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:
|
async def get_profile_by_user_id(user_id: UUID, current_user: UUID, token: str) -> dict:
|
||||||
try:
|
try:
|
||||||
async with httpx.AsyncClient(transport=ssl_transport, timeout=5.0) as client:
|
# async with httpx.AsyncClient(transport=ssl_transport, timeout=5.0) as client:
|
||||||
response = await client.post(
|
response = await client.post(
|
||||||
f"{settings.PROFILE_SERVICE}/user_id/internal",
|
f"{settings.PROFILE_SERVICE}/user_id/internal",
|
||||||
headers={"Authorization": f"Bearer {token}"},
|
headers={"Authorization": f"Bearer {token}"},
|
||||||
json={"user_id": str(user_id),
|
json={"user_id": str(user_id),
|
||||||
"current_user": str(current_user)})
|
"current_user": str(current_user)})
|
||||||
|
|
||||||
if response.status_code != 200:
|
if response.status_code != 200:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=response.status_code,
|
status_code=response.status_code,
|
||||||
detail=f"profile_service: {response.text}"
|
detail=f"profile_service: {response.text}"
|
||||||
)
|
)
|
||||||
|
|
||||||
wrapped = response.json()
|
wrapped = response.json()
|
||||||
return wrapped["data"]
|
return wrapped["data"]
|
||||||
|
|
||||||
except httpx.RequestError as e:
|
except httpx.RequestError as e:
|
||||||
raise HTTPException(status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail=f"profile_service unreachable: {str(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]:
|
async def get_profiles_by_user_ids(user_ids: List[UUID], token: str, user_id: UUID) -> Dict[str, dict]:
|
||||||
try:
|
try:
|
||||||
async with httpx.AsyncClient(transport=ssl_transport, timeout=5.0) as client:
|
# async with httpx.AsyncClient(transport=ssl_transport, timeout=5.0) as client:
|
||||||
response = await client.post(
|
response = await client.post(
|
||||||
f"{settings.PROFILE_SERVICE}/user_ids/internal",
|
f"{settings.PROFILE_SERVICE}/user_ids/internal",
|
||||||
headers={"Authorization": f"Bearer {token}"},
|
headers={"Authorization": f"Bearer {token}"},
|
||||||
json={"user_ids": [str(uid) for uid in user_ids],
|
json={"user_ids": [str(uid) for uid in user_ids],
|
||||||
"user_id": str(user_id)})
|
"user_id": str(user_id)})
|
||||||
|
|
||||||
if response.status_code != 200:
|
if response.status_code != 200:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=response.status_code,
|
status_code=response.status_code,
|
||||||
detail=f"profile_service: {response.text}"
|
detail=f"profile_service: {response.text}"
|
||||||
)
|
)
|
||||||
|
|
||||||
wrapped = response.json()
|
wrapped = response.json()
|
||||||
return wrapped["data"]
|
return wrapped["data"]
|
||||||
|
|
||||||
except httpx.RequestError as e:
|
except httpx.RequestError as e:
|
||||||
raise HTTPException(status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail=f"profile_service unreachable: {str(e)}")
|
raise HTTPException(status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail=f"profile_service unreachable: {str(e)}")
|
||||||
|
|||||||
@ -5,18 +5,9 @@ from typing import List
|
|||||||
|
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from config import settings
|
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
|
from .validators import validate_username as core_validate_username, validate_password as core_validate_password
|
||||||
|
from common_lib.utils.http_client import client
|
||||||
|
|
||||||
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
|
|
||||||
)
|
|
||||||
|
|
||||||
auth_scheme = HTTPBearer()
|
auth_scheme = HTTPBearer()
|
||||||
|
|
||||||
|
|||||||
15
common_lib/utils/http_client.py
Normal file
15
common_lib/utils/http_client.py
Normal file
@ -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,
|
||||||
|
)
|
||||||
@ -1,6 +1,6 @@
|
|||||||
[project]
|
[project]
|
||||||
name = "common-lib"
|
name = "common-lib"
|
||||||
version = "0.0.22"
|
version = "0.0.23"
|
||||||
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