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

from products.warehouse_sources.sources.deel._config import DeelSourceConfig
from products.warehouse_sources.sources.deel.deel import (
    DeelResumeConfig,
    deel_source,
    validate_credentials as validate_deel_credentials,
)
from products.warehouse_sources.sources.deel.settings import (
    DEEL_API_VERSION_2026_01_01,
    DEEL_API_VERSION_V2,
    endpoints_for_version,
)
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 DeelSource(ResumableSource[DeelSourceConfig, DeelResumeConfig]):
    codebase_detection = detect(env=("^DEEL_",))

    supported_versions = (DEEL_API_VERSION_V2, DEEL_API_VERSION_2026_01_01)
    default_version = DEEL_API_VERSION_2026_01_01
    api_docs_url = "https://developer.deel.com/api/api-versioning"

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

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

    def get_non_retryable_errors(self) -> dict[str, str | None]:
        return {
            "401 Client Error: Unauthorized for url: https://api.letsdeel.com": "Deel authentication failed. Please check your API token.",
            "403 Client Error: Forbidden for url: https://api.letsdeel.com": "Deel denied access. Please check that your API token has the read scope for this dataset.",
            "410 Client Error: Gone for url: https://api.letsdeel.com": "Deel has retired the API version this source uses. Contact PostHog support to move the source to a supported version.",
        }

    @property
    def get_source_config(self) -> SourceConfig:
        return SourceConfig(
            name=ExternalDataSourceType.DEEL,
            category=DataWarehouseSourceCategory.HR___RECRUITING,
            label="Deel",
            caption="""Enter your Deel API token to pull your Deel workforce and payroll data into the PostHog Data warehouse.

Create an organization token in [Deel](https://app.deel.com/developer-center) under More > Developer with read scopes for the data you want to sync (`people:read`, `contracts:read`, `accounting:read`, `timesheets:read`, `time-off:read`, `legal-entity:read`, `organizations:read`, `groups:read`, `global-payroll:read`, `payslips:read`, `it-seats:read`, `it-service-requests:read`, `equities:read`). Prefer an organization token over a personal token — personal tokens stop working when the user leaves the organization.""",
            iconPath="/static/services/deel.png",
            docsUrl="https://posthog.com/docs/cdp/sources/deel",
            releaseStatus=ReleaseStatus.BETA,
            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.deel.canonical_descriptions import CANONICAL_DESCRIPTIONS

        return CANONICAL_DESCRIPTIONS

    def get_schemas(
        self,
        config: DeelSourceConfig,
        team_id: int,
        with_counts: bool = False,
        names: list[str] | None = None,
        force_refresh: bool = False,
        api_version: str | None = None,
    ) -> list[SourceSchema]:
        # Core Deel objects have no updated-since filter, so every stream is an
        # honest full refresh (no incremental fields).
        return build_endpoint_schemas(endpoints_for_version(self.resolve_api_version(api_version)), {}, names)

    def validate_credentials(
        self, config: DeelSourceConfig, team_id: int, schema_name: Optional[str] = None, api_version: str | None = None
    ) -> tuple[bool, str | None]:
        return validate_deel_credentials(config.api_token, self.resolve_api_version(api_version))

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

    def source_for_pipeline(
        self,
        config: DeelSourceConfig,
        resumable_source_manager: ResumableSourceManager[DeelResumeConfig],
        inputs: SourceInputs,
    ) -> SourceResponse:
        return deel_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,
            api_version=self.resolve_api_version(inputs.api_version),
            db_incremental_field_last_value=None,  # every Deel endpoint is full refresh
        )