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.

20.8 KB
Raw Blame
from collections.abc import Iterable, Iterator
from functools import partial
from typing import Any, Optional, cast

from requests import Request, Response, Session

from products.warehouse_sources.sources.deel.settings import (
    DEEL_API_VERSION_2026_01_01,
    DEEL_API_VERSION_V2,
    DEEL_ENDPOINTS,
    GROSS_TO_NET_ENDPOINT,
    GROSS_TO_NET_PARENT,
    GROSS_TO_NET_ROOT,
    TIME_OFF_EVENTS_ENDPOINT,
    TIME_OFF_EVENTS_PARENT,
    DeelEndpointConfig,
)
from products.warehouse_sources.sources.sdk import (
    BasePaginator,
    ClientConfig,
    Endpoint,
    OffsetPaginator,
    RESTAPIConfig,
    ResumableSourceManager,
    SinglePagePaginator,
    SourceResponse,
    build_dependent_resource,
    frozen,
    make_tracked_session,
    rest_api_resource,
    validate_via_probe,
)

DEEL_BASE_URLS = {
    DEEL_API_VERSION_V2: "https://api.letsdeel.com/rest/v2",
    DEEL_API_VERSION_2026_01_01: "https://api.letsdeel.com/rest",
}
REQUEST_TIMEOUT_SECONDS = 30

# Time-off rows carry absence reasons and family/medical event details, timesheets carry a
# free-text work description plus a reviewer's remarks, the trackers carry worker names and work
# emails, IT seats carry the holder's address and identity documents, and gross-to-net and equity
# awards carry per-worker pay — none of it name-tagged in a way the name-based sample scrubbers can
# spot.
_UNCAPTURED_ENDPOINTS = frozenset(
    {
        "time_offs",
        TIME_OFF_EVENTS_ENDPOINT,
        "it_seats",
        "equity_awards",
        "timesheets",
        "onboarding_tracker",
        "offboarding_tracker",
        GROSS_TO_NET_ENDPOINT,
    }
)


@frozen
class DeelResumeConfig:
    # Offset-paginated endpoints persist the offset; the keyset endpoints persist Deel's opaque
    # next-page cursor instead.
    offset: Optional[int] = None
    cursor: Optional[str] = None
    # Fan-out endpoints resume per parent — see
    # `common.rest_source.__init__._make_paginate_dependent_resource`. Gross-to-net reuses
    # `completed` for the payroll cycles it has already emitted.
    completed: Optional[list[str]] = None
    current: Optional[str] = None
    child_state: Optional[dict[str, Any]] = None


def _find(body: Any, path: tuple[str, ...]) -> Any:
    for key in path:
        if not isinstance(body, dict):
            return None
        body = body.get(key)
    return body


class DeelCursorPaginator(BasePaginator):
    """Keyset paginator for Deel's cursor endpoints.

    The next cursor's body path and the query param that sends it back vary per endpoint.
    Terminate when the response carries no next cursor, publishes a false "has more" flag,
    returns no rows, OR echoes back the same cursor it was just sent — Deel can return a stale
    or unchanged cursor on an exhausted keyset while still claiming more pages exist, so both
    an empty page and a repeated cursor must stop the walk rather than loop forever.
    """

    def __init__(
        self,
        cursor_param: str = "after_cursor",
        cursor_path: tuple[str, ...] = ("page", "cursor"),
        has_more_path: Optional[tuple[str, ...]] = None,
    ) -> None:
        super().__init__()
        self.cursor_param = cursor_param
        self.cursor_path = cursor_path
        self.has_more_path = has_more_path
        self._cursor: Optional[str] = None

    def _inject(self, request: Request) -> None:
        if self._cursor is not None:
            if request.params is None:
                request.params = {}
            request.params[self.cursor_param] = self._cursor

    def init_request(self, request: Request) -> None:
        # Applies a seeded resume cursor to the first request.
        self._inject(request)

    def update_state(self, response: Response, data: Optional[list[Any]] = None) -> None:
        if not data:
            self._has_next_page = False
            return
        try:
            body = response.json()
        except Exception:
            self._has_next_page = False
            return
        if self.has_more_path is not None and not _find(body, self.has_more_path):
            self._has_next_page = False
            return
        next_cursor = _find(body, self.cursor_path)
        if next_cursor and str(next_cursor) != self._cursor:
            self._cursor = str(next_cursor)
            self._has_next_page = True
        else:
            self._has_next_page = False

    def update_request(self, request: Request) -> None:
        self._inject(request)

    def get_resume_state(self) -> Optional[dict[str, Any]]:
        return {"cursor": self._cursor} if self._has_next_page and self._cursor is not None else None

    def set_resume_state(self, state: dict[str, Any]) -> None:
        cursor = state.get("cursor")
        if cursor is not None:
            self._cursor = cursor
            self._has_next_page = True


