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.0 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.thinkific._config import ThinkificSourceConfig
from products.warehouse_sources.sources.thinkific.settings import ENDPOINTS, INCREMENTAL_FIELDS, THINKIFIC_ENDPOINTS
from products.warehouse_sources.sources.thinkific.thinkific import (
    ThinkificResumeConfig,
    is_valid_subdomain,
    thinkific_source,
    validate_credentials as validate_thinkific_credentials,
)


@SourceRegistry.register
class ThinkificSource(ResumableSource[ThinkificSourceConfig, ThinkificResumeConfig]):
    codebase_detection = detect(env=("^THINKIFIC_",))

    api_docs_url = "https://developers.thinkific.com/api/api-documentation"

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

    @property
    def get_source_config(self) -> SourceConfig:
        return SourceConfig(
            name=ExternalDataSourceType.THINKIFIC,
            category=DataWarehouseSourceCategory.E_COMMERCE,
            label="Thinkific",
            caption="""Enter your Thinkific API key and account subdomain to pull your Thinkific course, enrollment, and order data into the PostHog Data warehouse.

You can create an API key under **Settings → Code & analytics → API** in your Thinkific admin. The subdomain is the `<subdomain>` part of your `<subdomain>.thinkific.com` admin URL.""",
            iconPath="/static/services/thinkific.png",
            docsUrl="https://posthog.com/docs/cdp/sources/thinkific",
            keywords=["lms", "elearning", "e-learning", "courses"],
            releaseStatus=ReleaseStatus.BETA,
            fields=cast(
                list[FieldType],
                [
                    SourceFieldInputConfig(
                        name="api_key",
                        label="API key",
                        type=SourceFieldInputConfigType.PASSWORD,
                        required=True,
                        placeholder="",
                        secret=True,
                    ),
                    SourceFieldInputConfig(
                        name="subdomain",
                        label="Subdomain",
                        type=SourceFieldInputConfigType.TEXT,
                        required=True,
                        placeholder="mycompany",
                        secret=False,
                    ),
                ],
            ),
        )

    def get_non_retryable_errors(self) -> dict[str, str | None]:
        return {
            # An invalid/revoked API key or wrong subdomain surfaces as an HTTPError when `_fetch_page`
            # calls `raise_for_status()`. Retrying can never fix a credential problem, so stop the sync.
            "401 Client Error: Unauthorized for url: https://api.thinkific.com": "Your Thinkific API key or subdomain is invalid. Create a new API key in your Thinkific admin (Settings → Code & analytics → API) and confirm the subdomain, then reconnect.",
            "403 Client Error: Forbidden for url: https://api.thinkific.com": "Your Thinkific API key does not have permission to access this data. Check the key in your Thinkific admin, then reconnect.",
        }

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

        return CANONICAL_DESCRIPTIONS

    def get_schemas(
        self,
        config: ThinkificSourceConfig,
        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: ThinkificSourceConfig,
        team_id: int,
        schema_name: Optional[str] = None,
        api_version: str | None = None,
    ) -> tuple[bool, str | None]:
        if not is_valid_subdomain(config.subdomain):
            return False, "Thinkific subdomain is invalid"

        endpoint_path = THINKIFIC_ENDPOINTS[schema_name].path if schema_name in THINKIFIC_ENDPOINTS else "/courses"
        is_valid, status_code = validate_thinkific_credentials(config.api_key, config.subdomain, endpoint_path)
        if is_valid:
            return True, None

        # Accept a 403 at source-create (schema_name is None): the key is genuine but lacks access to
        # the probed endpoint, which is fine when the user only wants to sync endpoints they can read.
        # Per-schema checks (schema_name set) still surface the 403.
        if status_code == 403 and schema_name is None:
            return True, None

        return False, "Invalid Thinkific API key or subdomain"

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

    def source_for_pipeline(
        self,
        config: ThinkificSourceConfig,
        resumable_source_manager: ResumableSourceManager[ThinkificResumeConfig],
        inputs: SourceInputs,
    ) -> SourceResponse:
        return thinkific_source(
            api_key=config.api_key,
            subdomain=config.subdomain,
            endpoint=inputs.schema_name,
            team_id=inputs.team_id,
            job_id=inputs.job_id,
            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,
        )