Код: Выделить всё
from typing import Any, Dict, Optional, Tuple
from fastapi import File
from httpx._client import AsyncClient
async def post(
*,
url: str,
files: Optional[Dict[str, File]] = None,
json: Optional[Dict[str, Any]] = None,
data: Optional[Dict[str, str]] = None,
params: Optional[Dict[str, str]] = None,
timeout: int = 10000
) -> Tuple[bool, Any]:
try:
async with AsyncClient() as client:
response = await client.post(url, files=files, json=json, data=data, timeout=timeout)
response = response.json() if response.status_code == 200 else None
if not response:
return False, None
return True, response
except Exception as e:
print(e)
return False, None
Обновление:
Я попробовал следующий подход с HTTPTransport, но он не помог. у меня не работает:
Код: Выделить всё
from httpx import HTTPTransport # here
try:
async with AsyncClient(transport=transport) as client: # here
response = await client.post(url, files=files, json=json, data=data, timeout=timeout)
response = response.json() if response.status_code == 200 else None
if not response:
return False, None
return True, response
except Exception as e:
print(e)
return False, None
transport = HTTPTransport(retries=3)
'httptransport' объект не имеет атрибута ' aenter '
Подробнее здесь: https://stackoverflow.com/questions/776 ... -in-python