def _make_paginator(config: DeelEndpointConfig) -> BasePaginator:
    if config.pagination == "cursor":
        return DeelCursorPaginator(
            cursor_param=config.cursor_param,
            cursor_path=config.cursor_path,
            has_more_path=config.has_more_path,
        )
    if config.pagination == "offset":
        # OffsetPaginator injects both limit and offset; termination is a short/empty page
        # (Deel exposes no dependable top-level total).
        return OffsetPaginator(limit=config.page_size, total_path=None)
    return SinglePagePaginator()


def _base_url(api_version: str) -> str:
    base_url = DEEL_BASE_URLS.get(api_version)
    if base_url is None:
        raise ValueError(f"Unsupported Deel API version: {api_version}")
    return base_url


def _version_headers(api_version: str, config: DeelEndpointConfig) -> dict[str, str]:
    # The legacy pin keeps sending no selector, so its requests stay exactly as they were.
    if api_version == DEEL_API_VERSION_V2:
        return {}
    return {"X-Version": config.x_version}


def _client_config(api_token: str, config: DeelEndpointConfig, api_version: str) -> ClientConfig:
    client_config: ClientConfig = {
        "base_url": _base_url(api_version),
        # Bearer auth via the framework so the token is redacted from logs.
        "auth": {"type": "bearer", "token": api_token},
    }
    version_headers = _version_headers(api_version, config)
    if version_headers:
        client_config["headers"] = version_headers
    if config.name in _UNCAPTURED_ENDPOINTS:
        client_config["capture"] = False
    return client_config


def _base_params(config: DeelEndpointConfig) -> dict[str, Any]:
    params: dict[str, Any] = dict(config.params)
    # The offset paginator injects its own limit; the cursor endpoints need it sent up front.
    if config.pagination == "cursor" and config.page_size_param is not None:
        params[config.page_size_param] = config.page_size
    return params


def validate_credentials(api_token: str, api_version: str) -> tuple[bool, str | None]:
    """Confirm the API token is valid with a cheap one-person listing probe.

    Scoped tokens may lack individual resource scopes (403); only 401 means the
    token itself is bad. A transient network failure surfaces as a distinct
    "could not reach Deel" error so it isn't mistaken for a bad token."""
    _ok, status = validate_via_probe(
        lambda: make_tracked_session(redact_values=(api_token,)),
        f"{_base_url(api_version)}/people?limit=1",
        headers={"Authorization": f"Bearer {api_token}", **_version_headers(api_version, DEEL_ENDPOINTS["people"])},
    )

    if status is None:
        return False, "Could not reach Deel"
    if status == 401:
        return False, "Invalid Deel API token"
    return True, None


