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

from products.warehouse_sources.sources.bamboohr._config import BambooHRSourceConfig
from products.warehouse_sources.sources.bamboohr.bamboohr import (
    BambooHRResumeConfig,
    bamboohr_source,
    validate_credentials as validate_bamboohr_credentials,
)
from products.warehouse_sources.sources.bamboohr.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 BambooHRSource(ResumableSource[BambooHRSourceConfig, BambooHRResumeConfig]):
    codebase_detection = detect(env=("^BAMBOOHR_", "^BAMBOO_HR_"), pypi=("pybamboohr",), composer=("bamboohr/api",))

    supported_versions = ("v1",)
    default_version = "v1"
    api_docs_url = "https://documentation.bamboohr.com"

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

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

    @property
    def get_source_config(self) -> SourceConfig:
        return SourceConfig(
            name=ExternalDataSourceType.BAMBOOHR,
            category=DataWarehouseSourceCategory.HR___RECRUITING,
            label="BambooHR",
            releaseStatus=ReleaseStatus.BETA,
            caption="""Enter your BambooHR company subdomain and API key to pull your HR data into the PostHog Data warehouse.

You can generate an API key from your BambooHR account under **Account Settings → API Keys** (requires admin permissions).

Make sure your API key has access to the data you want to sync (employee, time off, and account metadata).""",
            iconPath="/static/services/bamboohr.png",
            docsUrl="https://posthog.com/docs/cdp/sources/bamboohr",
            fields=cast(
                list[FieldType],
                [
                    SourceFieldInputConfig(
                        name="subdomain",
                        label="Company subdomain",
                        type=SourceFieldInputConfigType.TEXT,
                        required=True,
                        placeholder="yourcompany",
                        secret=False,
                    ),
                    SourceFieldInputConfig(
                        name="api_key",
                        label="API key",
                        type=SourceFieldInputConfigType.PASSWORD,
                        required=True,
                        placeholder="",
                        secret=True,
                    ),
                ],
            ),
        )

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

        return CANONICAL_DESCRIPTIONS

    def get_schemas(
        self,
        config: BambooHRSourceConfig,
        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,
            # Every BambooHR stream that syncs incrementally is an employee-table history table
            # whose rows update in place, so appending would stack a copy of each row every sync.
            merge_only=[name for name, fields in INCREMENTAL_FIELDS.items() if fields],
        )

    def validate_credentials(
        self,
        config: BambooHRSourceConfig,
        team_id: int,
        schema_name: Optional[str] = None,
        api_version: str | None = None,
    ) -> tuple[bool, str | None]:
        return validate_bamboohr_credentials(config.subdomain, config.api_key, schema_name)

    def get_non_retryable_errors(self) -> dict[str, str | None]:
        return {
            "401 Client Error: Unauthorized for url": "Your BambooHR API key is invalid or expired. Please generate a new key and reconnect.",
            "403 Client Error: Forbidden for url": "Your BambooHR API key does not have the required permissions. Please check the key permissions and try again.",
        }

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

    def source_for_pipeline(
        self,
        config: BambooHRSourceConfig,
        resumable_source_manager: ResumableSourceManager[BambooHRResumeConfig],
        inputs: SourceInputs,
    ) -> SourceResponse:
        return bamboohr_source(
            subdomain=config.subdomain,
            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,
            should_use_incremental_field=inputs.should_use_incremental_field,
            db_incremental_field_last_value=inputs.db_incremental_field_last_value,
        )