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.drip._config import DripSourceConfig
from products.warehouse_sources.sources.drip.drip import (
    DripResumeConfig,
    drip_source,
    validate_credentials as validate_drip_credentials,
)
from products.warehouse_sources.sources.drip.settings import ENDPOINTS, INCREMENTAL_FIELDS
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 DripSource(ResumableSource[DripSourceConfig, DripResumeConfig]):
    codebase_detection = detect(
        env=("^DRIP_",), script_hosts=("tag.getdrip.com",), npm=("drip-nodejs",), rubygems=("drip-ruby",)
    )

    supported_versions = ("v2",)
    default_version = "v2"
    api_docs_url = "https://developer.drip.com"

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

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

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

        return CANONICAL_DESCRIPTIONS

    def get_non_retryable_errors(self) -> dict[str, str | None]:
        return {
            "401 Client Error: Unauthorized for url: https://api.getdrip.com": "Your Drip API token is invalid or expired. Please generate a new token and reconnect.",
            "403 Client Error: Forbidden for url: https://api.getdrip.com": "Your Drip API token does not have access to this account. Please check the token and account ID.",
        }

    def get_schemas(
        self,
        config: DripSourceConfig,
        team_id: int,
        with_counts: bool = False,
        names: list[str] | None = None,
        force_refresh: bool = False,
        api_version: str | None = None,
    ) -> list[SourceSchema]:
        # Every Drip endpoint is full refresh (no reliable server-side update cursor), so no endpoint
        # carries incremental fields and build_endpoint_schemas yields supports_incremental=False /
        # supports_append=False for all of them.
        return build_endpoint_schemas(ENDPOINTS, INCREMENTAL_FIELDS, names)

    def validate_credentials(
        self, config: DripSourceConfig, team_id: int, schema_name: Optional[str] = None, api_version: str | None = None
    ) -> tuple[bool, str | None]:
        return validate_drip_credentials(config.api_token, config.account_id)

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

    def source_for_pipeline(
        self,
        config: DripSourceConfig,
        resumable_source_manager: ResumableSourceManager[DripResumeConfig],
        inputs: SourceInputs,
    ) -> SourceResponse:
        return drip_source(
            api_token=config.api_token,
            account_id=config.account_id,
            endpoint=inputs.schema_name,
            team_id=inputs.team_id,
            job_id=inputs.job_id,
            resumable_source_manager=resumable_source_manager,
            db_incremental_field_last_value=None,  # every Drip endpoint is full refresh
        )

    @property
    def get_source_config(self) -> SourceConfig:
        return SourceConfig(
            name=ExternalDataSourceType.DRIP,
            category=DataWarehouseSourceCategory.MARKETING___EMAIL,
            label="Drip",
            caption="""Enter your Drip API token and account ID to pull your Drip data into the PostHog Data warehouse.

You can find your API token under **Settings → User Settings → API** in Drip, and your account ID under **Settings → Account → General Info** (it's the numeric ID in your Drip dashboard URL).""",
            iconPath="/static/services/drip.png",
            docsUrl="https://posthog.com/docs/cdp/sources/drip",
            releaseStatus=ReleaseStatus.GA,
            fields=cast(
                list[FieldType],
                [
                    SourceFieldInputConfig(
                        name="api_token",
                        label="API token",
                        type=SourceFieldInputConfigType.PASSWORD,
                        required=True,
                        placeholder="",
                        secret=True,
                    ),
                    SourceFieldInputConfig(
                        name="account_id",
                        label="Account ID",
                        type=SourceFieldInputConfigType.TEXT,
                        required=True,
                        placeholder="",
                        secret=False,
                    ),
                ],
            ),
        )