69 lines
3.2 KiB
Python
69 lines
3.2 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import Any, Dict
|
|
|
|
import requests
|
|
|
|
from ._common import ProviderError, extract_code, normalize_status, parse_json_or_text, redact_url_secrets
|
|
|
|
|
|
class SmsVnClient:
|
|
def __init__(self, api_key: str, lang: str = "en", base_url: str = "https://sms-verification-number.com/stubs/handler_api"):
|
|
self.api_key = api_key
|
|
self.lang = lang
|
|
self.base_url = base_url
|
|
|
|
def _call(self, action: str, **params: Any) -> Any:
|
|
q = {"api_key": self.api_key, "action": action, "lang": self.lang, **params}
|
|
try:
|
|
r = requests.get(self.base_url, params=q, timeout=20)
|
|
r.raise_for_status()
|
|
except requests.RequestException as e:
|
|
req = getattr(e, "request", None)
|
|
req_url = getattr(req, "url", "") or (getattr(e, "response", None).url if getattr(e, "response", None) else "")
|
|
safe_url = redact_url_secrets(req_url)
|
|
status = getattr(getattr(e, "response", None), "status_code", None)
|
|
code = f"HTTP_{status}" if status else "REQUEST_ERROR"
|
|
raise ProviderError("smsvn", code, f"{action} request failed", payload={"url": safe_url, "status": status}) from None
|
|
out = parse_json_or_text(r)
|
|
if isinstance(out, str) and out in {
|
|
"NO_BALANCE", "NO_NUMBERS", "BAD_ACTION", "BAD_KEY", "BAD_LANG", "NO_ACTIVATION", "ERROR_SQL", "ERROR_API", "REQUEST_LIMIT",
|
|
}:
|
|
raise ProviderError("smsvn", out, out, payload=out)
|
|
return out
|
|
|
|
def balance(self) -> Dict[str, Any]:
|
|
out = self._call("getBalance")
|
|
return {"provider": "smsvn", "balance": str(out).strip()}
|
|
|
|
def countries(self) -> Any:
|
|
return self._call("getCountryAndOperators")
|
|
|
|
def services(self, country: int) -> Any:
|
|
return self._call("getServicesAndCostWithStatistics", country=country)
|
|
|
|
def get_number(self, service: str, country: int, operator: str = "any", max_price: float | None = None, v2: bool = True) -> Dict[str, Any]:
|
|
action = "getNumberV2" if v2 else "getNumber"
|
|
p: Dict[str, Any] = {"service": service, "country": country, "operator": operator}
|
|
if max_price is not None:
|
|
p["maxPrice"] = max_price
|
|
out = self._call(action, **p)
|
|
if isinstance(out, str) and out.startswith("ACCESS_NUMBER:"):
|
|
_, aid, phone = out.split(":", 2)
|
|
return {"activationId": aid, "phoneNumber": phone, "raw": out}
|
|
if isinstance(out, dict):
|
|
return out
|
|
raise ProviderError("smsvn", "UNEXPECTED", f"unexpected get_number response: {out}", payload=out)
|
|
|
|
def get_status(self, activation_id: str) -> Dict[str, Any]:
|
|
out = self._call("getStatus", id=activation_id)
|
|
raw = str(out)
|
|
code = None
|
|
if raw.startswith("STATUS_OK:"):
|
|
code = raw.split(":", 1)[1]
|
|
return {"provider": "smsvn", "activation_id": activation_id, "status_raw": raw, "status": normalize_status("smsvn", raw), "sms_code": code or extract_code(raw)}
|
|
|
|
def set_status(self, activation_id: str, status: int) -> Dict[str, Any]:
|
|
out = self._call("setStatus", id=activation_id, status=status)
|
|
return {"provider": "smsvn", "activation_id": activation_id, "result": out}
|