feat: add cdr processing worker and dlq routing
This commit is contained in:
38
src/core/worker/processor.py
Normal file
38
src/core/worker/processor.py
Normal file
@@ -0,0 +1,38 @@
|
||||
import json
|
||||
from typing import Any
|
||||
|
||||
from core.domain.errors import (
|
||||
CDRProcessingError,
|
||||
FailureReason,
|
||||
)
|
||||
from core.domain.models import NormalizedCDR
|
||||
from core.domain.normalization import normalize_cdr
|
||||
from core.domain.validation import parse_raw_cdr
|
||||
|
||||
|
||||
def decode_payload(value: bytes) -> dict[str, Any]:
|
||||
try:
|
||||
text = value.decode("utf-8")
|
||||
payload = json.loads(text)
|
||||
|
||||
except (UnicodeDecodeError, json.JSONDecodeError) as exc:
|
||||
raise CDRProcessingError(
|
||||
reason=FailureReason.MALFORMED_JSON,
|
||||
detail=f"Unable to decode Kafka message as JSON: {exc}",
|
||||
) from exc
|
||||
|
||||
if not isinstance(payload, dict):
|
||||
raise CDRProcessingError(
|
||||
reason=FailureReason.NORMALIZATION_ERROR,
|
||||
detail="Kafka JSON payload must be an object.",
|
||||
)
|
||||
|
||||
return payload
|
||||
|
||||
|
||||
def process_payload(
|
||||
payload: dict[str, Any],
|
||||
) -> NormalizedCDR:
|
||||
raw_cdr = parse_raw_cdr(payload)
|
||||
|
||||
return normalize_cdr(raw_cdr)
|
||||
Reference in New Issue
Block a user