mirror of
https://github.com/bohd4nx/FragmentAPI.git
synced 2026-08-04 18:43:25 +00:00
feat: Refactor configuration handling and enhance logging setup; update imports and improve error handling in API methods
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
from app.utils.client import ApiClient
|
||||
from app.utils.client import ApiClient, parse_json_response
|
||||
from app.utils.decoder import clean_decode
|
||||
from app.utils.transaction import TransactionProcessor
|
||||
from app.utils.wallet import WalletLinker
|
||||
|
||||
__all__ = ['TransactionProcessor', 'WalletLinker', 'ApiClient', 'clean_decode']
|
||||
__all__ = ['TransactionProcessor', 'WalletLinker', 'ApiClient', 'clean_decode', 'parse_json_response']
|
||||
|
||||
+25
-7
@@ -1,18 +1,36 @@
|
||||
from typing import Dict, Any, Tuple
|
||||
from typing import Any
|
||||
import logging
|
||||
|
||||
import httpx
|
||||
|
||||
from app.core import config
|
||||
|
||||
|
||||
def parse_json_response(
|
||||
response: httpx.Response,
|
||||
logger: logging.Logger,
|
||||
context: str,
|
||||
) -> tuple[dict[str, Any] | None, str | None]:
|
||||
try:
|
||||
return response.json(), None
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to parse {context} response: {e}")
|
||||
logger.error(f"Response content: {response.content[:200]}")
|
||||
return None, str(e)
|
||||
|
||||
|
||||
class ApiClient:
|
||||
def __init__(self, config: dict, headers: dict, wallet_linker):
|
||||
self.config = config
|
||||
def __init__(self, headers: dict, wallet_linker):
|
||||
self.headers = headers
|
||||
self.wallet_linker = wallet_linker
|
||||
|
||||
async def execute_transaction_request(self, tx_data: Dict[str, Any], account: Dict[str, Any]) -> Tuple[
|
||||
bool, Dict[str, Any]]:
|
||||
async def execute_transaction_request(
|
||||
self,
|
||||
tx_data: dict[str, Any],
|
||||
account: dict[str, Any],
|
||||
) -> tuple[bool, dict[str, Any]]:
|
||||
async with httpx.AsyncClient() as client:
|
||||
tx_resp = await client.post(f"https://fragment.com/api?hash={self.config['hash']}",
|
||||
tx_resp = await client.post(f"https://fragment.com/api?hash={config.HASH}",
|
||||
headers=self.headers, data=tx_data)
|
||||
transaction = tx_resp.json()
|
||||
|
||||
@@ -20,7 +38,7 @@ class ApiClient:
|
||||
if not await self.wallet_linker.link_wallet(account):
|
||||
return False, {"success": False, "error": "Failed to link wallet"}
|
||||
|
||||
tx_resp = await client.post(f"https://fragment.com/api?hash={self.config['hash']}",
|
||||
tx_resp = await client.post(f"https://fragment.com/api?hash={config.HASH}",
|
||||
headers=self.headers, data=tx_data)
|
||||
transaction = tx_resp.json()
|
||||
|
||||
|
||||
+1
-10
@@ -28,7 +28,7 @@ def clean_decode(payload: str) -> str:
|
||||
# 4. Remove binary characters, keep only printable + whitespace
|
||||
t = ''.join(c for c in t if c in string.printable or c.isspace())
|
||||
|
||||
# 5. Extract main text with Ref#
|
||||
# 5. Extract the main text with Ref#
|
||||
match = re.search(r'([0-9]*\s*Telegram .*?Ref#[A-Za-z0-9]+)', t, re.S)
|
||||
if match:
|
||||
result = match.group(1).strip()
|
||||
@@ -38,12 +38,3 @@ def clean_decode(payload: str) -> str:
|
||||
logger.debug(f"Cleaned result: {result}")
|
||||
|
||||
return result
|
||||
|
||||
# payloads = [
|
||||
# "te6ccgEBAgEALwABTgAAAAAxMDAwMDAwIFRlbGVncmFtIFN0YXJzIAoKUmVmI1RQb01wegEABkM3ZQ",
|
||||
# "te6ccgEBAgEANAABTgAAAABUZWxlZ3JhbSBQcmVtaXVtIGZvciAxIHllYXIgCgpSZWYjcgEAEE9OQnM2cmNt",
|
||||
# "te6ccgEBAgEAMAABTgAAAABUZWxlZ3JhbSBhY2NvdW50IHRvcCB1cCAKClJlZiNrMXpDRQEACFkxd3g"
|
||||
# ]
|
||||
|
||||
# for p in payloads:
|
||||
# logger.debug("\n" + clean_decode(p) + "\n")
|
||||
|
||||
@@ -1,24 +1,25 @@
|
||||
import logging
|
||||
from typing import Tuple, Optional
|
||||
|
||||
from tonutils.client import TonapiClient
|
||||
from tonutils.wallet import WalletV5R1
|
||||
from tonutils.wallet.messages import TransferMessage
|
||||
|
||||
|
||||
from app.core import config
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class TransactionProcessor:
|
||||
def __init__(self, config: dict, clean_decode_func):
|
||||
self.config = config
|
||||
def __init__(self, clean_decode_func):
|
||||
self._clean_decode = clean_decode_func
|
||||
|
||||
async def process_transaction(self, transaction_data: dict) -> Tuple[bool, Optional[str], Optional[str]]:
|
||||
async def process_transaction(self, transaction_data: dict) -> tuple[bool, str | None, str | None]:
|
||||
if "transaction" not in transaction_data or "messages" not in transaction_data["transaction"]:
|
||||
return False, "Invalid transaction", None
|
||||
|
||||
client = TonapiClient(api_key=self.config['api_key'], is_testnet=False)
|
||||
wallet, _, _, _ = WalletV5R1.from_mnemonic(client=client, mnemonic=self.config['seed'])
|
||||
client = TonapiClient(api_key=config.API_KEY, is_testnet=False)
|
||||
wallet, _, _, _ = WalletV5R1.from_mnemonic(client=client, mnemonic=config.SEED)
|
||||
|
||||
try:
|
||||
message = transaction_data["transaction"]["messages"][0]
|
||||
|
||||
+6
-5
@@ -1,15 +1,16 @@
|
||||
from typing import Dict, Any
|
||||
from typing import Any
|
||||
|
||||
import httpx
|
||||
|
||||
from app.core import config
|
||||
|
||||
|
||||
class WalletLinker:
|
||||
def __init__(self, config: dict, headers: dict, transaction_processor):
|
||||
self.config = config
|
||||
def __init__(self, headers: dict, transaction_processor):
|
||||
self.headers = headers
|
||||
self.transaction_processor = transaction_processor
|
||||
|
||||
async def link_wallet(self, account: Dict[str, Any]) -> bool:
|
||||
async def link_wallet(self, account: dict[str, Any]) -> bool:
|
||||
async with httpx.AsyncClient() as client:
|
||||
data = {
|
||||
'account': account,
|
||||
@@ -17,7 +18,7 @@ class WalletLinker:
|
||||
'method': 'linkWallet'
|
||||
}
|
||||
|
||||
response = await client.post(f"https://fragment.com/api?hash={self.config['hash']}",
|
||||
response = await client.post(f"https://fragment.com/api?hash={config.HASH}",
|
||||
headers=self.headers, data=data)
|
||||
result = response.json()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user