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

from products.warehouse_sources.sources.quo._config import QuoSourceConfig
from products.warehouse_sources.sources.quo.quo import (
    QuoResumeConfig,
    quo_source,
    validate_credentials as validate_quo_credentials,
)
from products.warehouse_sources.sources.quo.settings import (
    DATED_ENDPOINTS_BY_VERSION,
    ENDPOINTS,
    INCREMENTAL_FIELDS,
    QUO_API_VERSION_2026_03_30,
    QUO_API_VERSION_V1,
    QUO_BASE_URL,
)
from products.warehouse_sources.sources.sdk import (
    NOT_DETECTABLE,
    CanonicalDescriptions,
    DataWarehouseSourceCategory,
    ExternalDataSourceType,
    FieldType,
    ReleaseStatus,
    ResumableSource,
    ResumableSourceManager,
    SourceConfig,
    SourceFieldInputConfig,
    SourceFieldInputConfigType,
    SourceInputs,
    SourceRegistry,
    SourceResponse,
    SourceSchema,
    build_endpoint_schemas,
)


@SourceRegistry.register
class QuoSource(ResumableSource[QuoSourceConfig, QuoResumeConfig]):
    codebase_detection = NOT_DETECTABLE

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

    supported_versions = (QUO_API_VERSION_V1, QUO_API_VERSION_2026_03_30)
    default_version = QUO_API_VERSION_2026_03_30
    api_docs_url = "https://www.quo.com/docs/mdx/api-reference/changelog"

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

    def get_non_retryable_errors(self) -> dict[str, str | None]:
        errors: dict[str, str | None] = {
            "401 Client Error: Unauthorized for url: https://api.quo.com": "Quo authentication failed. Check that your API key is correct and still active, then reconnect.",
            "403 Client Error: Forbidden for url: https://api.quo.com": "Quo denied access. Check that the API key was created by a workspace owner or admin.",
        }
        # Dated versions answer a missing or unsupported Quo-Api-Version header with a 400, which no retry fixes.
        # Keyed per dated path so v1 URLs, which all start with /v1/, never match.
        for dated_endpoints in DATED_ENDPOINTS_BY_VERSION.values():
            for config in dated_endpoints.values():
                errors[f"400 Client Error: Bad Request for url: {QUO_BASE_URL}{config.path}"] = (
                    "Quo rejected the request. Check that Quo still supports the API version this source uses."
                )
        return errors

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

        return CANONICAL_DESCRIPTIONS

    def get_schemas(
        self,
        config: QuoSourceConfig,
        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)

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

        return False, "Invalid Quo API key"

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

    def source_for_pipeline(
        self,
        config: QuoSourceConfig,
        resumable_source_manager: ResumableSourceManager[QuoResumeConfig],
        inputs: SourceInputs,
    ) -> SourceResponse:
        return quo_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,
            api_version=self.resolve_api_version(inputs.api_version),
            should_use_incremental_field=inputs.should_use_incremental_field,
            db_incremental_field_last_value=inputs.db_incremental_field_last_value,
            incremental_field=inputs.incremental_field,
        )

    @property
    def get_source_config(self) -> SourceConfig:
        return SourceConfig(
            name=ExternalDataSourceType.QUO,
            category=DataWarehouseSourceCategory.COMMUNICATION,
            label="Quo",
            caption="""Enter your Quo API key to pull your Quo (formerly OpenPhone) data into the PostHog Data warehouse.

You can generate an API key in your Quo workspace settings, under the API tab. You need workspace owner or admin access to do this.""",
            keywords=["openphone", "phone", "sms", "calls"],
            iconPath="/static/services/quo.png",
            docsUrl="https://posthog.com/docs/cdp/sources/quo",
            releaseStatus=ReleaseStatus.GA,
            fields=cast(
                list[FieldType],
                [
                    SourceFieldInputConfig(
                        name="api_key",
                        label="API key",
                        type=SourceFieldInputConfigType.PASSWORD,
                        required=True,
                        placeholder="",
                        secret=True,
                    ),
                ],
            ),
        )