Files
FragmentAPI/app/utils/client.py
T
bohd4nx 38a4619e42 fix: pass cookies to AsyncClient instead of per-request
Fixes httpx DeprecationWarning: Setting per-request cookies is deprecated.
Cookies are now set on the AsyncClient instance and removed from all
individual post() calls and internal function signatures.
2026-03-08 04:26:45 +02:00

43 lines
1.3 KiB
Python

import logging
from typing import Any
import httpx
from app.core import RequestError, WalletError
from app.utils.wallet import link_wallet
logger = logging.getLogger(__name__)
def parse_json_response(response: httpx.Response, context: str) -> dict[str, Any]:
try:
return response.json()
except Exception as exc:
raise RequestError(
f"Fragment API returned an unparseable response for '{context}': {exc}"
) from exc
async def execute_transaction_request(
client: httpx.AsyncClient,
headers: dict,
account: dict[str, Any],
tx_data: dict[str, Any],
fragment_hash: str,
) -> dict[str, Any]:
url = f"https://fragment.com/api?hash={fragment_hash}"
resp = await client.post(url, headers=headers, data=tx_data)
transaction = parse_json_response(resp, tx_data.get("method", "transaction"))
if transaction.get("need_verify"):
if not await link_wallet(client, headers, account, fragment_hash):
raise WalletError(
"Failed to link your TON wallet to Fragment. "
"Make sure the wallet matching your cookies is used."
)
resp = await client.post(url, headers=headers, data=tx_data)
transaction = parse_json_response(resp, tx_data.get("method", "transaction"))
return transaction