from __future__ import annotations from typing import Any, Dict import requests from ._common import ProviderError, extract_code, normalize_status, parse_json_or_text class FiveSimClient: def __init__(self, token: str, base_url: str = "https://5sim.net"): self.base_url = base_url.rstrip("/") self.s = requests.Session() self.s.headers.update({"Authorization": f"Bearer {token}", "Accept": "application/json"}) def _get(self, path: str, **params: Any) -> Any: r = self.s.get(self.base_url + path, params=params or None, timeout=25) if r.status_code in {401, 403}: raise ProviderError("5sim", "AUTH", f"unauthorized ({r.status_code})", payload=r.text) if r.status_code >= 400: raise ProviderError("5sim", "HTTP", f"http {r.status_code}", payload=r.text) return parse_json_or_text(r) def profile(self) -> Dict[str, Any]: return self._get("/v1/user/profile") def guest_products(self, country: str, operator: str = "any") -> Any: return self._get(f"/v1/guest/products/{country}/{operator}") def guest_prices(self, country: str | None = None, product: str | None = None) -> Any: params: Dict[str, Any] = {} if country: params["country"] = country if product: params["product"] = product return self._get("/v1/guest/prices", **params) def buy_activation(self, country: str, operator: str, product: str, max_price: float | None = None) -> Dict[str, Any]: params: Dict[str, Any] = {} if max_price is not None: params["maxPrice"] = max_price out = self._get(f"/v1/user/buy/activation/{country}/{operator}/{product}", **params) if isinstance(out, dict) and out.get("id"): return out raise ProviderError("5sim", "BUY_FAILED", "unexpected buy response", payload=out) def check(self, order_id: str) -> Dict[str, Any]: out = self._get(f"/v1/user/check/{order_id}") status_raw = out.get("status") if isinstance(out, dict) else "" code = None sms = out.get("sms") if isinstance(out, dict) else None if isinstance(sms, list) and sms: last = sms[-1] or {} code = last.get("code") or extract_code(last.get("text", "")) return {"provider": "5sim", "order": out, "status_raw": status_raw, "status": normalize_status("5sim", status_raw), "sms_code": code} def finish(self, order_id: str) -> Any: return self._get(f"/v1/user/finish/{order_id}") def cancel(self, order_id: str) -> Any: return self._get(f"/v1/user/cancel/{order_id}") def ban(self, order_id: str) -> Any: return self._get(f"/v1/user/ban/{order_id}")