Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

5.7 KB
Raw Blame
from typing import Optional, cast

from products.warehouse_sources.sources.eventzilla._config import EventzillaSourceConfig
from products.warehouse_sources.sources.eventzilla.eventzilla import (
    EventzillaResumeConfig,
    eventzilla_source,
    validate_credentials as validate_eventzilla_credentials,
)
from products.warehouse_sources.sources.eventzilla.settings import ENDPOINTS, EVENTZILLA_ENDPOINTS, INCREMENTAL_FIELDS
from products.warehouse_sources.sources.sdk import (
    CanonicalDescriptions,
    DataWarehouseSourceCategory,
    ExternalDataSourceType,
    FieldType,
    ReleaseStatus,
    ResumableSource,
    ResumableSourceManager,
    SourceConfig,
    SourceFieldInputConfig,
    SourceFieldInputConfigType,
    SourceInputs,
    SourceRegistry,
    SourceResponse,
    SourceSchema,
    detect,
)


@SourceRegistry.register
class EventzillaSource(ResumableSource[EventzillaSourceConfig, EventzillaResumeConfig]):
    codebase_detection = detect(env=("^EVENTZILLA_",))

    supported_versions = ("v2",)
    default_version = "v2"
    api_docs_url = "https://developer.eventzilla.net/docs/"

    lists_tables_without_credentials = True  # static endpoint catalog — safe for public docs

    @property
    def source_type(self) -> ExternalDataSourceType:
        return ExternalDataSourceType.EVENTZILLA

    @property
    def get_source_config(self) -> SourceConfig:
        return SourceConfig(
            name=ExternalDataSourceType.EVENTZILLA,
            category=DataWarehouseSourceCategory.PRODUCTIVITY,
            label="Eventzilla",
            releaseStatus=ReleaseStatus.BETA,
            caption="""Enter your Eventzilla API key to automatically pull your Eventzilla data into the PostHog Data warehouse.

You can generate an API key in your Eventzilla account under **Settings > App Management**.""",
            iconPath="/static/services/eventzilla.png",
            docsUrl="https://posthog.com/docs/cdp/sources/eventzilla",
            fields=cast(
                list[FieldType],
                [
                    SourceFieldInputConfig(
                        name="api_key",
                        label="API key",
                        type=SourceFieldInputConfigType.PASSWORD,
                        required=True,
                        placeholder="",
                        secret=True,
                    ),
                ],
            ),
        )

    def get_canonical_descriptions(self) -> CanonicalDescriptions:
        from products.warehouse_sources.sources.eventzilla.canonical_descriptions import CANONICAL_DESCRIPTIONS

        return CANONICAL_DESCRIPTIONS

    def get_non_retryable_errors(self) -> dict[str, str | None]:
        return {
            # Eventzilla returns 401/403 for an invalid, revoked, or unauthorized API key. Retrying
            # can never satisfy a credential problem, so stop the sync. Match the stable status text
            # and base host, not the per-request path/query.
            "401 Client Error: Unauthorized for url: https://www.eventzillaapi.net": "Your Eventzilla API key is invalid or has been revoked. Generate a new key under Settings > App Management, then reconnect.",
            "403 Client Error: Forbidden for url: https://www.eventzillaapi.net": "Your Eventzilla API key is not authorized to access this data. Check the key under Settings > App Management, then reconnect.",
        }

    def get_schemas(
        self,
        config: EventzillaSourceConfig,
        team_id: int,
        with_counts: bool = False,
        names: list[str] | None = None,
        force_refresh: bool = False,
        api_version: str | None = None,
    ) -> list[SourceSchema]:
        def _description(endpoint: str) -> str | None:
            if EVENTZILLA_ENDPOINTS[endpoint].fan_out_over_events:
                return "Fetched per event by walking every event. Full refresh only"
            return None

        def _build_schema(endpoint: str) -> SourceSchema:
            endpoint_config = EVENTZILLA_ENDPOINTS[endpoint]
            return SourceSchema(
                name=endpoint,
                # Eventzilla exposes no server-side updated-since filter, so every table is full
                # refresh only — no incremental or append modes.
                supports_incremental=False,
                supports_append=False,
                incremental_fields=INCREMENTAL_FIELDS.get(endpoint, []),
                should_sync_default=endpoint_config.should_sync_default,
                description=_description(endpoint),
            )

        schemas = [_build_schema(endpoint) for endpoint in ENDPOINTS]
        if names is not None:
            names_set = set(names)
            schemas = [s for s in schemas if s.name in names_set]
        return schemas

    def validate_credentials(
        self,
        config: EventzillaSourceConfig,
        team_id: int,
        schema_name: Optional[str] = None,
        api_version: str | None = None,
    ) -> tuple[bool, str | None]:
        if validate_eventzilla_credentials(config.api_key):
            return True, None

        return False, "Invalid Eventzilla API key"

    def get_resumable_source_manager(self, inputs: SourceInputs) -> ResumableSourceManager[EventzillaResumeConfig]:
        return ResumableSourceManager[EventzillaResumeConfig](inputs, EventzillaResumeConfig)

    def source_for_pipeline(
        self,
        config: EventzillaSourceConfig,
        resumable_source_manager: ResumableSourceManager[EventzillaResumeConfig],
        inputs: SourceInputs,
    ) -> SourceResponse:
        return eventzilla_source(
            api_key=config.api_key,
            endpoint=inputs.schema_name,
            team_id=inputs.team_id,
            job_id=inputs.job_id,
            resumable_source_manager=resumable_source_manager,
        )