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.

6.1 KB
Raw Blame
from typing import Optional, cast

from products.warehouse_sources.sources.plivo._config import PlivoSourceConfig
from products.warehouse_sources.sources.plivo.plivo import (
    PlivoResumeConfig,
    plivo_source,
    validate_credentials as validate_plivo_credentials,
)
from products.warehouse_sources.sources.plivo.settings import (
    ENDPOINT_DESCRIPTIONS,
    ENDPOINTS,
    INCREMENTAL_FIELDS,
    PLIVO_BASE_URL,
    USAGE_SUMMARY,
)
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 PlivoSource(ResumableSource[PlivoSourceConfig, PlivoResumeConfig]):
    codebase_detection = detect(
        env=("^PLIVO_",),
        npm=("plivo",),
        pypi=("plivo",),
        composer=("plivo/plivo-php",),
        go=("github.com/plivo/plivo-go", "github.com/plivo/plivo-agentstack-go", "github.com/plivo/plivo-cli"),
        maven=("com.plivo:plivo-java", "com.plivo:plivo-stream-sdk-java"),
        rubygems=("plivo",),
        swiftpm=("github.com/plivo/plivo-webrtc-ios", "github.com/plivo/plivovoicekit-ios"),
    )

    lists_tables_without_credentials = True  # static endpoint catalog — safe for public docs
    api_docs_url = "https://www.plivo.com/docs/"

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

    @property
    def get_source_config(self) -> SourceConfig:
        return SourceConfig(
            name=ExternalDataSourceType.PLIVO,
            category=DataWarehouseSourceCategory.COMMUNICATION,
            label="Plivo",
            releaseStatus=ReleaseStatus.BETA,
            keywords=["sms", "voice", "cpaas"],
            caption=(
                "Enter your Plivo Auth ID and Auth Token to sync your SMS message and voice call "
                "detail records into the PostHog Data warehouse. Both are shown on the overview page "
                "of the [Plivo console](https://console.plivo.com/dashboard/).\n\n"
                "Plivo retains message and call records for **90 days**, so syncing them into the "
                "warehouse lets you keep history beyond that window."
            ),
            iconPath="/static/services/plivo.png",
            docsUrl="https://posthog.com/docs/cdp/sources/plivo",
            fields=cast(
                list[FieldType],
                [
                    SourceFieldInputConfig(
                        name="auth_id",
                        label="Auth ID",
                        type=SourceFieldInputConfigType.TEXT,
                        required=True,
                        placeholder="MAXXXXXXXXXXXXXXXXXX",
                        secret=False,
                    ),
                    SourceFieldInputConfig(
                        name="auth_token",
                        label="Auth token",
                        type=SourceFieldInputConfigType.PASSWORD,
                        required=True,
                        placeholder="",
                        secret=True,
                    ),
                ],
            ),
        )

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

        return CANONICAL_DESCRIPTIONS

    def get_non_retryable_errors(self) -> dict[str, str | None]:
        # Plivo returns 401 for a bad Auth ID/Token pair and 403 for a valid credential that lacks
        # access to the resource; neither can succeed on retry.
        auth_message = (
            "Plivo authentication failed. Check the Auth ID and Auth Token from your Plivo console and reconnect."
        )
        return {
            f"401 Client Error: Unauthorized for url: {PLIVO_BASE_URL}": auth_message,
            f"403 Client Error: Forbidden for url: {PLIVO_BASE_URL}": (
                "Your Plivo credentials don't have access to this resource. "
                "Check the account's permissions in the Plivo console."
            ),
        }

    def get_schemas(
        self,
        config: PlivoSourceConfig,
        team_id: int,
        with_counts: bool = False,
        names: list[str] | None = None,
        force_refresh: bool = False,
        api_version: str | None = None,
    ) -> list[SourceSchema]:
        return build_endpoint_schemas(
            ENDPOINTS,
            INCREMENTAL_FIELDS,
            names,
            descriptions=ENDPOINT_DESCRIPTIONS,
            # Each sync re-reads the watermark day, which append would write twice.
            merge_only=(USAGE_SUMMARY,),
        )

    def validate_credentials(
        self, config: PlivoSourceConfig, team_id: int, schema_name: Optional[str] = None, api_version: str | None = None
    ) -> tuple[bool, str | None]:
        if validate_plivo_credentials(config.auth_id, config.auth_token):
            return True, None

        return False, "Invalid Plivo Auth ID or Auth Token"

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

    def source_for_pipeline(
        self,
        config: PlivoSourceConfig,
        resumable_source_manager: ResumableSourceManager[PlivoResumeConfig],
        inputs: SourceInputs,
    ) -> SourceResponse:
        return plivo_source(
            auth_id=config.auth_id,
            auth_token=config.auth_token,
            endpoint=inputs.schema_name,
            resumable_source_manager=resumable_source_manager,
            should_use_incremental_field=inputs.should_use_incremental_field,
            db_incremental_field_last_value=inputs.db_incremental_field_last_value
            if inputs.should_use_incremental_field
            else None,
            incremental_field=inputs.incremental_field,
        )