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.2 KB
Raw Blame
import dataclasses
from typing import Optional
from urllib.parse import parse_qs, urlencode, urlparse

import requests

from products.warehouse_sources.sources.polar.settings import ENDPOINT_SORT_FIELDS, ENDPOINTS
from products.warehouse_sources.sources.sdk import ResumableSourceManager, SourceResponse, make_tracked_session

POLAR_BASE_URL = "https://api.polar.sh"
PAGE_SIZE = 100

# Polar selects a dated API version via the `Polar-Version` header and serves its floating
# "Current" version when the header is absent. The legacy unversioned label ("v1") predates
# this source declaring a version and keeps sending no header, so already-pinned rows make the
# exact request they always have. Dated labels send the header so they stay on that contract.
POLAR_VERSION_2026_10 = "2026-10"

_VERSION_HEADER: dict[str, str] = {
    POLAR_VERSION_2026_10: POLAR_VERSION_2026_10,
}


@dataclasses.dataclass(frozen=False)
class PolarResumeConfig:
    next_url: str


NO_READABLE_SCOPE_MESSAGE = (
    "Your Polar Organization Access Token can't read any of the data this source syncs. "
    "Create a token with the read scopes listed in the setup instructions, then reconnect."
)


class PolarPermissionError(Exception):
    pass


def _get_polar_session() -> requests.Session:
    # DEFAULT_RETRY backs off on 429/5xx and transient connection errors (honoring
    # Retry-After), so a dropped connection mid-sync doesn't fail the whole activity.
    # 401/403 aren't in its status_forcelist, so credential errors still surface immediately.
    return make_tracked_session()


def _build_headers(api_key: str, api_version: str) -> dict[str, str]:
    headers = {
        "Authorization": f"Bearer {api_key}",
        "Accept": "application/json",
    }
    version = _VERSION_HEADER.get(api_version)
    if version:
        headers["Polar-Version"] = version
    return headers


def _build_url(endpoint: str, page: int) -> str:
    params: dict[str, str | int] = {"limit": PAGE_SIZE, "page": page}
    sort_field = ENDPOINT_SORT_FIELDS.get(endpoint)
    if sort_field is not None:
        params["sorting"] = sort_field
    return f"{POLAR_BASE_URL}/v1/{endpoint}/?{urlencode(params)}"


def _page_from_url(url: str) -> int:
    qs = parse_qs(urlparse(url).query)
    page_values = qs.get("page", ["1"])
    try:
        return int(page_values[0])
    except (ValueError, IndexError):
        return 1


def get_rows(
    api_key: str,
    endpoint: str,
    api_version: str,
    resumable_source_manager: ResumableSourceManager[PolarResumeConfig],
):
    headers = _build_headers(api_key, api_version)

    url: Optional[str] = _build_url(endpoint, page=1)

    resume_config = resumable_source_manager.load_state() if resumable_source_manager.can_resume() else None
    if resume_config and resume_config.next_url:
        url = resume_config.next_url

    session = _get_polar_session()
    seen_urls: set[str] = set()

    while url:
        if url in seen_urls:
            break
        seen_urls.add(url)

        response = session.request("GET", url, headers=headers)
        response.raise_for_status()
        data = response.json()

        # Yield the whole page list; the pipeline batcher handles chunking into
        # pa.Tables at its own thresholds (5000 rows / 200 MiB) and accepts
        # lists directly without per-row overhead.
        items = data.get("items", [])
        if items:
            yield items

        pagination = data.get("pagination") or {}
        max_page = int(pagination.get("max_page") or 0)
        current_page = _page_from_url(url)

        if current_page < max_page:
            url = _build_url(endpoint, page=current_page + 1)
        else:
            url = None

        if url:
            resumable_source_manager.save_state(PolarResumeConfig(next_url=url))
        else:
            resumable_source_manager.save_state(PolarResumeConfig(next_url=""))


def polar_source(
    api_key: str,
    endpoint: str,
    api_version: str,
    resumable_source_manager: ResumableSourceManager[PolarResumeConfig],
) -> SourceResponse:
    def items():
        yield from get_rows(
            api_key=api_key,
            endpoint=endpoint,
            api_version=api_version,
            resumable_source_manager=resumable_source_manager,
        )

    return SourceResponse(
        items=items,
        primary_keys=["id"],
        name=endpoint,
        column_hints={},
    )


def validate_credentials(api_key: str, api_version: str, table_name: Optional[str] = None) -> bool:
    headers = _build_headers(api_key, api_version)
    session = _get_polar_session()

    # When called per-schema (the incremental_fields action), a 403 should surface
    # immediately so the wizard can name the scope that endpoint needs.
    # When called at source-create (no table_name), the user may legitimately have
    # scopes for only a subset of endpoints, so we accept any single non-403
    # response as proof of a usable token. Per-endpoint scope checks are deferred
    # to the per-schema action / first sync. We do require ≥1 readable endpoint —
    # a token that 403s on every endpoint has no usable scope and would create a
    # source guaranteed to fail on first sync.
    if table_name and table_name in ENDPOINTS:
        is_create_probe = False
        endpoints_to_check: list[str] = [table_name]
    elif table_name:
        # A known per-schema caller (incremental_fields) but an endpoint we don't
        # support — reject explicitly rather than silently degrading to the
        # create-probe path, which would falsely pass if any other endpoint is readable.
        raise ValueError(f"Unknown Polar endpoint: {table_name}")
    else:
        is_create_probe = True
        endpoints_to_check = list(ENDPOINTS)

    forbidden: list[str] = []
    last_failure: Optional[requests.Response] = None
    for endpoint in endpoints_to_check:
        response = session.request(
            "GET",
            f"{POLAR_BASE_URL}/v1/{endpoint}/",
            headers=headers,
            params={"limit": 1},
        )
        if response.status_code == 403:
            if is_create_probe:
                forbidden.append(endpoint)
                continue
            raise PolarPermissionError(
                f"Your Polar Organization Access Token can't read {endpoint}. "
                f"Create a token with the {endpoint}:read scope, then reconnect."
            )
        if is_create_probe:
            # First non-403 success is proof enough — stop probing so a transient
            # 5xx on a later endpoint can't block source creation.
            if response.ok:
                return True
            # Transient non-403 (e.g. 5xx, 401) during create-probe: don't blow
            # up the whole validation — record it and let later endpoints answer.
            last_failure = response
            continue
        response.raise_for_status()

    if is_create_probe:
        if forbidden and not last_failure:
            raise PolarPermissionError(NO_READABLE_SCOPE_MESSAGE)
        if last_failure is not None:
            # Only transient errors (and possibly some 403s) — no endpoint
            # confirmed the token works. Surface the underlying HTTP error so
            # the user gets actionable feedback rather than a silent success.
            last_failure.raise_for_status()

    return True