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.

4.1 KB
Raw Blame
from dataclasses import dataclass

from products.warehouse_sources.sources.sdk import IncrementalField, IncrementalFieldType

# Recurly's API key authenticates to a single site; the host selects the data residency region.
RECURLY_BASE_URLS: dict[str, str] = {
    "us": "https://v3.recurly.com",
    "eu": "https://v3.eu.recurly.com",
}

# Every Recurly resource we sync carries a stable `created_at` we can partition on.
RECURLY_PARTITION_KEY = "created_at"


@dataclass(frozen=True)
class RecurlyEndpoint:
    name: str
    path: str
    # `supports_incremental` is only `True` where the list endpoint exposes the
    # server-side `begin_time` filter (confirmed against the v2021-02-25 OpenAPI spec).
    # Endpoints that only accept `sort` (no time filter) stay full-refresh, since an
    # "incremental" sync would still read every page.
    supports_incremental: bool
    # `gift_cards` is the one list endpoint the v2021-02-25 OpenAPI spec gives zero query
    # parameters at all — no `limit`, `sort`, `order`, or `begin_time`. Recurly rejects the
    # request with a 400 validation error if any are sent, so it can only ever be listed bare.
    supports_list_params: bool = True
    # Set for endpoints that only exist per parent row (e.g. `/accounts/{account_id}/...`).
    # They fan out over the parent listing and are always full refresh: rows arrive grouped
    # by parent, not in time order, so an ascending watermark would skip rows.
    parent: str | None = None
    primary_keys: tuple[str, ...] = ("id",)


RECURLY_ENDPOINTS: dict[str, RecurlyEndpoint] = {
    "accounts": RecurlyEndpoint("accounts", "/accounts", True),
    "acquisitions": RecurlyEndpoint("acquisitions", "/acquisitions", True),
    "add_ons": RecurlyEndpoint("add_ons", "/add_ons", True),
    "business_entities": RecurlyEndpoint("business_entities", "/business_entities", False, supports_list_params=False),
    "coupon_redemptions": RecurlyEndpoint(
        "coupon_redemptions",
        "/accounts/{account_id}/coupon_redemptions",
        False,
        parent="accounts",
        primary_keys=("account_id", "id"),
    ),
    "coupons": RecurlyEndpoint("coupons", "/coupons", True),
    "credit_payments": RecurlyEndpoint("credit_payments", "/credit_payments", True),
    "dunning_campaigns": RecurlyEndpoint("dunning_campaigns", "/dunning_campaigns", False),
    "external_invoices": RecurlyEndpoint("external_invoices", "/external_invoices", False),
    "external_subscriptions": RecurlyEndpoint("external_subscriptions", "/external_subscriptions", False),
    "gift_cards": RecurlyEndpoint("gift_cards", "/gift_cards", False, supports_list_params=False),
    "invoices": RecurlyEndpoint("invoices", "/invoices", True),
    "items": RecurlyEndpoint("items", "/items", True),
    "line_items": RecurlyEndpoint("line_items", "/line_items", True),
    "measured_units": RecurlyEndpoint("measured_units", "/measured_units", True),
    "plans": RecurlyEndpoint("plans", "/plans", True),
    "shipping_methods": RecurlyEndpoint("shipping_methods", "/shipping_methods", True),
    "subscriptions": RecurlyEndpoint("subscriptions", "/subscriptions", True),
    "transactions": RecurlyEndpoint("transactions", "/transactions", True),
    # Usage ids are unique site-wide (Recurly serves them at `/usage/{usage_id}`).
    "usage": RecurlyEndpoint(
        "usage",
        "/subscriptions/{subscription_id}/add_ons/{add_on_id}/usage",
        False,
        parent="subscriptions",
    ),
}

ENDPOINTS = tuple(RECURLY_ENDPOINTS.keys())


def _incremental_fields() -> list[IncrementalField]:
    return [
        {
            "label": "updated_at",
            "type": IncrementalFieldType.DateTime,
            "field": "updated_at",
            "field_type": IncrementalFieldType.DateTime,
        },
        {
            "label": "created_at",
            "type": IncrementalFieldType.DateTime,
            "field": "created_at",
            "field_type": IncrementalFieldType.DateTime,
        },
    ]


INCREMENTAL_FIELDS: dict[str, list[IncrementalField]] = {
    name: (_incremental_fields() if endpoint.supports_incremental else [])
    for name, endpoint in RECURLY_ENDPOINTS.items()
}