"""
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,
)
from logger import debug_log
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,
}
redirect_uri = self.context["redirect"]
redirect_status = self.context["redirect_status"]
# 2. Perform health check
health_result = check_health(
self.context["backend"],
self.context["health_check"],
self.context["skip_health_check"],
redirect_uri,
redirect_status,
)
if health_result is True:
return output_custom_redirect(redirect_uri, redirect_status)
# 4. Fallback for errors
debug_log(health_result)
return {
"status": 500,
"headers": {"Content-Type": "text/plain"},
"body": health_result,
}