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.

8.5 KB
Raw Blame
from datetime import UTC, date, datetime
from typing import Any, Optional
from urllib.parse import urlencode, urljoin, urlsplit

from requests import Response

from products.warehouse_sources.sources.rippling.settings import PAGE_SIZE, RIPPLING_ENDPOINTS, RipplingEndpointConfig
from products.warehouse_sources.sources.sdk import (
    BaseNextUrlPaginator,
    ClientConfig,
    RESTAPIConfig,
    ResumableSourceManager,
    SourceResponse,
    build_dependent_resource,
    frozen,
    make_tracked_session,
    rest_api_resource,
    validate_via_probe,
)

RIPPLING_BASE_URL = "https://rest.ripplingapis.com"
RIPPLING_HOST = urlsplit(RIPPLING_BASE_URL).netloc


@frozen
class RipplingResumeConfig:
    # Rippling cursor pagination returns a `next_link` URL (sometimes relative);
    # we persist it absolutized, so it's all we need to pick back up.
    next_url: str | None = None
    # Fan-out endpoints checkpoint the framework's per-parent state instead.
    fanout_state: dict[str, Any] | None = None


def _format_filter_timestamp(value: Any) -> str:
    """Format an incremental cursor for Rippling's OData-style filter (e.g. updated_at ge 2024-10-01T00:00:00)."""
    if isinstance(value, datetime):
        dt = value if value.tzinfo else value.replace(tzinfo=UTC)
        return dt.astimezone(UTC).strftime("%Y-%m-%dT%H:%M:%S")
    if isinstance(value, date):
        return value.strftime("%Y-%m-%dT00:00:00")
    return str(value)


def _build_params(
    config: RipplingEndpointConfig,
    should_use_incremental_field: bool,
    db_incremental_field_last_value: Any,
    incremental_field: str | None,
) -> dict[str, Any]:
    params: dict[str, Any] = {"limit": PAGE_SIZE}

    if should_use_incremental_field and config.incremental_fields and db_incremental_field_last_value is not None:
        cursor_field = incremental_field or config.default_incremental_field
        params["filter"] = f"{cursor_field} ge {_format_filter_timestamp(db_incremental_field_last_value)}"
        if config.order_by:
            # Ascending order on the cursor field so the incremental watermark
            # advances monotonically as pages are consumed (order_by defaults to asc).
            params["order_by"] = cursor_field
    elif config.order_by:
        # Full refresh: a stable creation-time sort prevents page-boundary
        # skips/duplicates if rows change mid-sync.
        params["order_by"] = config.order_by

    return params


def _absolutize_next_url(next_link: str) -> str:
    """Absolutize a pagination link against the Rippling host, rejecting off-domain targets.

    The session carries the user's bearer token in its default headers, so a malicious or
    buggy `next_link` pointing at another host could leak that token. Only https URLs on the
    Rippling API host (or relative paths resolving to it) are allowed."""
    next_url = urljoin(RIPPLING_BASE_URL, next_link)
    parts = urlsplit(next_url)
    if parts.scheme != "https" or parts.netloc != RIPPLING_HOST:
        raise ValueError(f"Rippling pagination link points off-domain: {next_link}")
    return next_url


def _build_url(path: str, params: dict[str, Any]) -> str:
    if not params:
        return f"{RIPPLING_BASE_URL}{path}"
    return f"{RIPPLING_BASE_URL}{path}?{urlencode(params)}"


class RipplingNextLinkPaginator(BaseNextUrlPaginator):
    """Follow Rippling's body-level `next_link` cursor.

    The link may be relative, so absolutize it against the API host (rejecting any off-domain
    or non-https target before the token-bearing request is sent). Resume is inherited from
    ``BaseNextUrlPaginator`` (``{"next_url": ...}``)."""

    def update_state(self, response: Response, data: Optional[list[Any]] = None) -> None:
        try:
            next_link = response.json().get("next_link")
        except Exception:
            next_link = None
        if next_link:
            self._next_url = _absolutize_next_url(next_link)
            self._has_next_page = True
        else:
            self._has_next_page = False


def validate_credentials(api_token: str) -> bool:
    """Confirm the API token is valid with a cheap one-company listing probe.

    Scoped tokens may lack individual dataset scopes (403); only 401 means the
    token itself is bad, so 403 is accepted at source-create time."""
    _ok, status = validate_via_probe(
        lambda: make_tracked_session(redact_values=(api_token,)),
        _build_url("/companies", {"limit": 1}),
        headers={"Authorization": f"Bearer {api_token}"},
    )
    return status is not None and status != 401


def rippling_source(
    api_token: str,
    endpoint: str,
    team_id: int,
    job_id: str,
    resumable_source_manager: ResumableSourceManager[RipplingResumeConfig],
    should_use_incremental_field: bool = False,
    db_incremental_field_last_value: Optional[Any] = None,
    incremental_field: str | None = None,
) -> SourceResponse:
    config = RIPPLING_ENDPOINTS[endpoint]

    client_config: ClientConfig = {
        "base_url": RIPPLING_BASE_URL,
        # Auth (Bearer) via the framework config so the token is redacted from logs/errors.
        "auth": {"type": "bearer", "token": api_token},
        "paginator": RipplingNextLinkPaginator(),
        # Pin every request (fan-out child paths included) to the base host so a hostile
        # run id or link can't replay the bearer token elsewhere. `[]` means base_url's host only.
        "allowed_hosts": [],
    }

    resume = resumable_source_manager.load_state() if resumable_source_manager.can_resume() else None

    resource: Any
    if config.fanout is not None:
        initial_fanout_state = resume.fanout_state if resume is not None else None

        def save_fanout_checkpoint(state: Optional[dict[str, Any]]) -> None:
            if state:
                resumable_source_manager.save_state(RipplingResumeConfig(fanout_state=state))

        resource = build_dependent_resource(
            endpoint_configs=RIPPLING_ENDPOINTS,
            child_endpoint=endpoint,
            fanout=config.fanout,
            client_config=client_config,
            path_format_values={},
            team_id=team_id,
            job_id=job_id,
            db_incremental_field_last_value=None,
            # A missing `results` key is a legit empty page (not a fail-loud shape change).
            parent_endpoint_extra={"data_selector": "results"},
            child_endpoint_extra={"data_selector": "results"},
            resume_hook=save_fanout_checkpoint,
            initial_paginator_state=initial_fanout_state,
        )
    else:
        rest_config: RESTAPIConfig = {
            "client": client_config,
            "resource_defaults": {},
            "resources": [
                {
                    "name": endpoint,
                    "endpoint": {
                        "path": config.path,
                        "params": _build_params(
                            config, should_use_incremental_field, db_incremental_field_last_value, incremental_field
                        ),
                        # A missing `results` key is a legit empty page (not a fail-loud shape change).
                        "data_selector": "results",
                    },
                }
            ],
        }

        initial_paginator_state = (
            {"next_url": resume.next_url} if resume is not None and resume.next_url is not None else None
        )

        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 on primary key) rather than skipping it.
            if state and state.get("next_url"):
                resumable_source_manager.save_state(RipplingResumeConfig(next_url=state["next_url"]))

        resource = rest_api_resource(
            rest_config,
            team_id,
            job_id,
            None,
            resume_hook=save_checkpoint,
            initial_paginator_state=initial_paginator_state,
        )

    return SourceResponse(
        name=endpoint,
        items=lambda: resource,
        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,
        # Endpoints that accept no order_by return rows in an undocumented order.
        sort_mode="asc" if config.order_by else None,
        column_hints=resource.column_hints,
    )