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.
This commit is contained in:
bohd4nx
2026-03-08 04:26:45 +02:00
parent 8cf57b58b7
commit 38a4619e42
5 changed files with 15 additions and 34 deletions
+4 -11
View File
@@ -33,14 +33,12 @@ HEADERS: dict[str, str] = {
async def search_premium_recipient(
client: httpx.AsyncClient,
fragment_hash: str,
cookies: dict,
username: str,
months: int,
) -> str:
resp = await client.post(
f"https://fragment.com/api?hash={fragment_hash}",
headers=HEADERS,
cookies=cookies,
data={"query": username, "months": months, "method": "searchPremiumGiftRecipient"},
)
result = parse_json_response(resp, "searchPremiumGiftRecipient")
@@ -56,14 +54,12 @@ async def search_premium_recipient(
async def init_gift_premium(
client: httpx.AsyncClient,
fragment_hash: str,
cookies: dict,
recipient: str,
months: int,
) -> str:
await client.post(
f"https://fragment.com/api?hash={fragment_hash}",
headers=HEADERS,
cookies=cookies,
data={
"mode": "new",
"lv": "false",
@@ -74,7 +70,6 @@ async def init_gift_premium(
resp = await client.post(
f"https://fragment.com/api?hash={fragment_hash}",
headers=HEADERS,
cookies=cookies,
data={"recipient": recipient, "months": months, "method": "initGiftPremiumRequest"},
)
result = parse_json_response(resp, "initGiftPremiumRequest")
@@ -101,14 +96,12 @@ async def buy_premium(username: str, months: int) -> dict:
# logger.info("Retrieving TON wallet info")
account = await get_account_info()
async with httpx.AsyncClient() as client:
async with httpx.AsyncClient(cookies=cookies) as client:
logger.info("Searching recipient: %s", username)
recipient = await search_premium_recipient(
client, fragment_hash, cookies, username, months
)
recipient = await search_premium_recipient(client, fragment_hash, username, months)
logger.info("Initializing Premium gift request: %s months to %s", months, username)
req_id = await init_gift_premium(client, fragment_hash, cookies, recipient, months)
req_id = await init_gift_premium(client, fragment_hash, recipient, months)
# logger.info("Requesting transaction payload (req_id=%s)", req_id)
tx_data = {
@@ -120,7 +113,7 @@ async def buy_premium(username: str, months: int) -> dict:
"method": "getGiftPremiumLink",
}
transaction = await execute_transaction_request(
client, HEADERS, cookies, account, tx_data, fragment_hash
client, HEADERS, account, tx_data, fragment_hash
)
logger.info("Broadcasting transaction to TON blockchain")
+4 -8
View File
@@ -33,13 +33,11 @@ HEADERS: dict[str, str] = {
async def search_stars_recipient(
client: httpx.AsyncClient,
fragment_hash: str,
cookies: dict,
username: str,
) -> str:
resp = await client.post(
f"https://fragment.com/api?hash={fragment_hash}",
headers=HEADERS,
cookies=cookies,
data={"query": username, "quantity": "", "method": "searchStarsRecipient"},
)
result = parse_json_response(resp, "searchStarsRecipient")
@@ -55,14 +53,12 @@ async def search_stars_recipient(
async def init_buy_stars(
client: httpx.AsyncClient,
fragment_hash: str,
cookies: dict,
recipient: str,
amount: int,
) -> str:
resp = await client.post(
f"https://fragment.com/api?hash={fragment_hash}",
headers=HEADERS,
cookies=cookies,
data={"recipient": recipient, "quantity": amount, "method": "initBuyStarsRequest"},
)
result = parse_json_response(resp, "initBuyStarsRequest")
@@ -89,12 +85,12 @@ async def buy_stars(username: str, amount: int) -> dict:
# logger.info("Retrieving TON wallet info")
account = await get_account_info()
async with httpx.AsyncClient() as client:
async with httpx.AsyncClient(cookies=cookies) as client:
logger.info("Searching recipient: %s", username)
recipient = await search_stars_recipient(client, fragment_hash, cookies, username)
recipient = await search_stars_recipient(client, fragment_hash, username)
logger.info("Initializing Stars purchase request: %s stars to %s", amount, username)
req_id = await init_buy_stars(client, fragment_hash, cookies, recipient, amount)
req_id = await init_buy_stars(client, fragment_hash, recipient, amount)
# logger.info("Requesting transaction payload (req_id=%s)", req_id)
tx_data = {
@@ -106,7 +102,7 @@ async def buy_stars(username: str, amount: int) -> dict:
"method": "getBuyStarsLink",
}
transaction = await execute_transaction_request(
client, HEADERS, cookies, account, tx_data, fragment_hash
client, HEADERS, account, tx_data, fragment_hash
)
logger.info("Broadcasting transaction to TON blockchain")
+4 -9
View File
@@ -26,19 +26,16 @@ HEADERS: dict[str, str] = {
async def search_ads_recipient(
client: httpx.AsyncClient,
fragment_hash: str,
cookies: dict,
username: str,
) -> str:
await client.post(
f"https://fragment.com/api?hash={fragment_hash}",
headers=HEADERS,
cookies=cookies,
data={"mode": "new", "method": "updateAdsTopupState"},
)
resp = await client.post(
f"https://fragment.com/api?hash={fragment_hash}",
headers=HEADERS,
cookies=cookies,
data={"query": username, "method": "searchAdsTopupRecipient"},
)
result = parse_json_response(resp, "searchAdsTopupRecipient")
@@ -54,14 +51,12 @@ async def search_ads_recipient(
async def init_ads_topup(
client: httpx.AsyncClient,
fragment_hash: str,
cookies: dict,
recipient: str,
amount: int,
) -> str:
resp = await client.post(
f"https://fragment.com/api?hash={fragment_hash}",
headers=HEADERS,
cookies=cookies,
data={"recipient": recipient, "amount": amount, "method": "initAdsTopupRequest"},
)
result = parse_json_response(resp, "initAdsTopupRequest")
@@ -88,12 +83,12 @@ async def topup_ton(username: str, amount: int) -> dict:
# logger.info("Retrieving TON wallet info")
account = await get_account_info()
async with httpx.AsyncClient() as client:
async with httpx.AsyncClient(cookies=cookies) as client:
logger.info("Searching recipient: %s", username)
recipient = await search_ads_recipient(client, fragment_hash, cookies, username)
recipient = await search_ads_recipient(client, fragment_hash, username)
logger.info("Initializing topup request: %s TON to %s", amount, username)
req_id = await init_ads_topup(client, fragment_hash, cookies, recipient, amount)
req_id = await init_ads_topup(client, fragment_hash, recipient, amount)
# logger.info("Requesting transaction payload (req_id=%s)", req_id)
tx_data = {
@@ -105,7 +100,7 @@ async def topup_ton(username: str, amount: int) -> dict:
"method": "getAdsTopupLink",
}
transaction = await execute_transaction_request(
client, HEADERS, cookies, account, tx_data, fragment_hash
client, HEADERS, account, tx_data, fragment_hash
)
logger.info("Broadcasting transaction to TON blockchain")
+3 -4
View File
@@ -21,23 +21,22 @@ def parse_json_response(response: httpx.Response, context: str) -> dict[str, Any
async def execute_transaction_request(
client: httpx.AsyncClient,
headers: dict,
cookies: 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, cookies=cookies, data=tx_data)
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, cookies, account, fragment_hash):
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, cookies=cookies, data=tx_data)
resp = await client.post(url, headers=headers, data=tx_data)
transaction = parse_json_response(resp, tx_data.get("method", "transaction"))
return transaction
-2
View File
@@ -83,14 +83,12 @@ async def get_account_info() -> dict[str, Any]:
async def link_wallet(
client: httpx.AsyncClient,
headers: dict,
cookies: dict,
account: dict[str, Any],
fragment_hash: str,
) -> bool:
resp = await client.post(
f"https://fragment.com/api?hash={fragment_hash}",
headers=headers,
cookies=cookies,
data={
"account": json.dumps(account),
"device": DEVICE,