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

from products.warehouse_sources.sources.bigmailer._config import BigMailerSourceConfig
from products.warehouse_sources.sources.bigmailer.bigmailer import (
    AUTH_ERROR_MESSAGE,
    BigMailerResumeConfig,
    bigmailer_source,
    validate_credentials as validate_bigmailer_credentials,
)
from products.warehouse_sources.sources.bigmailer.settings import BIGMAILER_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 BigMailerSource(ResumableSource[BigMailerSourceConfig, BigMailerResumeConfig]):
    codebase_detection = detect(env=("^(BIG_MAILER|BIGMAILER)_",))

    api_docs_url = "https://docs.bigmailer.io/"
    # `get_schemas` iterates a static endpoint catalog with no I/O, so the table list is safe to
    # render in public docs without credentials.
    lists_tables_without_credentials = True

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

    @property
    def get_source_config(self) -> SourceConfig:
        return SourceConfig(
            name=ExternalDataSourceType.BIGMAILER,
            category=DataWarehouseSourceCategory.MARKETING___EMAIL,
            label="BigMailer",
            releaseStatus=ReleaseStatus.BETA,
            # Kept hidden from the connector catalog while the source is validated end-to-end; flip this
            # off (delete the line) to make it connectable.
            caption="""Enter your BigMailer API key to sync your BigMailer data into the PostHog Data warehouse.

Create an API key in your BigMailer console under **Account Settings → API Keys**. The key has account-wide access, so no extra scopes need to be granted.""",
            iconPath="/static/services/bigmailer.png",
            docsUrl="https://posthog.com/docs/cdp/sources/bigmailer",
            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.bigmailer.canonical_descriptions import CANONICAL_DESCRIPTIONS

        return CANONICAL_DESCRIPTIONS

    def get_non_retryable_errors(self) -> dict[str, str | None]:
        return {
            AUTH_ERROR_MESSAGE: "Your BigMailer API key is invalid or lacks the required permissions. Create a new key in your BigMailer console, then reconnect.",
        }

    def get_schemas(
        self,
        config: BigMailerSourceConfig,
        team_id: int,
        with_counts: bool = False,
        names: list[str] | None = None,
        force_refresh: bool = False,
        api_version: str | None = None,
    ) -> list[SourceSchema]:
        # BigMailer has no server-side timestamp filter on any list endpoint, and cursor pagination
        # doesn't accept a sort param — an "incremental" sync would still page through everything, so
        # every table is full refresh only (no incremental fields declared).
        return build_endpoint_schemas(
            ENDPOINTS,
            {},
            names,
            should_sync_default={
                endpoint: endpoint_config.should_sync_default
                for endpoint, endpoint_config in BIGMAILER_ENDPOINTS.items()
            },
        )

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

        return False, "Could not connect to BigMailer. Check your API key and try again."

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

    def source_for_pipeline(
        self,
        config: BigMailerSourceConfig,
        resumable_source_manager: ResumableSourceManager[BigMailerResumeConfig],
        inputs: SourceInputs,
    ) -> SourceResponse:
        return bigmailer_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,
        )