mirror of
https://github.com/bohd4nx/FragmentAPI.git
synced 2026-07-25 06:14:29 +00:00
Refactor tonapi module: Move account and transaction logic to services
- Moved account-related functions and classes from `pyfragment.domains.tonapi.account` to `pyfragment.services.tonapi.account`. - Moved transaction-related functions and classes from `pyfragment.domains.tonapi.transaction` to `pyfragment.services.tonapi.transaction`. - Updated imports across the codebase to reflect the new structure. - Removed unused `tonapi` module files and cleaned up related code. - Introduced `ApiProvider` enum to manage API provider types. - Added validation functions for cookies and wallet versions in a new `validation.py` module.
This commit is contained in:
@@ -82,6 +82,3 @@ PREMIUM_MONTHS_VALID: frozenset[int] = frozenset({3, 6, 12})
|
||||
|
||||
# Mnemonic phrase valid word counts
|
||||
MNEMONIC_WORD_COUNTS_VALID: frozenset[int] = frozenset({12, 24})
|
||||
|
||||
# Tonapi API key minimum length (tonapi.io)
|
||||
TONAPI_KEY_MIN_LENGTH: int = 68
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from typing import Any, cast
|
||||
|
||||
from pyfragment.core.constants import MNEMONIC_WORD_COUNTS_VALID, REQUIRED_COOKIE_KEYS
|
||||
from pyfragment.enums import ApiProvider, WalletVersion
|
||||
from pyfragment.exceptions import ConfigurationError, CookieError
|
||||
|
||||
|
||||
def parse_cookies(cookies: dict[str, Any] | str) -> dict[str, Any]:
|
||||
if isinstance(cookies, str):
|
||||
try:
|
||||
cookies = json.loads(cookies)
|
||||
except Exception as exc:
|
||||
raise CookieError(CookieError.READ_FAILED.format(exc=exc)) from exc
|
||||
return cast(dict[str, Any], cookies)
|
||||
|
||||
|
||||
def validate_cookie_keys(cookies: dict[str, Any]) -> None:
|
||||
missing = [k for k in REQUIRED_COOKIE_KEYS if not str(cookies.get(k, "")).strip()]
|
||||
if missing:
|
||||
raise CookieError(CookieError.MISSING_KEYS.format(keys=", ".join(missing)))
|
||||
|
||||
|
||||
def normalize_provider(api_provider: str) -> ApiProvider:
|
||||
try:
|
||||
return ApiProvider(api_provider.strip().lower())
|
||||
except ValueError:
|
||||
raise ConfigurationError(
|
||||
ConfigurationError.UNSUPPORTED_PROVIDER.format(
|
||||
provider=api_provider,
|
||||
supported=", ".join(sorted(p.value for p in ApiProvider)),
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def normalize_wallet_version(wallet_version: str) -> WalletVersion:
|
||||
version = wallet_version.strip().upper()
|
||||
try:
|
||||
return WalletVersion(version)
|
||||
except ValueError:
|
||||
raise ConfigurationError(
|
||||
ConfigurationError.UNSUPPORTED_VERSION.format(
|
||||
version=version,
|
||||
supported=", ".join(sorted(m.value for m in WalletVersion)),
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def validate_credentials(seed: str, api_key: str) -> None:
|
||||
missing = [name for name, val in (("seed", seed), ("api_key", api_key)) if not val or not str(val).strip()]
|
||||
if missing:
|
||||
raise ConfigurationError(ConfigurationError.MISSING_VARS.format(keys=", ".join(missing)))
|
||||
|
||||
word_count = len(seed.split())
|
||||
if word_count not in MNEMONIC_WORD_COUNTS_VALID:
|
||||
raise ConfigurationError(ConfigurationError.INVALID_MNEMONIC.format(count=word_count))
|
||||
Reference in New Issue
Block a user