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.

4.8 KB
Raw Blame
from typing import Optional, cast

from products.warehouse_sources.sources.campayn._config import CampaynSourceConfig
from products.warehouse_sources.sources.campayn.campayn import (
    campayn_source,
    is_subdomain_valid,
    validate_credentials as validate_campayn_credentials,
)
from products.warehouse_sources.sources.campayn.settings import ENDPOINTS
from products.warehouse_sources.sources.sdk import (
    CanonicalDescriptions,
    DataWarehouseSourceCategory,
    ExternalDataSourceType,
    FieldType,
    ReleaseStatus,
    SimpleSource,
    SourceConfig,
    SourceFieldInputConfig,
    SourceFieldInputConfigType,
    SourceInputs,
    SourceRegistry,
    SourceResponse,
    SourceSchema,
    build_endpoint_schemas,
    detect,
)


@SourceRegistry.register
class CampaynSource(SimpleSource[CampaynSourceConfig]):
    codebase_detection = detect(env=("^CAMPAYN_",))

    lists_tables_without_credentials = True  # static endpoint catalog — safe for public docs
    supported_versions = ("v1",)
    default_version = "v1"
    api_docs_url = "https://github.com/nebojsac/Campayn-API"

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

    @property
    def connection_host_fields(self) -> list[str]:
        # The API key is sent to {subdomain}.campayn.com, so retargeting the subdomain must re-require
        # the key — otherwise an org member could point it at a host they control and exfiltrate it.
        return ["subdomain"]

    @property
    def get_source_config(self) -> SourceConfig:
        return SourceConfig(
            name=ExternalDataSourceType.CAMPAYN,
            category=DataWarehouseSourceCategory.MARKETING___EMAIL,
            label="Campayn",
            releaseStatus=ReleaseStatus.BETA,
            caption="""Enter your Campayn subdomain and API key to pull your Campayn email-marketing data into the PostHog Data warehouse.

Your subdomain is the first part of your account host — for `acme.campayn.com`, enter `acme`. You can find and regenerate your API key in your Campayn account settings.""",
            iconPath="/static/services/campayn.png",
            docsUrl="https://posthog.com/docs/cdp/sources/campayn",
            fields=cast(
                list[FieldType],
                [
                    SourceFieldInputConfig(
                        name="subdomain",
                        label="Subdomain",
                        type=SourceFieldInputConfigType.TEXT,
                        required=True,
                        placeholder="acme",
                        secret=False,
                    ),
                    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.campayn.canonical_descriptions import CANONICAL_DESCRIPTIONS

        return CANONICAL_DESCRIPTIONS

    def get_non_retryable_errors(self) -> dict[str, str | None]:
        return {
            "401 Client Error": "Your Campayn API key is invalid or has been regenerated. Create a new key in your Campayn account settings, then reconnect.",
            "403 Client Error": "Your Campayn API key is not authorized for this data. Check the key's permissions, then reconnect.",
        }

    def get_schemas(
        self,
        config: CampaynSourceConfig,
        team_id: int,
        with_counts: bool = False,
        names: list[str] | None = None,
        force_refresh: bool = False,
        api_version: str | None = None,
    ) -> list[SourceSchema]:
        # Campayn's API exposes no pagination, cursors, or timestamp filters, so every table is full
        # refresh only.
        return build_endpoint_schemas(ENDPOINTS, {}, names)

    def validate_credentials(
        self,
        config: CampaynSourceConfig,
        team_id: int,
        schema_name: Optional[str] = None,
        api_version: str | None = None,
    ) -> tuple[bool, str | None]:
        if not is_subdomain_valid(config.subdomain):
            return False, "Campayn subdomain is incorrect"

        if validate_campayn_credentials(config.subdomain, config.api_key):
            return True, None

        return False, "Campayn rejected the credentials. Check the subdomain and API key are correct."

    def source_for_pipeline(self, config: CampaynSourceConfig, inputs: SourceInputs) -> SourceResponse:
        return campayn_source(
            subdomain=config.subdomain,
            api_key=config.api_key,
            endpoint=inputs.schema_name,
            team_id=inputs.team_id,
            job_id=inputs.job_id,
        )