def _top_level_items(
    config: DeelEndpointConfig,
    api_token: str,
    team_id: int,
    job_id: str,
    resumable_source_manager: ResumableSourceManager[DeelResumeConfig],
    db_incremental_field_last_value: Optional[Any],
    api_version: str,
) -> Iterable[Any]:
    resume = resumable_source_manager.load_state() if resumable_source_manager.can_resume() else None

    initial_paginator_state: Optional[dict[str, Any]] = None
    if resume is not None:
        if config.pagination == "cursor" and resume.cursor is not None:
            initial_paginator_state = {"cursor": resume.cursor}
        elif config.pagination == "offset" and resume.offset is not None:
            initial_paginator_state = {"offset": resume.offset}

    def save_checkpoint(state: Optional[dict[str, Any]]) -> None:
        # Persist only when a next page remains; save AFTER a page is yielded so a crash
        # re-yields the last page (merge dedupes) rather than skipping it.
        if not state:
            return
        if config.pagination == "cursor" and state.get("cursor") is not None:
            resumable_source_manager.save_state(DeelResumeConfig(cursor=str(state["cursor"])))
        elif config.pagination == "offset" and state.get("offset") is not None:
            resumable_source_manager.save_state(DeelResumeConfig(offset=int(state["offset"])))

    client_config = _client_config(api_token, config, api_version)
    client_config["paginator"] = _make_paginator(config)

    rest_config: RESTAPIConfig = {
        "client": client_config,
        "resources": [
            {
                "name": config.name,
                "endpoint": {
                    "path": config.path,
                    "params": _base_params(config),
                    # A missing `data` key is treated as an empty page (lenient), matching the
                    # hand-rolled `body.get("data", [])` — not a fail-loud like other sources.
                    "data_selector": config.data_selector,
                },
            }
        ],
    }

    return rest_api_resource(
        rest_config,
        team_id,
        job_id,
        db_incremental_field_last_value,
        resume_hook=save_checkpoint,
        initial_paginator_state=initial_paginator_state,
    )


def _fanout_items(
    config: DeelEndpointConfig,
    api_token: str,
    team_id: int,
    job_id: str,
    resumable_source_manager: ResumableSourceManager[DeelResumeConfig],
    db_incremental_field_last_value: Optional[Any],
    api_version: str,
) -> Iterable[Any]:
    fanout = config.fanout
    assert fanout is not None
    parent_config = DEEL_ENDPOINTS[fanout.parent_name]

    resume = resumable_source_manager.load_state() if resumable_source_manager.can_resume() else None
    initial_state: Optional[dict[str, Any]] = None
    if resume is not None and (resume.completed or resume.current):
        initial_state = {
            "completed": resume.completed or [],
            "current": resume.current,
            "child_state": resume.child_state,
        }

    def save_checkpoint(state: Optional[dict[str, Any]]) -> None:
        if state is not None:
            resumable_source_manager.save_state(
                DeelResumeConfig(
                    completed=state.get("completed"),
                    current=state.get("current"),
                    child_state=state.get("child_state"),
                )
            )

    parent_endpoint: Endpoint = {
        "paginator": _make_paginator(parent_config),
        "data_selector": parent_config.data_selector,
    }
    child_endpoint: Endpoint = {
        "paginator": _make_paginator(config),
        "data_selector": config.data_selector,
    }

    return cast(
        Iterable[Any],
        build_dependent_resource(
            endpoint_configs=DEEL_ENDPOINTS,
            child_endpoint=config.name,
            fanout=fanout,
            client_config=_client_config(api_token, config, api_version),
            path_format_values={},
            team_id=team_id,
            job_id=job_id,
            db_incremental_field_last_value=db_incremental_field_last_value,
            # Both hops take their page size from fanout.parent_params, or none at all.
            page_size_param=None,
            parent_endpoint_extra=parent_endpoint,
            child_endpoint_extra=child_endpoint,
            resume_hook=save_checkpoint,
            initial_paginator_state=initial_state,
        ),
    )


