fdf
This commit is contained in:
@@ -106,16 +106,7 @@ class HydrusNetwork:
|
||||
|
||||
def _perform_request(self, spec: HydrusRequestSpec) -> Any:
|
||||
headers: dict[str, str] = {}
|
||||
|
||||
# On first request, try to acquire session key for security
|
||||
if not self._session_key and self.access_key and spec.endpoint != "/session_key":
|
||||
try:
|
||||
logger.debug(f"[Hydrus] Acquiring session key on first request...")
|
||||
self._acquire_session_key()
|
||||
except Exception as e:
|
||||
# If session key acquisition fails, fall back to access key
|
||||
logger.debug(f"[Hydrus] Session key acquisition failed: {e}. Using access key instead.")
|
||||
|
||||
|
||||
# Use session key if available, otherwise use access key
|
||||
if self._session_key:
|
||||
headers["Hydrus-Client-API-Session-Key"] = self._session_key
|
||||
@@ -496,6 +487,7 @@ class HydrusNetwork:
|
||||
file_service_name: str | None = None,
|
||||
return_hashes: bool = False,
|
||||
return_file_ids: bool = True,
|
||||
return_file_count: bool = False,
|
||||
include_current_tags: bool | None = None,
|
||||
include_pending_tags: bool | None = None,
|
||||
file_sort_type: int | None = None,
|
||||
@@ -511,6 +503,7 @@ class HydrusNetwork:
|
||||
("file_service_name", file_service_name, lambda v: v),
|
||||
("return_hashes", return_hashes, lambda v: "true" if v else None),
|
||||
("return_file_ids", return_file_ids, lambda v: "true" if v else None),
|
||||
("return_file_count", return_file_count, lambda v: "true" if v else None),
|
||||
(
|
||||
"include_current_tags",
|
||||
include_current_tags,
|
||||
@@ -1222,15 +1215,17 @@ def is_hydrus_available(config: dict[str, Any]) -> bool:
|
||||
|
||||
|
||||
def get_client(config: dict[str, Any]) -> HydrusNetwork:
|
||||
"""Create and return a Hydrus client with session key authentication.
|
||||
|
||||
Reuses cached client instance to preserve session keys across requests.
|
||||
"""Create and return a Hydrus client.
|
||||
|
||||
Uses access-key authentication by default (no session key acquisition).
|
||||
A session key may still be acquired explicitly by calling
|
||||
`HydrusNetwork.ensure_session_key()`.
|
||||
|
||||
Args:
|
||||
config: Configuration dict with Hydrus settings
|
||||
|
||||
Returns:
|
||||
HydrusClient instance (with active session key)
|
||||
HydrusClient instance
|
||||
|
||||
Raises:
|
||||
RuntimeError: If Hydrus is not configured or unavailable
|
||||
@@ -1259,34 +1254,11 @@ def get_client(config: dict[str, Any]) -> HydrusNetwork:
|
||||
|
||||
# Check if we have a cached client
|
||||
if cache_key in _hydrus_client_cache:
|
||||
cached_client = _hydrus_client_cache[cache_key]
|
||||
# If cached client has a session key, reuse it (don't re-acquire)
|
||||
if hasattr(cached_client, '_session_key') and cached_client._session_key:
|
||||
# debug(f"Reusing cached session key for {hydrus_url}")
|
||||
return cached_client
|
||||
# If no session key in cache, try to get one
|
||||
try:
|
||||
cached_client.ensure_session_key()
|
||||
return cached_client
|
||||
except Exception as e:
|
||||
# If verification fails, remove from cache and create new one
|
||||
debug(f"Cached client invalid, creating new: {e}")
|
||||
del _hydrus_client_cache[cache_key]
|
||||
return _hydrus_client_cache[cache_key]
|
||||
|
||||
# Create new client
|
||||
client = HydrusNetwork(hydrus_url, access_key, timeout)
|
||||
|
||||
# Acquire session key for secure authentication
|
||||
try:
|
||||
client.ensure_session_key()
|
||||
except HydrusConnectionError:
|
||||
# This should not happen since we checked availability above
|
||||
debug(f"Hydrus service unavailable during client creation")
|
||||
raise RuntimeError("Hydrus is unavailable") from None
|
||||
except Exception as e:
|
||||
# Log other exceptions but don't fail - client can still work with access_key
|
||||
debug(f"Warning: Could not acquire session key: {e}")
|
||||
|
||||
# Cache the client
|
||||
_hydrus_client_cache[cache_key] = client
|
||||
|
||||
|
||||
Reference in New Issue
Block a user