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

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,
)
from products.warehouse_sources.sources.vendr._config import VendrSourceConfig
from products.warehouse_sources.sources.vendr.settings import ENDPOINTS, VENDR_ENDPOINTS
from products.warehouse_sources.sources.vendr.vendr import (
    VendrResumeConfig,
    validate_credentials as validate_vendr_credentials,
    vendr_source,
)


@SourceRegistry.register
class VendrSource(ResumableSource[VendrSourceConfig, VendrResumeConfig]):
    codebase_detection = detect(env=("^VENDR_",))

    # Every endpoint here is a static entry in ENDPOINTS with no I/O - safe for public docs.
    lists_tables_without_credentials = True
    # Vendr's OpenPrice API has no versioned path segment, version header, or dated release -
    # https://api.vendr.com is the only documented base.
    api_docs_url = "https://developers.vendr.com/docs/introduction"

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

    @property
    def get_source_config(self) -> SourceConfig:
        return SourceConfig(
            name=ExternalDataSourceType.VENDR,
            category=DataWarehouseSourceCategory.FINANCE___ACCOUNTING,
            label="Vendr (OpenPrice API)",
            releaseStatus=ReleaseStatus.BETA,
            caption="Sync Vendr's OpenPrice software catalog: companies, products, product "
            "families, and categories. Access is partnership-gated - email "
            "[[email protected]](mailto:[email protected]) to request an API key.",
            iconPath="/static/services/vendr.png",
            docsUrl="https://posthog.com/docs/cdp/sources/vendr",
            fields=cast(
                list[FieldType],
                [
                    SourceFieldInputConfig(
                        name="api_key",
                        label="API key",
                        type=SourceFieldInputConfigType.PASSWORD,
                        required=True,
                        placeholder="",
                        secret=True,
                    ),
                ],
            ),
        )

    def get_non_retryable_errors(self) -> dict[str, str | None]:
        return {
            "401 Client Error: Unauthorized for url": "Your Vendr API key is invalid or has been revoked. "
            "Contact [email protected] to get a new key, then reconnect.",
            "403 Client Error: Forbidden for url": "Your Vendr API key does not have access to the catalog. "
            "Check your partnership terms with Vendr, then reconnect.",
        }

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

        return CANONICAL_DESCRIPTIONS

    def get_schemas(
        self,
        config: VendrSourceConfig,
        team_id: int,
        with_counts: bool = False,
        names: list[str] | None = None,
        force_refresh: bool = False,
        api_version: str | None = None,
    ) -> list[SourceSchema]:
        schemas = build_endpoint_schemas(ENDPOINTS, {}, names)
        for schema in schemas:
            schema.detected_primary_keys = VENDR_ENDPOINTS[schema.name].primary_keys
        return schemas

    def validate_credentials(
        self,
        config: VendrSourceConfig,
        team_id: int,
        schema_name: Optional[str] = None,
        api_version: str | None = None,
    ) -> tuple[bool, str | None]:
        ok, _status_code = validate_vendr_credentials(config.api_key)
        if ok:
            return True, None
        return False, "Invalid Vendr API key"

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

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