"""Lambda fivetran-webhooks function module.""" import logging from typing import Any from app.enums import ConnectorType from app.exceptions import WebhookHandleError from app.google import logic as google_logic from app.meta import logic as meta_logic from app.models import LambdaResponse from app.shopify import logic as shopify_logic from app.tiktok import logic as tiktok_logic from app.utils import parse_event logger = logging.getLogger(__name__) def handler(event_obj: Any, context: Any) -> Any: """Lambda entry point.""" logger.info("Received event", extra={"event": event_obj}) try: event, webhook_event = parse_event(event_obj) if event.http_method != "POST": return LambdaResponse( status_code=405, status_description="405 Method Not Allowed" ).model_dump(by_alias=True) if event.path == "/shopify": shopify_logic.handle_webhook_event(webhook_event) elif event.path == "/facebook-ads": meta_logic.handle_webhook_event(webhook_event) elif event.path == "/ad-reporting": if webhook_event.connector_type == ConnectorType.FACEBOOK_ADS: meta_logic.handle_webhook_event(webhook_event) elif webhook_event.connector_type == ConnectorType.TIKTOK_ADS: tiktok_logic.handle_webhook_event(webhook_event) elif webhook_event.connector_type == ConnectorType.GOOGLE_ADS: google_logic.handle_webhook_event(webhook_event) else: return LambdaResponse( status_code=404, status_description="404 Not Found" ).model_dump(by_alias=True) except WebhookHandleError: logger.exception("Webhook handle error") return LambdaResponse( status_code=400, status_description="400 Bad Request", ).model_dump(by_alias=True) except Exception: logger.exception("Unknown error") return LambdaResponse( status_code=500, status_description="500 Internal Server Error", ).model_dump(by_alias=True) return LambdaResponse( status_code=200, status_description="200 OK", ).model_dump(by_alias=True)