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.7 KB
Raw Blame
import dataclasses
from collections.abc import Iterator
from typing import Any, Optional

from products.warehouse_sources.sources.loops.settings import LOOPS_ENDPOINTS, PAGE_SIZE, LoopsEndpointConfig
from products.warehouse_sources.sources.sdk import (
    BearerTokenAuth,
    ClientConfig,
    DependentEndpointConfig,
    Endpoint,
    EndpointResource,
    JSONResponseCursorPaginator,
    ResponseAction,
    RESTAPIConfig,
    RESTClient,
    ResumableSourceManager,
    SinglePagePaginator,
    SourceResponse,
    build_dependent_resource,
    create_response_hooks,
    make_tracked_session,
    rest_api_resource,
)

BASE_URL = "https://app.loops.so/api"

SEND_EMAIL_NODE_TYPE = "SendEmailAction"

# Loops answers 400 for a node with no email message, and 404 for a workflow or node deleted after
# the parent listing. Neither has metrics.
_WORKFLOW_NODE_RESPONSE_ACTIONS: list[ResponseAction] = [
    {"status_code": 400, "action": "ignore"},
    {"status_code": 404, "action": "ignore"},
]


@dataclasses.dataclass(frozen=False)
class LoopsResumeConfig:
    cursor: str


def get_resource(endpoint: str) -> EndpointResource:
    config = LOOPS_ENDPOINTS[endpoint]

    params: dict[str, Any] = dict(config.extra_params)

    endpoint_config: Endpoint = {
        "path": config.path,
        "params": params,
    }

    if config.paginated:
        endpoint_config["data_selector"] = "data"
        params["perPage"] = PAGE_SIZE
        endpoint_config["paginator"] = JSONResponseCursorPaginator(
            cursor_path="pagination.nextCursor",
            cursor_param="cursor",
        )
    else:
        # Unpaginated endpoints (mailing lists, contact properties) return the
        # full collection as a bare JSON array with no wrapper object.
        endpoint_config["paginator"] = SinglePagePaginator()

    return {
        "name": config.name,
        "table_name": config.name,
        "write_disposition": "replace",
        "endpoint": endpoint_config,
        "table_format": "delta",
    }


def _client_config(api_key: str) -> ClientConfig:
    return {
        "base_url": BASE_URL,
        "auth": {
            "type": "bearer",
            "token": api_key,
        },
        "headers": {
            "Accept": "application/json",
        },
        "request_timeout": (10, 60),
    }


def _fanout_source_response(
    api_key: str,
    endpoint_config: LoopsEndpointConfig,
    fanout: DependentEndpointConfig,
    team_id: int,
    job_id: str,
) -> SourceResponse:
    # One request per parent row with no cursor of its own, so the fan-out runs as a plain
    # full refresh without resume state.
    resource = build_dependent_resource(
        endpoint_configs=LOOPS_ENDPOINTS,
        child_endpoint=endpoint_config.name,
        fanout=fanout,
        client_config=_client_config(api_key),
        path_format_values={},
        team_id=team_id,
        job_id=job_id,
        db_incremental_field_last_value=None,
        # Only the parent list takes perPage; it rides in the fan-out's parent_params.
        page_size_param=None,
        parent_endpoint_extra={
            "data_selector": "data",
            "paginator": JSONResponseCursorPaginator(cursor_path="pagination.nextCursor", cursor_param="cursor"),
        },
        child_endpoint_extra={"data_selector": "$", "paginator": SinglePagePaginator()},
    )

    return SourceResponse(
        name=endpoint_config.name,
        items=lambda: resource,
        primary_keys=list(endpoint_config.primary_keys),
        partition_count=1,
        partition_size=1,
    )


