From 38a4619e42b48b7e1aa473786a7255f4c2cd2696 Mon Sep 17 00:00:00 2001 From: bohd4nx Date: Sun, 8 Mar 2026 04:26:45 +0200 Subject: [PATCH] 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. --- app/methods/premium.py | 15 ++++----------- app/methods/stars.py | 12 ++++-------- app/methods/ton.py | 13 ++++--------- app/utils/client.py | 7 +++---- app/utils/wallet.py | 2 -- 5 files changed, 15 insertions(+), 34 deletions(-) diff --git a/app/methods/premium.py b/app/methods/premium.py index 9d68d9c..bea566b 100644 --- a/app/methods/premium.py +++ b/app/methods/premium.py @@ -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") diff --git a/app/methods/stars.py b/app/methods/stars.py index 97d1f60..fa3d507 100644 --- a/app/methods/stars.py +++ b/app/methods/stars.py @@ -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") diff --git a/app/methods/ton.py b/app/methods/ton.py index 79ed10a..bba62d0 100644 --- a/app/methods/ton.py +++ b/app/methods/ton.py @@ -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") diff --git a/app/utils/client.py b/app/utils/client.py index 8627390..434da6c 100644 --- a/app/utils/client.py +++ b/app/utils/client.py @@ -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 diff --git a/app/utils/wallet.py b/app/utils/wallet.py index a19af69..5c71ab3 100644 --- a/app/utils/wallet.py +++ b/app/utils/wallet.py @@ -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,