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.6 KB
Raw Blame
from typing import Optional, cast

from products.warehouse_sources.sources.chameleon._config import ChameleonSourceConfig
from products.warehouse_sources.sources.chameleon.chameleon import (
    ChameleonResumeConfig,
    chameleon_source,
    validate_credentials as validate_chameleon_credentials,
)
from products.warehouse_sources.sources.chameleon.settings import CHAMELEON_ENDPOINTS, ENDPOINTS
from products.warehouse_sources.sources.sdk import (
    CanonicalDescriptions,
    DataWarehouseSourceCategory,
    ExternalDataSourceType,
    FieldType,
    ReleaseStatus,
    ResumableSource,
    ResumableSourceManager,
    SourceConfig,
    SourceFieldInputConfig,
    SourceFieldInputConfigType,
    SourceInputs,
    SourceRegistry,
    SourceResponse,
    SourceSchema,
    build_endpoint_schemas,
    detect,
)


@SourceRegistry.register
class ChameleonSource(ResumableSource[ChameleonSourceConfig, ChameleonResumeConfig]):
    codebase_detection = detect(
        script_hosts=("fast.chameleon.io", "fast.trychameleon.com"), npm=("@chamaeleonidae/chmln",)
    )

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

    supported_versions = ("v3",)
    default_version = "v3"
    api_docs_url = "https://developers.chameleon.io/"

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

    @property
    def get_source_config(self) -> SourceConfig:
        return SourceConfig(
            name=ExternalDataSourceType.CHAMELEON,
            category=DataWarehouseSourceCategory.ANALYTICS,
            label="Chameleon",
            releaseStatus=ReleaseStatus.GA,
            caption="""Enter your Chameleon API secret to automatically pull your Chameleon data into the PostHog Data warehouse.

You can generate an account-specific API secret in your [Chameleon account settings](https://app.chameleon.io/settings/tokens). The secret is only shown once, so copy it when you create it.""",
            iconPath="/static/services/chameleon.png",
            docsUrl="https://posthog.com/docs/cdp/sources/chameleon",
            keywords=["chameleon.io", "onboarding", "product adoption", "tours"],
            fields=cast(
                list[FieldType],
                [
                    SourceFieldInputConfig(
                        name="account_secret",
                        label="Account secret",
                        type=SourceFieldInputConfigType.PASSWORD,
                        required=True,
                        placeholder="Your Chameleon account secret",
                        secret=True,
                    ),
                ],
            ),
        )

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

        return CANONICAL_DESCRIPTIONS

    def get_non_retryable_errors(self) -> dict[str, str | None]:
        return {
            # Chameleon returns 403 (not 401) for an invalid or revoked account secret. It surfaces as a
            # requests HTTPError when `_fetch_page` calls `raise_for_status()`. Retrying can never satisfy
            # a credential problem, so stop the sync. Match the stable status text and base host.
            "403 Client Error: Forbidden for url: https://api.chameleon.io": "Your Chameleon account secret is invalid or has been revoked. Generate a new secret in your Chameleon account settings, then reconnect.",
        }

    def get_schemas(
        self,
        config: ChameleonSourceConfig,
        team_id: int,
        with_counts: bool = False,
        names: list[str] | None = None,
        force_refresh: bool = False,
        api_version: str | None = None,
    ) -> list[SourceSchema]:
        # Chameleon's only server-side time filter (`after`) keys on creation time, so it can't catch
        # updates to existing records. We ship every endpoint as full refresh (no incremental fields)
        # until incremental semantics are verified against a live account.
        return build_endpoint_schemas(
            ENDPOINTS,
            {},
            names,
            descriptions={
                "responses": "Microsurvey responses, fanned out across every Microsurvey. Full refresh only",
                "interactions": "Per-user Tour state, fanned out across every Tour. Full refresh only",
                "properties": "Custom data property definitions for both User Profiles and Companies. Full refresh only",
            },
            should_sync_default={
                endpoint: config.should_sync_default for endpoint, config in CHAMELEON_ENDPOINTS.items()
            },
        )

    def validate_credentials(
        self,
        config: ChameleonSourceConfig,
        team_id: int,
        schema_name: Optional[str] = None,
        api_version: str | None = None,
    ) -> tuple[bool, str | None]:
        return validate_chameleon_credentials(config.account_secret)

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

    def source_for_pipeline(
        self,
        config: ChameleonSourceConfig,
        resumable_source_manager: ResumableSourceManager[ChameleonResumeConfig],
        inputs: SourceInputs,
    ) -> SourceResponse:
        return chameleon_source(
            account_secret=config.account_secret,
            endpoint=inputs.schema_name,
            team_id=inputs.team_id,
            job_id=inputs.job_id,
            resumable_source_manager=resumable_source_manager,
        )