68 lines
2.1 KiB
Python
68 lines
2.1 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import Any, TYPE_CHECKING
|
|
|
|
import httpx
|
|
|
|
from yuxi.channels.exceptions import ChannelConnectionError
|
|
from yuxi.utils.logging_config import logger
|
|
|
|
if TYPE_CHECKING:
|
|
from .client import UrbitClient
|
|
|
|
|
|
_SCRY_BASE = "/~/scry"
|
|
|
|
|
|
async def scry_get(client: UrbitClient, app: str, path: str) -> Any:
|
|
url = f"{_SCRY_BASE}/{app}{path}"
|
|
try:
|
|
r = await client.get(url, timeout=15.0)
|
|
if r.status_code == 400:
|
|
logger.warning(f"[Urbit] Scry {url} returned 400 (likely no data)")
|
|
return None
|
|
if r.status_code == 404:
|
|
logger.debug(f"[Urbit] Scry {url} not found")
|
|
return None
|
|
r.raise_for_status()
|
|
return r.json()
|
|
except httpx.HTTPStatusError as e:
|
|
logger.warning(f"[Urbit] Scry {url} failed: {e.response.status_code}")
|
|
return None
|
|
except httpx.RequestError as e:
|
|
raise ChannelConnectionError(f"Scry request failed: {e}") from e
|
|
|
|
|
|
async def scry_contacts(client: UrbitClient) -> dict[str, Any] | None:
|
|
return await scry_get(client, "contacts", "/all.json")
|
|
|
|
|
|
async def scry_settings(client: UrbitClient, desk: str = "moltbot") -> dict[str, Any] | None:
|
|
return await scry_get(client, "settings", f"/desk/{desk}/all.json")
|
|
|
|
|
|
async def scry_channels(client: UrbitClient) -> list[str] | None:
|
|
result = await scry_get(client, "channels", "/v2")
|
|
if isinstance(result, dict):
|
|
return result.get("channels", [])
|
|
return result
|
|
|
|
|
|
async def scry_groups_init(client: UrbitClient) -> dict[str, Any] | None:
|
|
return await scry_get(client, "groups-ui", "/v6/init.json")
|
|
|
|
|
|
async def scry_groups(client: UrbitClient) -> dict[str, Any] | None:
|
|
return await scry_get(client, "groups", "/groups.json")
|
|
|
|
|
|
async def scry_foreigns(client: UrbitClient) -> dict[str, Any] | None:
|
|
return await scry_get(client, "groups", "/v1/foreigns.json")
|
|
|
|
|
|
async def scry_blocked_ships(client: UrbitClient) -> list[str] | None:
|
|
result = await scry_get(client, "chat", "/blocked.json")
|
|
if isinstance(result, dict):
|
|
return result.get("blocked", [])
|
|
return result
|