"""
Orchestration layer for proxy redirection.
Defines the base class that utilizes pure utility functions to determine redirect state.
"""
from typing import Dict, Any
from utils import (
extract_request_context,
handle_no_redirect_needed,
check_health,
output_custom_redirect,
)
class ProxyRedirectBase:
"""
Base orchestrator. Manages state and pure function execution.
"""
def __init__(self, env: Dict[str, Any]):
"""
Initializes the context dictionary using the provided environment.
"""
self.context = extract_request_context(env)
def run(self) -> Dict[str, Any]:
"""
Orchestrates the logic. Returns a dictionary representing
the final response payload.
"""
# 1. Check if we can proceed or if an immediate response is needed
intervention = handle_no_redirect_needed(
self.context["redirect"], self.context["request_uri"]
)
if intervention:
status, body = intervention
return {
"status": status,
"headers": {"Content-Type": "text/plain"},
"body": body,
}
# 2. Perform health check
health_result = check_health(
self.context["skip_health_check"],
self.context["backend"],
self.context["redirect"],
self.context["redirect_status"],
)
# 3. If the health check dictates a redirect, generate the HTML
if health_result["action"] == "redirect":
return output_custom_redirect(health_result["url"], health_result["status"])
# 4. Fallback for errors
return {
"status": health_result.get("status", 500),
"headers": {"Content-Type": "text/plain"},
"body": health_result.get("body", "Internal Server Error"),
}