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

from products.warehouse_sources.sources.coda._config import CodaSourceConfig
from products.warehouse_sources.sources.coda.coda import (
    CodaResumeConfig,
    coda_source,
    validate_credentials as validate_coda_credentials,
)
from products.warehouse_sources.sources.coda.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 CodaSource(ResumableSource[CodaSourceConfig, CodaResumeConfig]):
    codebase_detection = detect(env=("^CODA_API_",), npm=("@codahq/packs-sdk",), pypi=("codaio",))

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

    supported_versions = ("v1",)
    default_version = "v1"
    api_docs_url = "https://coda.io/developers/apis/v1"

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

    def get_non_retryable_errors(self) -> dict[str, str | None]:
        return {
            "401 Client Error: Unauthorized for url: https://coda.io": "Coda authentication failed. Please check your API token.",
            "403 Client Error: Forbidden for url: https://coda.io": "Coda denied access. Please check that your API token can read the requested docs.",
        }

    @property
    def get_source_config(self) -> SourceConfig:
        return SourceConfig(
            name=ExternalDataSourceType.CODA,
            category=DataWarehouseSourceCategory.PRODUCTIVITY,
            label="Coda",
            caption="""Enter your Coda API token to pull your docs, tables, and rows into the PostHog Data warehouse.

You can generate an API token in [Coda account settings](https://coda.io/account). The token only sees docs its creator can access. Rows are synced from every table of every doc — note Coda's doc-listing rate limit makes large workspaces slow to sync. Workspace users and role activity sync only for workspaces where the token's creator is an admin.""",
            iconPath="/static/services/coda.png",
            docsUrl="https://posthog.com/docs/cdp/sources/coda",
            releaseStatus=ReleaseStatus.GA,
            fields=cast(
                list[FieldType],
                [
                    SourceFieldInputConfig(
                        name="api_token",
                        label="API token",
                        type=SourceFieldInputConfigType.PASSWORD,
                        required=True,
                        placeholder="",
                        secret=True,
                    ),
                ],
            ),
        )

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

        return CANONICAL_DESCRIPTIONS

    def get_schemas(
        self,
        config: CodaSourceConfig,
        team_id: int,
        with_counts: bool = False,
        names: list[str] | None = None,
        force_refresh: bool = False,
        api_version: str | None = None,
    ) -> list[SourceSchema]:
        # Coda's list endpoints have no updated-since filters; full refresh only.
        return build_endpoint_schemas(ENDPOINTS, INCREMENTAL_FIELDS, names)

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

        return False, "Invalid Coda API token"

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

    def source_for_pipeline(
        self,
        config: CodaSourceConfig,
        resumable_source_manager: ResumableSourceManager[CodaResumeConfig],
        inputs: SourceInputs,
    ) -> SourceResponse:
        return coda_source(
            api_token=config.api_token,
            endpoint=inputs.schema_name,
            team_id=inputs.team_id,
            job_id=inputs.job_id,
            resumable_source_manager=resumable_source_manager,
        )