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.4 KB
Raw Blame
from dataclasses import field
from typing import Optional

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

# Bloomerang's v2 list endpoints cap `take` at 50 (confirmed via the Parsons client library, which
# clamps page_size to this maximum).
PAGE_SIZE = 50


def _last_modified_incremental_fields() -> list[IncrementalField]:
    # Only endpoints whose rows carry AuditTrail.LastModifiedDate *and* whose list action accepts a
    # server-side `lastModified` filter get this. See BloomerangEndpointConfig.supports_incremental.
    return [
        {
            "label": "LastModifiedDate",
            "type": IncrementalFieldType.DateTime,
            "field": "LastModifiedDate",
            "field_type": IncrementalFieldType.DateTime,
        },
    ]


@frozen
class BloomerangFanoutConfig:
    """A child table read once for each row of a parent list endpoint."""

    parent_path: str
    parent_params: dict[str, str]
    # Column that carries the parent id on each child row.
    parent_id_column: str
    # When set, the child is read once for each embedded designation of this type, not once for each
    # parent row. Pledges are designations inside transactions and have no list endpoint of their own.
    designation_type: Optional[str] = None


@frozen
class BloomerangEndpointConfig:
    name: str
    path: str  # path under https://api.bloomerang.co/v2
    # Whether the list action accepts a server-side `lastModified` filter *and* `orderBy=LastModifiedDate`,
    # so incremental pages arrive in ascending cursor order. Households, Addresses, and Emails accept
    # the filter but not `orderBy`, so they stay full refresh.
    supports_incremental: bool
    # Whether the list action accepts `orderBy`/`orderDirection` at all (Appeals/Campaigns/Funds
    # accept neither, per the API's own parameter docs).
    supports_sort: bool
    # Whether rows carry a nested `AuditTrail` object ({CreatedDate, LastModifiedDate, ...}) that
    # gets flattened onto the row root. Appeals/Campaigns/Funds have no AuditTrail field.
    has_audit_trail: bool
    # Column for the stable full-refresh sort. The `orderBy` enum varies per endpoint: Refunds,
    # SoftCredits, Notes, and Tasks don't accept `Id`.
    sort_field: str = "Id"
    extra_params: dict[str, str] = field(default_factory=dict)
    incremental_fields: list[IncrementalField] = field(default_factory=list)
    primary_keys: list[str] = field(default_factory=lambda: ["Id"])
    # Stable creation-time column to partition by. None when the endpoint has no AuditTrail.
    partition_key: Optional[str] = "CreatedDate"
    fanout: Optional[BloomerangFanoutConfig] = None


BLOOMERANG_ENDPOINTS: dict[str, BloomerangEndpointConfig] = {
    "Constituents": BloomerangEndpointConfig(
        name="Constituents",
        path="constituents",
        supports_incremental=True,
        supports_sort=True,
        has_audit_trail=True,
        incremental_fields=_last_modified_incremental_fields(),
    ),
    "Transactions": BloomerangEndpointConfig(
        name="Transactions",
        path="transactions",
        supports_incremental=False,
        supports_sort=True,
        has_audit_trail=True,
    ),
    "Interactions": BloomerangEndpointConfig(
        name="Interactions",
        path="interactions",
        supports_incremental=False,
        supports_sort=True,
        has_audit_trail=True,
    ),
    "Appeals": BloomerangEndpointConfig(
        name="Appeals",
        path="appeals",
        supports_incremental=False,
        supports_sort=False,
        has_audit_trail=False,
        partition_key=None,
    ),
    "Campaigns": BloomerangEndpointConfig(
        name="Campaigns",
        path="campaigns",
        supports_incremental=False,
        supports_sort=False,
        has_audit_trail=False,
        partition_key=None,
    ),
    "Funds": BloomerangEndpointConfig(
        name="Funds",
        path="funds",
        supports_incremental=False,
        supports_sort=False,
        has_audit_trail=False,
        partition_key=None,
    ),
    "Refunds": BloomerangEndpointConfig(
        name="Refunds",
        path="refunds",
        supports_incremental=True,
        supports_sort=True,
        has_audit_trail=True,
        sort_field="CreatedDate",
        incremental_fields=_last_modified_incremental_fields(),
    ),
    "SoftCredits": BloomerangEndpointConfig(
        name="SoftCredits",
        path="softcredits",
        supports_incremental=True,
        supports_sort=True,
        has_audit_trail=True,
        sort_field="CreatedDate",
        incremental_fields=_last_modified_incremental_fields(),
    ),
    "Households": BloomerangEndpointConfig(
        name="Households",
        path="households",
        supports_incremental=False,
        supports_sort=False,
        has_audit_trail=True,
    ),
    "Phones": BloomerangEndpointConfig(
        name="Phones",
        path="phones",
        supports_incremental=False,
        supports_sort=False,
        has_audit_trail=False,
        partition_key=None,
    ),
    "Addresses": BloomerangEndpointConfig(
        name="Addresses",
        path="addresses",
        supports_incremental=False,
        supports_sort=False,
        has_audit_trail=True,
    ),
    "Emails": BloomerangEndpointConfig(
        name="Emails",
        path="emails",
        supports_incremental=False,
        supports_sort=False,
        has_audit_trail=True,
    ),
    "Notes": BloomerangEndpointConfig(
        name="Notes",
        path="notes",
        supports_incremental=False,
        supports_sort=True,
        has_audit_trail=True,
        sort_field="CreatedDate",
    ),
    "Tasks": BloomerangEndpointConfig(
        name="Tasks",
        path="tasks",
        supports_incremental=False,
        supports_sort=True,
        has_audit_trail=True,
        sort_field="CreatedDate",
        # The API doesn't document which statuses an unfiltered request returns, so ask for all of them.
        extra_params={"status": "Active|Complete|Archived"},
    ),
    "Relationships": BloomerangEndpointConfig(
        name="Relationships",
        path="constituent/{id}/relationships",
        # Rows carry no AuditTrail, so the `lastModified` filter has no cursor column to advance.
        supports_incremental=False,
        supports_sort=False,
        has_audit_trail=False,
        # A relationship links two constituents and is listed under each of them.
        primary_keys=["ConstituentId", "Id"],
        partition_key=None,
        fanout=BloomerangFanoutConfig(
            parent_path="constituents",
            parent_params={"orderBy": "Id", "orderDirection": "Asc"},
            parent_id_column="ConstituentId",
        ),
    ),
    "PledgePaymentFailures": BloomerangEndpointConfig(
        name="PledgePaymentFailures",
        path="pledge/{id}/paymentFailures",
        supports_incremental=False,
        supports_sort=False,
        has_audit_trail=False,
        primary_keys=["PledgeId", "Id"],
        partition_key="Date",
        fanout=BloomerangFanoutConfig(
            parent_path="transactions",
            # /transactions takes no `orderBy=Id`.
            parent_params={"type": "Pledge", "orderBy": "CreatedDate", "orderDirection": "Asc"},
            parent_id_column="PledgeId",
            designation_type="Pledge",
        ),
    ),
}

ENDPOINTS = tuple(BLOOMERANG_ENDPOINTS.keys())

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