"""Alert Router — utils for channel payloads.""" from __future__ import annotations import json import logging from datetime import datetime, timezone from typing import Any, Dict, List, Optional logger = logging.getLogger(__name__) class AlertUtils: """Utils for Alert Router channel payloads.""" _DATE_FIELDS = ("dispatched_at") @classmethod def loads(cls, raw: str) -> Dict[str, Any]: """Deserialise a JSON channel payload.""" data = json.loads(raw) return cls._coerce(data) @classmethod def dumps(cls, record: Dict[str, Any]) -> str: """Serialise a channel record to JSON.""" return json.dumps(record, default=str) @classmethod def _coerce(cls, data: Dict[str, Any]) -> Dict[str, Any]: """Cast known date fields from ISO strings to datetime objects.""" out: Dict[str, Any] = {} for k, v in data.items(): if k in cls._DATE_FIELDS and isinstance(v, str): try: out[k] = datetime.fromisoformat(v) except ValueError: out[k] = v else: out[k] = v return out def parse_channels(payload: str) -> List[Dict[str, Any]]: """Parse a JSON array of Channel payloads.""" raw = json.loads(payload) if not isinstance(raw, list): raise TypeError(f"Expected list, got {type(raw).__name__}") return [AlertUtils._coerce(item) for item in raw] def escalate_channel_to_str( record: Dict[str, Any], indent: Optional[int] = None ) -> str: """Convenience wrapper — serialise a Channel to a JSON string.""" if indent is None: return AlertUtils.dumps(record) return json.dumps(record, indent=indent, default=str)