fix: replace RuntimeError with StultusUnavailable exception for better error handling
This commit is contained in:
parent
9b73e9150b
commit
fb0463f5c3
@ -1,37 +1,32 @@
|
|||||||
# multinut/stultus.py
|
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
BASE_URL = "https://stultus.chipperfluff.at"
|
BASE_URL = "https://stultus.chipperfluff.at"
|
||||||
|
|
||||||
|
class StultusUnavailable(Exception):
|
||||||
|
"""Raised when the stultus server is unreachable or returns an error."""
|
||||||
|
pass
|
||||||
|
|
||||||
def _get(path: str, params: dict | list = None) -> bool:
|
def _get(path: str, params: dict | list = None) -> bool:
|
||||||
try:
|
try:
|
||||||
r = requests.get(f"{BASE_URL}/{path}", params=params, timeout=5)
|
r = requests.get(f"{BASE_URL}/{path}", params=params, timeout=5)
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
return r.json().get("result", False)
|
return r.json().get("result", False)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise RuntimeError(f"[STULTUS] Failed logic call to '{path}': {e}")
|
raise StultusUnavailable(f"[STULTUS] Failed logic call to '{path}': {e}")
|
||||||
|
|
||||||
def EQUALS(a, b):
|
def ping() -> bool:
|
||||||
return _get("equals", {"a": a, "b": b})
|
try:
|
||||||
|
r = requests.get(f"{BASE_URL}/ping", timeout=2)
|
||||||
|
return r.status_code == 200 and r.json().get("status") == "ok"
|
||||||
|
except Exception:
|
||||||
|
return False
|
||||||
|
|
||||||
def GREATER(a, b):
|
def EQUALS(a, b): return _get("equals", {"a": a, "b": b})
|
||||||
return _get("greater", {"a": a, "b": b})
|
def GREATER(a, b): return _get("greater", {"a": a, "b": b})
|
||||||
|
def LESSER(a, b): return _get("lesser", {"a": a, "b": b})
|
||||||
|
def NOT(x): return _get("not", {"val": str(x).lower()})
|
||||||
|
def AND(*args): return _get("and", [("val", str(v).lower()) for v in args])
|
||||||
|
def OR(*args): return _get("or", [("val", str(v).lower()) for v in args])
|
||||||
|
|
||||||
def LESSER(a, b):
|
def GREATER_EQUAL(a, b): return OR(GREATER(a, b), EQUALS(a, b))
|
||||||
return _get("lesser", {"a": a, "b": b})
|
def LESSER_EQUAL(a, b): return OR(LESSER(a, b), EQUALS(a, b))
|
||||||
|
|
||||||
def AND(*args):
|
|
||||||
return _get("and", [("val", str(v).lower()) for v in args])
|
|
||||||
|
|
||||||
def OR(*args):
|
|
||||||
return _get("or", [("val", str(v).lower()) for v in args])
|
|
||||||
|
|
||||||
def NOT(x):
|
|
||||||
return _get("not", {"val": str(x).lower()})
|
|
||||||
|
|
||||||
def GREATER_EQUAL(a, b):
|
|
||||||
return OR(GREATER(a, b), EQUALS(a, b))
|
|
||||||
|
|
||||||
def LESSER_EQUAL(a, b):
|
|
||||||
return OR(LESSER(a, b), EQUALS(a, b))
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user