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.

7.0 KB
Raw Blame
from dataclasses import dataclass, field
from typing import Optional

from products.warehouse_sources.sources.sdk import (
    DependentEndpointConfig,
    IncrementalField,
    IncrementalFieldType,
    SortMode,
)

# Scopes requested when validating a new connection. Scopes are fixed on the
# developer app at creation, so the caption tells users to grant these.
TOKEN_SCOPES = "transactions:read reimbursements:read users:read cards:read departments:read"
# Ramp list pages cap at 100 items.
PAGE_SIZE = 100


@dataclass(frozen=True)
class RampEndpointConfig:
    name: str
    # Path under {host}/developer/v1.
    path: str
    # The token for a sync requests only this scope: Ramp rejects a mint that asks for a
    # scope the app lacks, so an app without a newer scope still syncs its other tables.
    scope: str
    primary_keys: list[str] = field(default_factory=lambda: ["id"])
    incremental_fields: list[IncrementalField] = field(default_factory=list)
    # Query param that pushes the incremental cursor server-side.
    incremental_param: Optional[str] = None
    # Stable event-time field used for datetime partitioning.
    partition_key: Optional[str] = None
    fanout: Optional[DependentEndpointConfig] = None
    page_size: int = PAGE_SIZE
    default_incremental_field: Optional[str] = None
    sort_mode: Optional[SortMode] = "asc"
    default_incremental_lookback_seconds: Optional[int] = None


_VENDORS_FANOUT = DependentEndpointConfig(
    parent_name="vendors",
    resolve_param="vendor_id",
    resolve_field="id",
    include_from_parent=["id"],
    parent_field_renames={"id": "vendor_id"},
)


RAMP_ENDPOINTS: dict[str, RampEndpointConfig] = {
    "transactions": RampEndpointConfig(
        name="transactions",
        path="/transactions",
        scope="transactions:read",
        partition_key="user_transaction_time",
        # from_date filters on the transaction time. There is no updated_after,
        # so late state changes (pending→cleared) need an occasional full
        # refresh — the same trade-off Fivetran makes.
        incremental_param="from_date",
        # Result ordering within a from_date window is not documented, so the
        # pipeline defers incremental watermark commits until a run completes.
        sort_mode="desc",
        incremental_fields=[
            {
                "label": "user_transaction_time",
                "type": IncrementalFieldType.DateTime,
                "field": "user_transaction_time",
                "field_type": IncrementalFieldType.DateTime,
            },
        ],
    ),
    "reimbursements": RampEndpointConfig(
        name="reimbursements",
        path="/reimbursements",
        scope="reimbursements:read",
    ),
    "users": RampEndpointConfig(
        name="users",
        path="/users",
        scope="users:read",
    ),
    "cards": RampEndpointConfig(
        name="cards",
        path="/cards",
        scope="cards:read",
    ),
    "departments": RampEndpointConfig(
        name="departments",
        path="/departments",
        scope="departments:read",
    ),
    # Bills and vendors change after creation (payment status, approvals, details), but the
    # bills endpoints only filter on created_at and vendors.updated_at is nullable, so these
    # lookups stay full refresh rather than risk silently missing changed or new rows.
    "vendors": RampEndpointConfig(
        name="vendors",
        path="/vendors",
        scope="vendors:read",
        partition_key="created_at",
    ),
    "vendor_contacts": RampEndpointConfig(
        name="vendor_contacts",
        path="/vendors/{vendor_id}/contacts",
        scope="vendors:read",
        primary_keys=["vendor_id", "id"],
        fanout=_VENDORS_FANOUT,
    ),
    "merchants": RampEndpointConfig(
        name="merchants",
        path="/merchants",
        scope="merchants:read",
    ),
    "bills": RampEndpointConfig(
        name="bills",
        path="/bills",
        scope="bills:read",
        partition_key="created_at",
    ),
    "bill_drafts": RampEndpointConfig(
        name="bill_drafts",
        path="/bills/drafts",
        scope="bills:read",
        partition_key="created_at",
    ),
    "accounting_accounts": RampEndpointConfig(
        name="accounting_accounts",
        path="/accounting/accounts",
        scope="accounting:read",
        # `id` is the ERP's remote ID and can be absent; `ramp_id` is Ramp's own unique ID.
        primary_keys=["ramp_id"],
        partition_key="created_at",
    ),
    "billing_statements": RampEndpointConfig(
        name="billing_statements",
        path="/billing/statements",
        scope="billing_statements:read",
    ),
    "locations": RampEndpointConfig(
        name="locations",
        path="/locations",
        scope="locations:read",
    ),
    "entities": RampEndpointConfig(
        name="entities",
        path="/entities",
        scope="entities:read",
    ),
    # Ramp does not document the list order of transfers or statements, so their incremental
    # reads promise no sort order.
    "transfers": RampEndpointConfig(
        name="transfers",
        path="/transfers",
        scope="transfers:read",
        partition_key="created_at",
        # from_date filters on when the transfer occurred, but the status keeps changing while an
        # ACH transfer settles, so incremental runs re-read the trailing week.
        incremental_param="from_date",
        sort_mode=None,
        default_incremental_lookback_seconds=7 * 24 * 60 * 60,
        incremental_fields=[
            {
                "label": "created_at",
                "type": IncrementalFieldType.DateTime,
                "field": "created_at",
                "field_type": IncrementalFieldType.DateTime,
            },
        ],
    ),
    "statements": RampEndpointConfig(
        name="statements",
        path="/statements",
        scope="statements:read",
        partition_key="end_date",
        # from_date filters on end_date (inclusive).
        incremental_param="from_date",
        sort_mode=None,
        incremental_fields=[
            {
                "label": "end_date",
                "type": IncrementalFieldType.DateTime,
                "field": "end_date",
                "field_type": IncrementalFieldType.DateTime,
            },
        ],
        # Each statement embeds every line of its period, so a full page can be very large.
        page_size=10,
    ),
}

ENDPOINTS = tuple(RAMP_ENDPOINTS.keys())

INCREMENTAL_FIELDS: dict[str, list[IncrementalField]] = {
    name: config.incremental_fields for name, config in RAMP_ENDPOINTS.items() if config.incremental_fields
}

DEFAULT_INCREMENTAL_LOOKBACK_SECONDS: dict[str, int] = {
    name: config.default_incremental_lookback_seconds
    for name, config in RAMP_ENDPOINTS.items()
    if config.default_incremental_lookback_seconds is not None
}

# Credential validation only proves TOKEN_SCOPES, so tables needing any other scope start
# unselected rather than failing every sync on apps that were never granted it.
SHOULD_SYNC_DEFAULT: dict[str, bool] = {
    name: False for name, config in RAMP_ENDPOINTS.items() if config.scope not in TOKEN_SCOPES.split()
}