45 lines
1.4 KiB
Python
45 lines
1.4 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
import httpx
|
|
|
|
from ..settings import settings
|
|
|
|
|
|
class WolfApiService:
|
|
"""Talk to the in-cluster proxy for Wolf's local management API."""
|
|
|
|
def enabled(self) -> bool:
|
|
return bool(settings.wolf_api_url)
|
|
|
|
def _request(self, method: str, path: str, payload: dict[str, Any] | None = None) -> dict[str, Any]:
|
|
if not self.enabled():
|
|
raise RuntimeError("wolf api not configured")
|
|
url = f"{settings.wolf_api_url.rstrip('/')}/{path.lstrip('/')}"
|
|
with httpx.Client(timeout=settings.wolf_api_timeout_sec) as client:
|
|
resp = client.request(method, url, json=payload)
|
|
resp.raise_for_status()
|
|
data = resp.json()
|
|
if not isinstance(data, dict):
|
|
raise RuntimeError("unexpected wolf api response")
|
|
return data
|
|
|
|
def clients(self) -> dict[str, Any]:
|
|
return self._request("GET", "/api/v1/clients")
|
|
|
|
def pending_pair_requests(self) -> dict[str, Any]:
|
|
return self._request("GET", "/api/v1/pair/pending")
|
|
|
|
def pair_client(self, pair_secret: str, pin: str) -> dict[str, Any]:
|
|
return self._request("POST", "/api/v1/pair/client", {"pair_secret": pair_secret, "pin": pin})
|
|
|
|
def sessions(self) -> dict[str, Any]:
|
|
return self._request("GET", "/api/v1/sessions")
|
|
|
|
def apps(self) -> dict[str, Any]:
|
|
return self._request("GET", "/api/v1/apps")
|
|
|
|
|
|
wolf_api = WolfApiService()
|