def _iter_workflow_node_metrics(
    client: RESTClient,
    endpoint_config: LoopsEndpointConfig,
    resumable_source_manager: ResumableSourceManager[LoopsResumeConfig],
) -> Iterator[list[dict[str, Any]]]:
    hooks = create_response_hooks(_WORKFLOW_NODE_RESPONSE_ACTIONS, resource_name=endpoint_config.name)

    for workflows in client.paginate(
        path=LOOPS_ENDPOINTS["workflows"].path,
        params={"perPage": PAGE_SIZE},
        paginator=JSONResponseCursorPaginator(cursor_path="pagination.nextCursor", cursor_param="cursor"),
        data_selector="data",
    ):
        for summary in workflows:
            workflow_id = summary["id"]
            for details in client.paginate(
                path=f"/v1/workflows/{workflow_id}", paginator=SinglePagePaginator(), data_selector="$", hooks=hooks
            ):
                for workflow in details:
                    for node_id, node in (workflow.get("nodes") or {}).items():
                        if node.get("typeName") != SEND_EMAIL_NODE_TYPE:
                            continue
                        for metrics in client.paginate(
                            path=endpoint_config.path.format(workflowId=workflow_id, nodeId=node_id),
                            paginator=SinglePagePaginator(),
                            data_selector="$",
                            hooks=hooks,
                        ):
                            yield [
                                {
                                    **row,
                                    "workflowId": workflow_id,
                                    "nodeId": node_id,
                                    "emailMessageId": node.get("emailMessageId"),
                                    "subject": node.get("subject"),
                                }
                                for row in metrics
                            ]
                        # Many nodes return no metrics, so a long run of requests can pass without a yield.
                        resumable_source_manager.safe_point()
            resumable_source_manager.safe_point()


def loops_source(
    api_key: str,
    endpoint: str,
    team_id: int,
    job_id: str,
    resumable_source_manager: ResumableSourceManager[LoopsResumeConfig],
) -> SourceResponse:
    endpoint_config = LOOPS_ENDPOINTS[endpoint]

    if endpoint_config.fanout is not None:
        return _fanout_source_response(api_key, endpoint_config, endpoint_config.fanout, team_id, job_id)

    if endpoint_config.per_workflow_node:
        client = RESTClient(
            base_url=BASE_URL,
            headers={"Accept": "application/json"},
            auth=BearerTokenAuth(token=api_key),
            request_timeout=(10, 60),
        )
        return SourceResponse(
            name=endpoint_config.name,
            items=lambda: _iter_workflow_node_metrics(client, endpoint_config, resumable_source_manager),
            primary_keys=list(endpoint_config.primary_keys),
            partition_count=1,
            partition_size=1,
        )

    rest_config: RESTAPIConfig = {
        "client": _client_config(api_key),
        "resource_defaults": {
            "write_disposition": "replace",
        },
        "resources": [get_resource(endpoint)],
    }

    initial_paginator_state: Optional[dict[str, Any]] = None
    if resumable_source_manager.can_resume():
        resume_config = resumable_source_manager.load_state()
        if resume_config is not None:
            initial_paginator_state = {"cursor": resume_config.cursor}

    def save_checkpoint(state: Optional[dict[str, Any]]) -> None:
        # Persist only when there's a next page to resume to; the Redis TTL handles
        # cleanup once the sync finishes. Saving happens after each page is yielded,
        # so a crash re-fetches the last page rather than skipping it.
        if state and state.get("cursor"):
            resumable_source_manager.save_state(LoopsResumeConfig(cursor=str(state["cursor"])))

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

    return SourceResponse(
        name=endpoint,
        items=lambda: resource,
        primary_keys=list(endpoint_config.primary_keys),
        partition_count=1,
        partition_size=1,
        partition_mode="datetime" if endpoint_config.partition_key else None,
        partition_format="week" if endpoint_config.partition_key else None,
        partition_keys=[endpoint_config.partition_key] if endpoint_config.partition_key else None,
    )


def validate_credentials(api_key: str) -> tuple[bool, str | None]:
    try:
        response = make_tracked_session(redact_values=(api_key,)).get(
            f"{BASE_URL}/v1/api-key",
            headers={
                "Authorization": f"Bearer {api_key}",
                "Accept": "application/json",
            },
            timeout=10,
        )
    except Exception as e:
        return False, str(e)

    if response.status_code == 200:
        return True, None
    if response.status_code == 401:
        return False, "Invalid Loops API key"
    return False, f"Loops returned an unexpected status code: {response.status_code}"