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.

5.2 KB
Raw Blame
from dataclasses import dataclass, field
from typing import Literal

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

# Qualtrics serves every brand from a datacenter-specific host; the API version is a path
# segment under it (`https://{datacenter}.qualtrics.com/API/v3`).
QUALTRICS_API_VERSION = "v3"
QUALTRICS_DEFAULT_DOMAIN_SUFFIX = ".qualtrics.com"

# How an endpoint reaches its rows.
#   "list"          — a brand-wide paginated collection under `result.elements`
#   "survey_query"  — the same collection shape, filtered per survey via a `surveyId` param
#   "survey_path"   — one un-paginated `result.elements` payload per survey, keyed in the path
#   "survey_export" — the async response-export pipeline (create job -> poll -> download zip)
#   "directory_path" — a paginated `result.elements` collection per XM Directory, keyed in the path
#   "distribution_path" — a paginated collection per distribution, reached through each survey's
#                         distribution list (surveys -> distributions -> rows)
FetchMode = Literal["list", "survey_query", "survey_path", "survey_export", "directory_path", "distribution_path"]


@dataclass(frozen=True)
class QualtricsEndpointConfig:
    name: str
    # Path under `/API/{version}`. `{survey_id}`, `{directory_id}` or `{distribution_id}` is
    # substituted per parent.
    path: str
    primary_key: list[str]
    fetch_mode: FetchMode = "list"
    params: dict[str, str] = field(default_factory=dict)
    incremental_fields: list[IncrementalField] = field(default_factory=list)
    # Stable creation-time field, never a `lastModified`-style column that would rewrite
    # partitions on every sync.
    partition_key: str | None = None

    @property
    def fans_out(self) -> bool:
        return self.fetch_mode != "list"

    @property
    def parent_path(self) -> str:
        return "/directories" if self.fetch_mode == "directory_path" else "/surveys"


RECORDED_DATE_INCREMENTAL = incremental_field("recordedDate")

QUALTRICS_ENDPOINTS: dict[str, QualtricsEndpointConfig] = {
    "surveys": QualtricsEndpointConfig(
        name="surveys",
        path="/surveys",
        primary_key=["id"],
        # `lastModified` is returned but Qualtrics exposes no modified-since filter on the
        # collection, so a cursor here would still walk every page — full refresh only.
        partition_key="creationDate",
    ),
    "users": QualtricsEndpointConfig(
        name="users",
        path="/users",
        primary_key=["id"],
    ),
    "groups": QualtricsEndpointConfig(
        name="groups",
        path="/groups",
        primary_key=["id"],
    ),
    "divisions": QualtricsEndpointConfig(
        name="divisions",
        path="/divisions",
        primary_key=["divisionId"],
    ),
    "distributions": QualtricsEndpointConfig(
        name="distributions",
        path="/distributions",
        # Distributions are listed per survey, so the survey id is part of the row identity.
        primary_key=["surveyId", "id"],
        fetch_mode="survey_query",
    ),
    "survey_questions": QualtricsEndpointConfig(
        name="survey_questions",
        path="/survey-definitions/{survey_id}/questions",
        primary_key=["surveyId", "QuestionID"],
        fetch_mode="survey_path",
    ),
    "survey_responses": QualtricsEndpointConfig(
        name="survey_responses",
        path="/surveys/{survey_id}/export-responses",
        primary_key=["surveyId", "responseId"],
        fetch_mode="survey_export",
        # `startDate` on the export request filters server-side on `recordedDate`.
        incremental_fields=[RECORDED_DATE_INCREMENTAL],
        partition_key="recordedDate",
    ),
    # The XM Directory endpoints below have no modified-since filter, so they are full refresh.
    "directories": QualtricsEndpointConfig(
        name="directories",
        path="/directories",
        primary_key=["directoryId"],
        params={"includeCount": "true"},
    ),
    "directory_contacts": QualtricsEndpointConfig(
        name="directory_contacts",
        path="/directories/{directory_id}/contacts",
        primary_key=["directoryId", "contactId"],
        fetch_mode="directory_path",
    ),
    "mailing_lists": QualtricsEndpointConfig(
        name="mailing_lists",
        path="/directories/{directory_id}/mailinglists",
        primary_key=["directoryId", "mailingListId"],
        fetch_mode="directory_path",
        params={"includeCount": "true"},
    ),
    "distribution_history": QualtricsEndpointConfig(
        name="distribution_history",
        path="/distributions/{distribution_id}/history",
        primary_key=["distributionId", "contactId"],
        fetch_mode="distribution_path",
    ),
}

ENDPOINTS = tuple(QUALTRICS_ENDPOINTS)

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

# Endpoint probed during per-schema credential validation. Fan-out endpoints have no reachable
# URL without a parent id, so they validate through their parent list instead.
VALIDATION_PATHS: dict[str, str] = {
    name: config.parent_path if config.fans_out else config.path for name, config in QUALTRICS_ENDPOINTS.items()
}