fdf
This commit is contained in:
@@ -12,8 +12,7 @@ import sys
|
||||
from SYS.logger import log, debug
|
||||
import time
|
||||
import logging
|
||||
from pathlib import Path
|
||||
from typing import Any, Dict, Optional, Set, List, Sequence
|
||||
from typing import Any, Dict, Optional, Set, List, Sequence, Tuple
|
||||
from urllib.parse import urlencode, urlparse
|
||||
from .HTTP import HTTPClient
|
||||
|
||||
@@ -31,6 +30,24 @@ _CACHE_TIMESTAMP: float = 0
|
||||
_CACHE_DURATION: float = 3600 # 1 hour
|
||||
|
||||
|
||||
# Cache for init-time connectivity checks (api_key fingerprint -> (ok, reason))
|
||||
_INIT_CHECK_CACHE: Dict[str, Tuple[bool, Optional[str]]] = {}
|
||||
|
||||
|
||||
def _ping_alldebrid(base_url: str) -> Tuple[bool, Optional[str]]:
|
||||
"""Ping the AllDebrid API base URL (no API key required)."""
|
||||
try:
|
||||
url = str(base_url or "").rstrip("/") + "/ping"
|
||||
with HTTPClient(timeout=10.0, headers={'User-Agent': 'downlow/1.0'}) as client:
|
||||
response = client.get(url)
|
||||
data = json.loads(response.content.decode('utf-8'))
|
||||
if data.get('status') == 'success' and data.get('data', {}).get('ping') == 'pong':
|
||||
return True, None
|
||||
return False, "Invalid API response"
|
||||
except Exception as exc:
|
||||
return False, str(exc)
|
||||
|
||||
|
||||
class AllDebridClient:
|
||||
"""Client for AllDebrid API."""
|
||||
|
||||
@@ -50,6 +67,18 @@ class AllDebridClient:
|
||||
if not self.api_key:
|
||||
raise AllDebridError("AllDebrid API key is empty")
|
||||
self.base_url = self.BASE_url[0] # Start with v4
|
||||
|
||||
# Init-time availability validation (cached per process)
|
||||
fingerprint = f"base:{self.base_url}" # /ping does not require the api key
|
||||
cached = _INIT_CHECK_CACHE.get(fingerprint)
|
||||
if cached is None:
|
||||
ok, reason = _ping_alldebrid(self.base_url)
|
||||
_INIT_CHECK_CACHE[fingerprint] = (ok, reason)
|
||||
else:
|
||||
ok, reason = cached
|
||||
|
||||
if not ok:
|
||||
raise AllDebridError(reason or "AllDebrid unavailable")
|
||||
|
||||
def _request(self, endpoint: str, params: Optional[Dict[str, str]] = None) -> Dict[str, Any]:
|
||||
"""Make a request to AllDebrid API.
|
||||
|
||||
Reference in New Issue
Block a user