"""Dynadot 适配器(Domain API v3,API Key 查询参数) API 文档:https://www.dynadot.com/domain/api-commands 所需配置:{"api_key": "..."} 覆盖:注册域名清单(含到期日)、账户余额。 Dynadot 的 DNS 记录通过 set_dns2 的 main_record0..19 位置参数写入, 无法安全地做「按记录增删改」,故不声明 DNS 能力。 """ from datetime import date, datetime, timezone import httpx from app.adapters.base import ( AccountInfo, BaseAdapter, NormalizedDomain, parse_iso_date, ) from app.adapters.registry import register def _epoch_ms_to_date(value) -> date | None: """Dynadot 的 Expiration 是 Unix 毫秒字符串;兼容秒级和 ISO 字符串""" if value in (None, ""): return None text = str(value).strip() try: num = int(float(text)) except ValueError: return parse_iso_date(text) if num > 10_000_000_000: # 毫秒 num //= 1000 try: return datetime.fromtimestamp(num, tz=timezone.utc).date() except (ValueError, OSError, OverflowError): return None def _as_list(value) -> list: if isinstance(value, list): return value if isinstance(value, dict): return [value] return [] @register("dynadot-api") class DynadotAdapter(BaseAdapter): required_config = ["api_key"] BASE = "https://api.dynadot.com/api3.json" def _call(self, command: str, params: dict | None = None) -> dict: query = {"key": self.config.get("api_key", ""), "command": command} if params: query.update({k: v for k, v in params.items() if v is not None}) with httpx.Client(timeout=30) as client: resp = client.get(self.BASE, params=query) resp.raise_for_status() data = resp.json() # 每个命令的根键不同(ListDomainInfoResponse / GetAccountBalanceResponse…) root = next( (v for k, v in data.items() if isinstance(v, dict) and k.endswith("Response")), data if isinstance(data, dict) else {}, ) code = root.get("ResponseCode", root.get("SuccessCode")) status_text = str(root.get("Status") or "").lower() if str(code) != "0" or status_text == "error": reason = root.get("Error") or root.get("Message") or root.get("Status") or "未知错误" raise RuntimeError(f"Dynadot {command} 失败(code={code}):{reason}") return root def test_connection(self) -> dict: try: acc = self.get_account() if acc.balance is None: return {"ok": True, "message": "连接成功(账户未返回余额)"} return {"ok": True, "message": f"连接成功,账户余额 {acc.balance} {acc.currency}"} except httpx.HTTPStatusError as e: return {"ok": False, "message": f"HTTP {e.response.status_code}:API Key 无效"} except Exception as e: # noqa: BLE001 return {"ok": False, "message": str(e)} # ---------------- 只读清单 ---------------- def list_domains(self) -> list: root = self._call("list_domain") result = [] for entry in _as_list(root.get("MainDomains")): # JSON 里可能是 {"Domain": {...}},也可能直接把字段摊平 node = entry.get("Domain") if isinstance(entry.get("Domain"), dict) else entry name = node.get("Name") or entry.get("Name") if not name: continue expiry = _epoch_ms_to_date(node.get("Expiration") or entry.get("Expiration")) status = str(node.get("Status") or entry.get("Status") or "active").lower() result.append( NormalizedDomain( external_id=str(name), domain_name=str(name), registrar="dynadot", expiry_date=expiry, status=status, raw=entry, ) ) return result def get_account(self) -> AccountInfo: root = self._call("get_account_balance") balances = _as_list(root.get("BalanceList")) chosen = next( (b for b in balances if str(b.get("Currency", "")).upper() == "USD"), balances[0] if balances else {}, ) amount = chosen.get("Amount") try: balance = float(str(amount).replace(",", "")) if amount not in (None, "") else None except (TypeError, ValueError): balance = None return AccountInfo( balance=balance, currency=str(chosen.get("Currency") or "USD").upper(), raw=root, )