68 lines
3.2 KiB
Python
68 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 GrizzlyClient:
|
|
def __init__(self, api_key: str, base_url: str = "https://api.grizzlysms.com/stubs/handler_api.php", lang: str = "en"):
|
|
self.api_key = api_key
|
|
self.base_url = base_url
|
|
self.lang = lang
|
|
|
|
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("grizzly", 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 {"BAD_KEY", "NO_NUMBERS", "SERVICE_UNAVAILABLE_REGION", "BAD_ACTION", "NO_ACTIVATION", "REQUEST_LIMIT"}:
|
|
raise ProviderError("grizzly", out, out, payload=out)
|
|
return out
|
|
|
|
def balance(self) -> Dict[str, Any]:
|
|
out = self._call("getBalance")
|
|
return {"provider": "grizzly", "balance": str(out)}
|
|
|
|
def get_number(
|
|
self,
|
|
service: str,
|
|
country: int,
|
|
max_price: float | None = None,
|
|
provider_ids: str | None = None,
|
|
except_provider_ids: str | None = None,
|
|
v2: bool = True,
|
|
) -> Dict[str, Any]:
|
|
action = "getNumberV2" if v2 else "getNumber"
|
|
p: Dict[str, Any] = {"service": service, "country": country}
|
|
if max_price is not None: p["maxPrice"] = max_price
|
|
if provider_ids: p["providerIds"] = provider_ids
|
|
if except_provider_ids: p["exceptProviderIds"] = except_provider_ids
|
|
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("grizzly", "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 = raw.split(":", 1)[1] if raw.startswith("STATUS_OK:") else extract_code(raw)
|
|
return {"provider": "grizzly", "activation_id": activation_id, "status_raw": raw, "status": normalize_status("grizzly", raw), "sms_code": code}
|
|
|
|
def set_status(self, activation_id: str, status: int) -> Dict[str, Any]:
|
|
out = self._call("setStatus", id=activation_id, status=status)
|
|
return {"provider": "grizzly", "activation_id": activation_id, "result": out}
|