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 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)}")
|
||||
|
||||
@ -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()
|
||||
|
||||
|
||||
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]
|
||||
name = "common-lib"
|
||||
version = "0.0.22"
|
||||
version = "0.0.23"
|
||||
description = "Библиотека общих компонентов для микросервисов yobble"
|
||||
authors = [{ name = "cheykrym", email = "you@example.com" }]
|
||||
license = "MIT"
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user