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.

14.3 KB
Raw Blame
import base64
import dataclasses
from typing import Any, Optional

from requests import Request, Response

from products.warehouse_sources.sources.sdk import (
    BasePaginator,
    EndpointResource,
    RESTAPIConfig,
    ResumableSourceManager,
    make_tracked_session,
    rest_api_resource,
)


@dataclasses.dataclass(frozen=False)
class ChargebeeResumeConfig:
    next_offset: str


def incremental_param(cursor_path: str) -> dict[str, Any]:
    return {
        "type": "incremental",
        "cursor_path": cursor_path,
        "initial_value": 0,
    }


def get_resource(
    name: str, should_use_incremental_field: bool, db_incremental_field_last_value: Optional[Any] = None
) -> EndpointResource:
    # Chargebee only guarantees `updated_at`/`occurred_at` on records updated after
    # 2016-09-28 and omits it on some older, never-updated records (e.g. voided
    # authorization transactions). Attaching the cursor filter with its initial value
    # (0) on the first sync excludes those records server-side, and no later resync
    # recovers them because the filter is sent again. Only send the filter once a real
    # watermark exists from a previous sync; the watermark is computed from synced
    # rows, so the first unfiltered sync still advances it.
    apply_incremental_filter = should_use_incremental_field and db_incremental_field_last_value is not None
    resources: dict[str, EndpointResource] = {
        # Credit units and meters have no server-side timestamp filter, so they only support full refresh.
        "CreditUnits": {
            "name": "CreditUnits",
            "table_name": "credit_units",
            "write_disposition": "replace",
            "endpoint": {
                "data_selector": "list[*].credit_unit",
                "path": "/v2/credit_units",
                "params": {
                    "limit": 100,
                },
            },
            "table_format": "delta",
        },
        "Customers": {
            "name": "Customers",
            "table_name": "customers",
            "write_disposition": {
                "disposition": "merge",
                "strategy": "upsert",
            }
            if should_use_incremental_field
            else "replace",
            "endpoint": {
                "data_selector": "list[*].customer",
                "path": "/v2/customers",
                "params": {
                    # the parameters below can optionally be configured
                    "updated_at[after]": incremental_param("updated_at") if apply_incremental_filter else None,
                    "limit": 100,
                    # by default, API does not return deleted resources
                    "include_deleted": "true",
                },
            },
            "table_format": "delta",
        },
        # Disputes only filter on `created_at`, which would miss status changes on existing disputes.
        "Disputes": {
            "name": "Disputes",
            "table_name": "disputes",
            "write_disposition": "replace",
            "endpoint": {
                "data_selector": "list[*].dispute",
                "path": "/v2/disputes",
                "params": {
                    "limit": 100,
                },
            },
            "table_format": "delta",
        },
        "Einvoices": {
            "name": "Einvoices",
            "table_name": "einvoices",
            "write_disposition": {
                "disposition": "merge",
                "strategy": "upsert",
            }
            if should_use_incremental_field
            else "replace",
            "endpoint": {
                "data_selector": "list[*].einvoice",
                "path": "/v2/einvoices",
                "params": {
                    "updated_at[after]": incremental_param("updated_at") if apply_incremental_filter else None,
                    "sort_by[asc]": "updated_at",
                    "limit": 100,
                },
            },
            "table_format": "delta",
        },
        # Note: it is possible to filter by event type, but for now we're
        # fetching all events
        "Events": {
            "name": "Events",
            "table_name": "events",
            "write_disposition": {
                "disposition": "merge",
                "strategy": "upsert",
            }
            if should_use_incremental_field
            else "replace",
            "endpoint": {
                "data_selector": "list[*].event",
                "path": "/v2/events",
                "params": {
                    # the parameters below can optionally be configured
                    "occurred_at[after]": incremental_param("occurred_at") if apply_incremental_filter else None,
                    "limit": 100,
                },
            },
            "table_format": "delta",
        },
        "Invoices": {
            "name": "Invoices",
            "table_name": "invoices",
            "write_disposition": {
                "disposition": "merge",
                "strategy": "upsert",
            }
            if should_use_incremental_field
            else "replace",
            "endpoint": {
                "data_selector": "list[*].invoice",
                "path": "/v2/invoices",
                "params": {
                    # the parameters below can optionally be configured
                    "updated_at[after]": incremental_param("updated_at") if apply_incremental_filter else None,
                    "limit": 100,
                    # by default, API does not return deleted resources
                    "include_deleted": "true",
                },
            },
            "table_format": "delta",
        },
        # Items and item prices have no `include_deleted` param; the list endpoints return
        # deleted records with `deleted: true` and `status: "deleted"`.
        "ItemPrices": {
            "name": "ItemPrices",
            "table_name": "item_prices",
            "write_disposition": {
                "disposition": "merge",
                "strategy": "upsert",
            }
            if should_use_incremental_field
            else "replace",
            "endpoint": {
                "data_selector": "list[*].item_price",
                "path": "/v2/item_prices",
                "params": {
                    "updated_at[after]": incremental_param("updated_at") if apply_incremental_filter else None,
                    "limit": 100,
                },
            },
            "table_format": "delta",
        },
        "Items": {
            "name": "Items",
            "table_name": "items",
            "write_disposition": {
                "disposition": "merge",
                "strategy": "upsert",
            }
            if should_use_incremental_field
            else "replace",
            "endpoint": {
                "data_selector": "list[*].item",
                "path": "/v2/items",
                "params": {
                    "updated_at[after]": incremental_param("updated_at") if apply_incremental_filter else None,
                    "limit": 100,
                },
            },
            "table_format": "delta",
        },
        "Meters": {
            "name": "Meters",
            "table_name": "meters",
            "write_disposition": "replace",
            "endpoint": {
                "data_selector": "list[*].meter",
                "path": "/v2/meters",
                "params": {
                    "limit": 100,
                },
            },
            "table_format": "delta",
        },
        "Orders": {
            "name": "Orders",
            "table_name": "orders",
            "write_disposition": {
                "disposition": "merge",
                "strategy": "upsert",
            }
            if should_use_incremental_field
            else "replace",
            "endpoint": {
                "data_selector": "list[*].order",
                "path": "/v2/orders",
                "params": {
                    # the parameters below can optionally be configured
                    "updated_at[after]": incremental_param("updated_at") if apply_incremental_filter else None,
                    "limit": 100,
                    # by default, API does not return deleted resources
                    "include_deleted": "true",
                },
            },
            "table_format": "delta",
        },
        "PaymentSchedules": {
            "name": "PaymentSchedules",
            "table_name": "payment_schedules",
            "write_disposition": {
                "disposition": "merge",
                "strategy": "upsert",
            }
            if should_use_incremental_field
            else "replace",
            "endpoint": {
                "data_selector": "list[*].payment_schedule",
                "path": "/v2/payment_schedules",
                "params": {
                    "updated_at[after]": incremental_param("updated_at") if apply_incremental_filter else None,
                    "limit": 100,
                },
            },
            "table_format": "delta",
        },
        "Subscriptions": {
            "name": "Subscriptions",
            "table_name": "subscriptions",
            "write_disposition": {
                "disposition": "merge",
                "strategy": "upsert",
            }
            if should_use_incremental_field
            else "replace",
            "endpoint": {
                "data_selector": "list[*].subscription",
                "path": "/v2/subscriptions",
                "params": {
                    # the parameters below can optionally be configured
                    "updated_at[after]": incremental_param("updated_at") if apply_incremental_filter else None,
                    "limit": 100,
                    # by default, API does not return deleted resources
                    "include_deleted": "true",
                },
            },
            "table_format": "delta",
        },
        "Transactions": {
            "name": "Transactions",
            "table_name": "transactions",
            "write_disposition": {
                "disposition": "merge",
                "strategy": "upsert",
            }
            if should_use_incremental_field
            else "replace",
            "endpoint": {
                "data_selector": "list[*].transaction",
                "path": "/v2/transactions",
                "params": {
                    # the parameters below can optionally be configured
                    "updated_at[after]": incremental_param("updated_at") if apply_incremental_filter else None,
                    "limit": 100,
                    # by default, API does not return deleted resources
                    "include_deleted": "true",
                },
            },
            "table_format": "delta",
        },
    }
    return resources[name]


