mirror of
https://github.com/bohd4nx/FragmentAPI.git
synced 2026-07-25 14:24:31 +00:00
228fbfcd1c
- 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.
59 lines
2.1 KiB
Python
59 lines
2.1 KiB
Python
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))
|