v2.1.2: list_app_catalog использует GET и расширенный список путей

This commit is contained in:
2026-08-03 00:22:09 +03:00
parent 70314a0097
commit fee69e65b3
+65 -43
View File
@@ -1,48 +1,70 @@
def _rest_request(self, method: str, endpoint: str, payload: Optional[dict] = None, timeout: Optional[int] = None) -> Optional[Any]: payload: Dict[str, Any] = {"catalog_name": catalog, "retrieve_all_trains": True}
method = method.upper() if category:
if method not in ("POST", "PUT", "DELETE", "PATCH", "GET"): payload["categories"] = [category]
self._last_error = f"_rest_request: неподдерживаемый метод {method}" # v2.1.2: TrueNAS 25.10 исторически делал POST /app/available, но в новых билдах
return None # многие эндпоинты переехали на GET. _rest_request сам делает POST→GET fallback
url = f"{self._base}/api/v2.0/{endpoint}" # при 405, поэтому здесь используем _post — он сам разберётся.
t = timeout if timeout is not None else self.valves.request_timeout # Дополнительно пробуем GET напрямую для надёжности.
endpoints = [
"app/available", # новый (Electric Eel+)
"app.available",
"chart/release/names",
"chart.release.names",
"chart_release/names",
"catalog/items", # fallback
"catalog.items",
"catalog_items",
"app/available_apps",
"app/list_apps",
]
apps: List[Dict[str, Any]] = []
used_ep = None
used_method = None
for ep in endpoints:
# Сначала GET (часто работает на 25.10)
try: try:
r = requests.request(method, url, headers=self._headers, json=payload if payload is not None else {}, timeout=t, verify=self.valves.verify_ssl) r = requests.get(
# v2.1.2: автоматический fallback POST → GET при 405 (Method Not Allowed). f"{self._base}/api/v2.0/{ep}",
# Это часто встречается в TrueNAS 25.10: некоторые эндпоинты каталога headers=self._headers,
# исторически были POST, но в новых версиях стали GET. params=payload if payload else None,
if r.status_code == 405 and method == "POST": timeout=self.valves.request_timeout,
verify=self.valves.verify_ssl,
)
if r.status_code == 200:
try: try:
r2 = requests.get(url, headers=self._headers, params=payload or {}, timeout=t, verify=self.valves.verify_ssl) data = r.json()
if r2.status_code in (200, 201, 202, 204): if isinstance(data, list):
if not r2.text: apps = data
return {"status": "ok", "http_code": r2.status_code, "via": "GET-fallback"} used_ep = ep
try: used_method = "GET"
data = r2.json() break
if isinstance(data, dict): if isinstance(data, dict):
data["_via"] = "GET-fallback" for key in ("items", "applications", "releases", "apps"):
return data if key in data and isinstance(data[key], list):
apps = data[key]
used_ep = ep
used_method = "GET"
break
if apps:
break
except Exception: except Exception:
return {"status": "ok", "text": r2.text, "http_code": r2.status_code, "_via": "GET-fallback"} pass
except Exception: except Exception:
pass # вернёмся к исходной ошибке ниже pass
if r.status_code in (200, 201, 202, 204): # Затем POST (через _rest_request — у него есть fallback на GET при 405)
if not r.text: res = self._post(ep, payload, timeout=self.valves.request_timeout)
return {"status": "ok", "http_code": r.status_code} if res is not None:
try: if isinstance(res, list):
return r.json() apps = res
except Exception: used_ep = ep
return {"status": "ok", "text": r.text, "http_code": r.status_code} used_method = "POST"
self._last_error = f"{method} {endpoint} → HTTP {r.status_code}: {r.text[:400]}" break
return None if isinstance(res, dict):
except requests.exceptions.SSLError as e: for key in ("items", "applications", "releases", "apps"):
self._last_error = f"SSL-ошибка {method} {endpoint}: {e}" if key in res and isinstance(res[key], list):
return None apps = res[key]
except requests.exceptions.ConnectionError as e: used_ep = ep
self._last_error = f"Соединение {method} {endpoint}: {e}" used_method = res.get("_via", "POST")
return None break
except requests.exceptions.Timeout: if apps:
self._last_error = f"Таймаут {method} {endpoint} (> {t}с)" break
return None
except Exception as e:
self._last_error = f"{method} {endpoint}: {type(e).__name__}: {e}"
return None