fixed default network
This commit is contained in:
+87
-8
@@ -12,6 +12,90 @@ from config_store import (ALLOWED_ICON_EXT, icons_dir, list_icons, load_config,
|
||||
|
||||
api_bp = Blueprint("api", __name__)
|
||||
|
||||
_RESPONSE_RESULTS = {"ok", "error", "pending"}
|
||||
|
||||
|
||||
def _validate_response_check(data):
|
||||
if data is None:
|
||||
return None
|
||||
if not isinstance(data, dict):
|
||||
abort(400, "responseCheck must be an object")
|
||||
|
||||
enabled = bool(data.get("enabled", False))
|
||||
if not enabled:
|
||||
return {"enabled": False}
|
||||
|
||||
rules = data.get("rules", [])
|
||||
if not isinstance(rules, list):
|
||||
abort(400, "responseCheck.rules must be an array")
|
||||
|
||||
normalized_rules = []
|
||||
for rule in rules:
|
||||
if not isinstance(rule, dict):
|
||||
abort(400, "each responseCheck rule must be an object")
|
||||
field = (rule.get("field") or "").strip()
|
||||
if not field:
|
||||
abort(400, "responseCheck rule field is required")
|
||||
result = (rule.get("result") or "error").strip().lower()
|
||||
if result not in _RESPONSE_RESULTS:
|
||||
abort(400, "responseCheck rule result must be ok, error or pending")
|
||||
if not rule.get("equals") and rule.get("contains") in (None, ""):
|
||||
abort(400, "responseCheck rule needs equals or contains")
|
||||
item = {"field": field, "result": result}
|
||||
if rule.get("equals") not in (None, ""):
|
||||
item["equals"] = str(rule.get("equals"))
|
||||
if rule.get("contains") not in (None, ""):
|
||||
item["contains"] = str(rule.get("contains"))
|
||||
message = (rule.get("message") or "").strip()
|
||||
if message:
|
||||
item["message"] = message
|
||||
normalized_rules.append(item)
|
||||
|
||||
default_result = (data.get("defaultResult") or "error").strip().lower()
|
||||
if default_result not in _RESPONSE_RESULTS:
|
||||
abort(400, "defaultResult must be ok, error or pending")
|
||||
|
||||
poll_in = data.get("poll") or {}
|
||||
if poll_in and not isinstance(poll_in, dict):
|
||||
abort(400, "responseCheck.poll must be an object")
|
||||
interval_ms = int(poll_in.get("intervalMs", 3000))
|
||||
max_attempts = int(poll_in.get("maxAttempts", 20))
|
||||
timeout_ms = int(poll_in.get("timeoutMs", 120000))
|
||||
if interval_ms < 500 or interval_ms > 60000:
|
||||
abort(400, "poll.intervalMs must be 500..60000")
|
||||
if max_attempts < 1 or max_attempts > 100:
|
||||
abort(400, "poll.maxAttempts must be 1..100")
|
||||
if timeout_ms < interval_ms or timeout_ms > 600000:
|
||||
abort(400, "poll.timeoutMs must be >= intervalMs and <= 600000")
|
||||
|
||||
return {
|
||||
"enabled": True,
|
||||
"requireHttpSuccess": bool(data.get("requireHttpSuccess", True)),
|
||||
"rules": normalized_rules,
|
||||
"defaultResult": default_result,
|
||||
"poll": {
|
||||
"intervalMs": interval_ms,
|
||||
"maxAttempts": max_attempts,
|
||||
"timeoutMs": timeout_ms,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def _apply_button_action(action, data):
|
||||
action["type"] = data.get("actionType", action.get("type", "http_get"))
|
||||
action["url"] = data.get("url", action.get("url", ""))
|
||||
if "headers" in data:
|
||||
action["headers"] = data["headers"]
|
||||
if "body" in data:
|
||||
action["body"] = data["body"]
|
||||
if "responseCheck" in data:
|
||||
rc = _validate_response_check(data.get("responseCheck"))
|
||||
if rc is None:
|
||||
action.pop("responseCheck", None)
|
||||
else:
|
||||
action["responseCheck"] = rc
|
||||
return action
|
||||
|
||||
|
||||
@api_bp.route("/icons/<path:fname>")
|
||||
@login_required
|
||||
@@ -46,12 +130,12 @@ def api_add_button():
|
||||
"id": data.get("id") or uuid.uuid4().hex,
|
||||
"label": data.get("label", "Кнопка"),
|
||||
"iconPath": data.get("iconPath", ""),
|
||||
"action": {
|
||||
"action": _apply_button_action({
|
||||
"type": data.get("actionType", "http_get"),
|
||||
"url": data.get("url", ""),
|
||||
"headers": data.get("headers", {}),
|
||||
"body": data.get("body", ""),
|
||||
},
|
||||
}, data),
|
||||
"feedback": {
|
||||
"successText": data.get("successText", ""),
|
||||
"errorText": data.get("errorText", "Ошибка"),
|
||||
@@ -79,12 +163,7 @@ def api_update_button(bid):
|
||||
b["label"] = data.get("label", b.get("label"))
|
||||
b["iconPath"] = data.get("iconPath", b.get("iconPath"))
|
||||
action = b.setdefault("action", {})
|
||||
action["type"] = data.get("actionType", action.get("type", "http_get"))
|
||||
action["url"] = data.get("url", action.get("url", ""))
|
||||
if "headers" in data:
|
||||
action["headers"] = data["headers"]
|
||||
if "body" in data:
|
||||
action["body"] = data["body"]
|
||||
_apply_button_action(action, data)
|
||||
feedback = b.setdefault("feedback", {})
|
||||
feedback["successText"] = data.get("successText", feedback.get("successText", ""))
|
||||
feedback["errorText"] = data.get("errorText", feedback.get("errorText", "Ошибка"))
|
||||
|
||||
Reference in New Issue
Block a user