feat: harden sms activation skill and add deterministic e2e runner
This commit is contained in:
commit
702280a416
22 files changed
+24363
No files matched your search
@@ -0,0 +1,122 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import time
|
||||
from urllib.parse import parse_qsl, urlencode, urlsplit, urlunsplit
|
||||
from dataclasses import dataclass
|
||||
from typing import Any, Dict, Optional
|
||||
|
||||
import requests
|
||||
|
||||
|
||||
class ProviderError(RuntimeError):
|
||||
def __init__(self, provider: str, code: str, message: str, *, payload: Any = None):
|
||||
super().__init__(f"{provider}:{code}:{message}")
|
||||
self.provider = provider
|
||||
self.code = code
|
||||
self.message = message
|
||||
self.payload = payload
|
||||
|
||||
|
||||
@dataclass
|
||||
class PollConfig:
|
||||
attempts: int = 30
|
||||
interval_sec: float = 3.0
|
||||
backoff: float = 1.1
|
||||
|
||||
|
||||
def now_ts() -> float:
|
||||
return time.time()
|
||||
|
||||
|
||||
def sleep_with_backoff(base: float, factor: float, i: int) -> None:
|
||||
time.sleep(base * (factor ** i))
|
||||
|
||||
|
||||
def parse_json_or_text(resp: requests.Response) -> Any:
|
||||
ctype = resp.headers.get("content-type", "")
|
||||
txt = resp.text
|
||||
if "json" in ctype.lower():
|
||||
return resp.json()
|
||||
try:
|
||||
return resp.json()
|
||||
except Exception:
|
||||
return txt.strip()
|
||||
|
||||
|
||||
def extract_code(text: str) -> Optional[str]:
|
||||
if not text:
|
||||
return None
|
||||
m = re.search(r"(?<!\d)(\d{4,8})(?!\d)", text)
|
||||
return m.group(1) if m else None
|
||||
|
||||
|
||||
def env_first(*names: str) -> Optional[str]:
|
||||
for n in names:
|
||||
v = os.getenv(n)
|
||||
if v:
|
||||
return v
|
||||
return None
|
||||
|
||||
|
||||
def read_state(path: str) -> Dict[str, Any]:
|
||||
if not os.path.exists(path):
|
||||
return {}
|
||||
with open(path, "r", encoding="utf-8") as f:
|
||||
return json.load(f)
|
||||
|
||||
|
||||
def write_state(path: str, state: Dict[str, Any]) -> None:
|
||||
os.makedirs(os.path.dirname(path), exist_ok=True)
|
||||
with open(path, "w", encoding="utf-8") as f:
|
||||
json.dump(state, f, ensure_ascii=False, indent=2)
|
||||
|
||||
|
||||
def mask_secret(value: Optional[str]) -> Optional[str]:
|
||||
if not value:
|
||||
return value
|
||||
if len(value) <= 8:
|
||||
return "*" * len(value)
|
||||
return value[:4] + "..." + value[-4:]
|
||||
|
||||
|
||||
def redact_url_secrets(url: str) -> str:
|
||||
if not url:
|
||||
return url
|
||||
try:
|
||||
parts = urlsplit(url)
|
||||
q = []
|
||||
for k, v in parse_qsl(parts.query, keep_blank_values=True):
|
||||
if k.lower() in {"api_key", "token", "authorization", "auth", "key"}:
|
||||
q.append((k, "***"))
|
||||
else:
|
||||
q.append((k, v))
|
||||
return urlunsplit((parts.scheme, parts.netloc, parts.path, urlencode(q), parts.fragment))
|
||||
except Exception:
|
||||
return url
|
||||
|
||||
|
||||
def normalize_status(provider: str, status_raw: Any, payload: Any = None) -> str:
|
||||
s = str(status_raw or "").upper()
|
||||
if provider in {"smsvn", "grizzly"}:
|
||||
if s.startswith("STATUS_WAIT_CODE") or s in {"WAIT_CODE", "WAITING"}:
|
||||
return "waiting"
|
||||
if s.startswith("STATUS_OK") or "CODE" in s:
|
||||
return "code_received"
|
||||
if "CANCEL" in s:
|
||||
return "canceled"
|
||||
if "ACCESS_ACTIVATION" in s or "FINISH" in s:
|
||||
return "completed"
|
||||
if provider == "5sim":
|
||||
m = {
|
||||
"PENDING": "waiting",
|
||||
"RECEIVED": "code_received",
|
||||
"FINISHED": "completed",
|
||||
"CANCELED": "canceled",
|
||||
"TIMEOUT": "timeout",
|
||||
"BANNED": "banned",
|
||||
}
|
||||
return m.get(s, "error")
|
||||
return "error"
|
||||
Reference in new issue
Block a user