"""Client for the patchbayd API socket (one request line, one JSON line back).""" import json import os import socket from flask import current_app def request(cmd, timeout=3.0): path = os.path.join(current_app.config["PB_CONF"].get("rundir"), "api.sock") try: with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as s: s.settimeout(timeout) s.connect(path) s.sendall(cmd.encode() + b"\n") data = b"" while not data.endswith(b"\n"): chunk = s.recv(65536) if not chunk: break data += chunk return json.loads(data or b"{}") except (OSError, ValueError) as e: return {"ok": False, "error": f"daemon not reachable: {e}"} def reload(): return request("RELOAD")