class ChargebeePaginator(BasePaginator):
    def __init__(self) -> None:
        super().__init__()
        self._next_offset: Optional[str] = None

    def init_request(self, request: Request) -> None:
        # Honour a seeded resume offset on the first request.
        if self._next_offset is not None:
            if request.params is None:
                request.params = {}
            request.params["offset"] = self._next_offset

    def update_state(self, response: Response, data: Optional[list[Any]] = None) -> None:
        res = response.json()

        self._next_offset = None

        if not res:
            self._has_next_page = False
            return

        if "next_offset" in res:
            self._has_next_page = True
            self._next_offset = res["next_offset"]
        else:
            self._has_next_page = False

    def update_request(self, request: Request) -> None:
        if request.params is None:
            request.params = {}

        request.params["offset"] = self._next_offset

    def get_resume_state(self) -> Optional[dict[str, Any]]:
        if self._next_offset is not None and self._has_next_page:
            return {"next_offset": self._next_offset}
        return None

    def set_resume_state(self, state: dict[str, Any]) -> None:
        next_offset = state.get("next_offset")
        if next_offset is not None:
            self._next_offset = str(next_offset)
            self._has_next_page = True


def chargebee_source(
    api_key: str,
    site_name: str,
    endpoint: str,
    team_id: int,
    job_id: str,
    resumable_source_manager: ResumableSourceManager[ChargebeeResumeConfig],
    db_incremental_field_last_value: Optional[Any],
    should_use_incremental_field: bool = False,
):
    config: RESTAPIConfig = {
        "client": {
            "base_url": f"https://{site_name}.chargebee.com/api",
            "auth": {
                "type": "http_basic",
                "username": api_key,
                "password": "",
            },
            "paginator": ChargebeePaginator(),
        },
        "resource_defaults": {
            "write_disposition": {
                "disposition": "merge",
                "strategy": "upsert",
            }
            if should_use_incremental_field
            else "replace",
        },
        "resources": [get_resource(endpoint, should_use_incremental_field, db_incremental_field_last_value)],
    }

    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 = {"next_offset": resume_config.next_offset}

    def save_checkpoint(state: Optional[dict[str, Any]]) -> None:
        # Only persist when there's a next page to resume to; the Redis TTL
        # handles cleanup on completion.
        if state and state.get("next_offset"):
            resumable_source_manager.save_state(ChargebeeResumeConfig(next_offset=str(state["next_offset"])))

    return rest_api_resource(
        config,
        team_id,
        job_id,
        db_incremental_field_last_value,
        resume_hook=save_checkpoint,
        initial_paginator_state=initial_paginator_state,
    )


def validate_credentials(api_key: str, site_name: str) -> bool:
    basic_token = base64.b64encode(f"{api_key}:".encode("ascii")).decode("ascii")
    res = make_tracked_session().get(
        f"https://{site_name}.chargebee.com/api/v2/customers?limit=1",
        headers={"Authorization": f"Basic {basic_token}"},
    )
    return res.status_code == 200