36 lines
1.1 KiB
Python
36 lines
1.1 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import Any, Dict, cast
|
|
|
|
from .errors import ApiError
|
|
from .models import *
|
|
from .utils import strip_ansi
|
|
|
|
|
|
class TraceApiMixin:
|
|
# Trace
|
|
def trace_get(self, mode: TraceMode = "full") -> TraceDump:
|
|
m = str(mode).lower().strip()
|
|
if m not in ("full", "gui", "smartdns"):
|
|
m = "full"
|
|
data = cast(
|
|
Dict[str, Any],
|
|
self._json(self._request("GET", "/api/v1/trace-json", params={"mode": m}, timeout=5.0)) or {},
|
|
)
|
|
lines = data.get("lines") or []
|
|
if not isinstance(lines, list):
|
|
lines = []
|
|
return TraceDump(lines=[strip_ansi(str(x)) for x in lines])
|
|
|
|
def trace_append(self, kind: Literal["gui", "smartdns", "info"], line: str) -> None:
|
|
try:
|
|
self._request(
|
|
"POST",
|
|
"/api/v1/trace/append",
|
|
json_body={"kind": kind, "line": str(line)},
|
|
timeout=2.0,
|
|
)
|
|
except ApiError:
|
|
# Logging must never crash UI.
|
|
pass
|