def _time_off_event_items(
    config: DeelEndpointConfig,
    api_token: str,
    team_id: int,
    job_id: str,
    resumable_source_manager: ResumableSourceManager[DeelResumeConfig],
    api_version: str,
) -> Iterator[list[dict[str, Any]]]:
    """Yield one batch of time-off event rows per page of people.

    Hand-rolled rather than a dependent resource because Deel takes the worker profile as a query
    param, and the shared fan-out helper binds path params only.
    """
    parent_config = DEEL_ENDPOINTS[TIME_OFF_EVENTS_PARENT]

    resume = resumable_source_manager.load_state() if resumable_source_manager.can_resume() else None
    initial_paginator_state = {"offset": resume.offset} if resume is not None and resume.offset is not None else None

    def save_checkpoint(state: Optional[dict[str, Any]]) -> None:
        # The hook runs when this generator asks for the next page, so after its rows are out.
        if state and state.get("offset") is not None:
            resumable_source_manager.save_state(DeelResumeConfig(offset=int(state["offset"])))

    parent_client_config = _client_config(api_token, parent_config, api_version)
    parent_client_config["paginator"] = _make_paginator(parent_config)

    parent_rest_config: RESTAPIConfig = {
        "client": parent_client_config,
        "resources": [
            {
                "name": TIME_OFF_EVENTS_PARENT,
                "endpoint": {
                    "path": parent_config.path,
                    "params": _base_params(parent_config),
                    "data_selector": parent_config.data_selector,
                },
            }
        ],
    }

    people = rest_api_resource(
        parent_rest_config,
        team_id,
        job_id,
        None,
        resume_hook=save_checkpoint,
        initial_paginator_state=initial_paginator_state,
    )

    session = make_tracked_session(redact_values=(api_token,), capture=False)
    headers = {
        "Authorization": f"Bearer {api_token}",
        "Accept": "application/json",
        **_version_headers(api_version, config),
    }
    base_url = _base_url(api_version)

    for page in people:
        rows: list[dict[str, Any]] = []
        for person in page:
            hris_profile_id = person.get("id")
            if hris_profile_id is None:
                continue
            response = session.get(
                f"{base_url}{config.path}",
                params={"hris_profile_id": str(hris_profile_id)},
                headers=headers,
                timeout=REQUEST_TIMEOUT_SECONDS,
            )
            if response.status_code == 404:
                # The person went away between the listing and this fetch.
                continue
            response.raise_for_status()
            body = response.json()
            events = body.get("data") if isinstance(body, dict) else None
            for event in events or []:
                # Deel omits hris_profile_id from some event rows, and it is half the primary key.
                rows.append({**event, "hris_profile_id": event.get("hris_profile_id") or str(hris_profile_id)})
        if rows:
            yield rows


def _iter_cursor_pages(
    session: Session,
    url: str,
    config: DeelEndpointConfig,
    headers: dict[str, str],
    ignore_statuses: tuple[int, ...] = (),
) -> Iterator[list[dict[str, Any]]]:
    """Walk a Deel keyset endpoint by hand, with the same termination rules as DeelCursorPaginator.

    Used where the endpoint sits too deep for the shared fan-out helper to bind its path params.
    """
    params = _base_params(config)
    cursor: Optional[str] = None

    while True:
        if cursor is not None:
            params[config.cursor_param] = cursor
        response = session.get(url, params=params, headers=headers, timeout=REQUEST_TIMEOUT_SECONDS)
        if response.status_code in ignore_statuses:
            return
        response.raise_for_status()
        body = response.json()

        rows = _find(body, tuple(config.data_selector.split(".")))
        if not rows:
            return
        yield rows

        if config.has_more_path is not None and not _find(body, config.has_more_path):
            return
        next_cursor = _find(body, config.cursor_path)
        if not next_cursor or str(next_cursor) == cursor:
            return
        cursor = str(next_cursor)


