refactor(api): Audit and cleanup api_hook_client.py and api_hooks.py

This commit is contained in:
2026-05-02 13:08:47 -04:00
parent 6dd9b67d5c
commit f9b5acd758
2 changed files with 15 additions and 0 deletions
+6
View File
@@ -42,6 +42,7 @@ See Also:
"""
def _get_app_attr(app: Any, name: str, default: Any = None) -> Any:
"""Retrieves an attribute from the App or its Controller."""
if hasattr(app, name):
val = getattr(app, name)
return val
@@ -51,11 +52,13 @@ def _get_app_attr(app: Any, name: str, default: Any = None) -> Any:
return default
def _has_app_attr(app: Any, name: str) -> bool:
"""Checks if an attribute exists on the App or its Controller."""
if hasattr(app, name): return True
if hasattr(app, 'controller') and hasattr(app.controller, name): return True
return False
def _set_app_attr(app: Any, name: str, value: Any) -> None:
"""Sets an attribute on the App or its Controller."""
if hasattr(app, name):
setattr(app, name, value)
elif hasattr(app, 'controller'):
@@ -66,10 +69,12 @@ def _set_app_attr(app: Any, name: str, value: Any) -> None:
class HookServerInstance(ThreadingHTTPServer):
"""Custom HTTPServer that carries a reference to the main App instance."""
def __init__(self, server_address: tuple[str, int], RequestHandlerClass: type, app: Any) -> None:
"""Initializes the server instance with an app reference."""
super().__init__(server_address, RequestHandlerClass)
self.app = app
def _serialize_for_api(obj: Any) -> Any:
"""Serializes complex objects into API-friendly formats (dicts/lists)."""
if hasattr(obj, "to_dict"):
return obj.to_dict()
if isinstance(obj, list):
@@ -81,6 +86,7 @@ def _serialize_for_api(obj: Any) -> Any:
class HookHandler(BaseHTTPRequestHandler):
"""Handles incoming HTTP requests for the API hooks."""
def do_GET(self) -> None:
"""Handles GET requests by routing to the appropriate state provider."""
try:
app = self.server.app
session_logger.log_api_hook("GET", self.path, "")