def _gross_to_net_items(
    config: DeelEndpointConfig,
    api_token: str,
    resumable_source_manager: ResumableSourceManager[DeelResumeConfig],
    api_version: str,
) -> Iterator[list[dict[str, Any]]]:
    """Yield gross-to-net rows per payroll cycle.

    Hand-rolled because the cycle ids are two hops from a top-level listing: legal entities
    carry payroll cycles, and the shared fan-out helper binds a single level of path params.
    Resume is per cycle rather than per page, so an interrupted sync re-walks the cheap
    listings and skips straight to the reports it had not reached.
    """
    root_config = DEEL_ENDPOINTS[GROSS_TO_NET_ROOT]
    cycle_config = DEEL_ENDPOINTS[GROSS_TO_NET_PARENT]

    resume = resumable_source_manager.load_state() if resumable_source_manager.can_resume() else None
    # Keyed by legal entity and cycle together, because a cycle id is only unique within its
    # entity — the same reason payroll_cycles takes a composite primary key.
    completed: list[str] = list(resume.completed) if resume is not None and resume.completed else []
    done = set(completed)

    session = make_tracked_session(redact_values=(api_token,), capture=False)
    base_url = _base_url(api_version)

    def headers(config: DeelEndpointConfig) -> dict[str, str]:
        return {
            "Authorization": f"Bearer {api_token}",
            "Accept": "application/json",
            **_version_headers(api_version, config),
        }

    for entity_page in _iter_cursor_pages(session, f"{base_url}{root_config.path}", root_config, headers(root_config)):
        for entity in entity_page:
            legal_entity_id = entity.get("id")
            if legal_entity_id is None:
                continue
            cycles_url = f"{base_url}{cycle_config.path.replace('{legal_entity_id}', str(legal_entity_id))}"
            for cycle_page in _iter_cursor_pages(
                session, cycles_url, cycle_config, headers(cycle_config), ignore_statuses=(404,)
            ):
                for cycle in cycle_page:
                    cycle_id = cycle.get("id")
                    if cycle_id is None:
                        continue
                    cycle_key = f"{legal_entity_id}:{cycle_id}"
                    # Deel only publishes a report for a cycle it flags; the rest 404.
                    if not cycle.get("has_g2n_report") or cycle_key in done:
                        continue
                    report_url = f"{base_url}{config.path.replace('{cycle_id}', str(cycle_id))}"
                    for rows in _iter_cursor_pages(
                        session, report_url, config, headers(config), ignore_statuses=(404,)
                    ):
                        yield [
                            {**row, "cycle_id": str(cycle_id), "legal_entity_id": str(legal_entity_id)} for row in rows
                        ]
                    completed.append(cycle_key)
                    done.add(cycle_key)
                    resumable_source_manager.save_state(DeelResumeConfig(completed=list(completed)))


def deel_source(
    api_token: str,
    endpoint: str,
    team_id: int,
    job_id: str,
    resumable_source_manager: ResumableSourceManager[DeelResumeConfig],
    api_version: str,
    db_incremental_field_last_value: Optional[Any] = None,
) -> SourceResponse:
    config = DEEL_ENDPOINTS[endpoint]

    items: Any
    column_hints: Optional[dict[str, Any]] = None
    if endpoint == TIME_OFF_EVENTS_ENDPOINT:
        items = partial(
            _time_off_event_items,
            config,
            api_token,
            team_id,
            job_id,
            resumable_source_manager,
            api_version,
        )
    elif endpoint == GROSS_TO_NET_ENDPOINT:
        items = partial(_gross_to_net_items, config, api_token, resumable_source_manager, api_version)
    else:
        builder = _fanout_items if config.fanout is not None else _top_level_items
        resource = builder(
            config,
            api_token,
            team_id,
            job_id,
            resumable_source_manager,
            db_incremental_field_last_value,
            api_version,
        )
        items = lambda: resource  # noqa: E731
        column_hints = getattr(resource, "column_hints", None)

    return SourceResponse(
        name=endpoint,
        items=items,
        primary_keys=config.primary_keys,
        partition_count=1,
        partition_size=1,
        partition_mode="datetime" if config.partition_key else None,
        partition_format="month" if config.partition_key else None,
        partition_keys=[config.partition_key] if config.partition_key else None,
        sort_mode="asc",
        column_hints=column_hints,
    )