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.8 KB
Raw Blame
"""Help Scout source settings and endpoint catalog."""

from dataclasses import field
from typing import Any, Optional

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

HELP_SCOUT_API_VERSION_V2 = "v2"
HELP_SCOUT_API_VERSION_V3 = "v3"

HELP_SCOUT_API_BASE = "https://api.helpscout.net/v2"
HELP_SCOUT_API_BASE_V3 = "https://api.helpscout.net/v3"


@frozen
class HelpScoutEndpointConfig:
    name: str
    path: str
    # Key the rows live under inside the HAL `_embedded` object, or at the body root for reports.
    embedded_key: str
    primary_key: list[str] = field(default_factory=lambda: ["id"])
    incremental_fields: list[IncrementalField] = field(default_factory=list)
    default_incremental_field: Optional[str] = None
    # Server-side `modifiedSince` filter param name; None means full refresh only.
    updated_since_param: Optional[str] = None
    # Every Help Scout resource carries an immutable createdAt.
    partition_key: Optional[str] = "createdAt"
    # Only conversations/customers document a `sortField`/`sortOrder` param; other list
    # endpoints don't accept one (and reject unknown params on some Help Scout API versions).
    supports_sort: bool = False
    # Help Scout versions its Mailbox API per endpoint: only these endpoints have a v3 route, so
    # a v3 pin moves them and leaves every other endpoint on its v2 route.
    has_v3_route: bool = False
    # Structural requirement of `FanoutEndpointLike` (see common/rest_source/fanout.py); unused
    # here since threads' fan-out passes `page_size_param=None` (Help Scout has no page-size param).
    page_size: int = 50
    fanout: Optional[DependentEndpointConfig] = None
    params: dict[str, Any] = field(default_factory=dict)
    # Reports return a plain JSON body (`results` with `page`/`pages`) instead of HAL, and require a
    # `start`/`end` window.
    is_report: bool = False


# The conversations list returns only active conversations unless a status is sent.
_ALL_CONVERSATION_STATUSES: dict[str, Any] = {"status": "all"}

HELP_SCOUT_ENDPOINTS: dict[str, HelpScoutEndpointConfig] = {
    "conversations": HelpScoutEndpointConfig(
        name="conversations",
        path="/conversations",
        embedded_key="conversations",
        # Rows carry no `modifiedAt`. Only some changes move `userUpdatedAt`, so a watermark on it
        # lags `modifiedSince` and a sync re-reads rows rather than skipping them.
        incremental_fields=[incremental_field("userUpdatedAt")],
        default_incremental_field="userUpdatedAt",
        updated_since_param="modifiedSince",
        supports_sort=True,
        has_v3_route=True,
        params=_ALL_CONVERSATION_STATUSES,
    ),
    "customers": HelpScoutEndpointConfig(
        name="customers",
        path="/customers",
        embedded_key="customers",
        incremental_fields=[incremental_field("updatedAt")],
        default_incremental_field="updatedAt",
        updated_since_param="modifiedSince",
        supports_sort=True,
        has_v3_route=True,
    ),
    "mailboxes": HelpScoutEndpointConfig(
        name="mailboxes",
        path="/mailboxes",
        embedded_key="mailboxes",
    ),
    "users": HelpScoutEndpointConfig(
        name="users",
        path="/users",
        embedded_key="users",
    ),
    "tags": HelpScoutEndpointConfig(
        name="tags",
        path="/tags",
        embedded_key="tags",
    ),
    "workflows": HelpScoutEndpointConfig(
        name="workflows",
        path="/workflows",
        embedded_key="workflows",
    ),
    "threads": HelpScoutEndpointConfig(
        name="threads",
        path="/conversations/{conversation_id}/threads",
        embedded_key="threads",
        # Thread ids are unique only within their conversation, so the parent id is part of the
        # key to keep it unique across the whole table.
        primary_key=["conversation_id", "id"],
        has_v3_route=True,
        fanout=DependentEndpointConfig(
            parent_name="conversations",
            resolve_param="conversation_id",
            resolve_field="id",
            include_from_parent=["id"],
            parent_field_renames={"id": "conversation_id"},
            parent_params=_ALL_CONVERSATION_STATUSES,
        ),
    ),
    "mailbox_fields": HelpScoutEndpointConfig(
        name="mailbox_fields",
        path="/mailboxes/{mailbox_id}/fields",
        embedded_key="fields",
        # Field ids aren't documented as unique across mailboxes.
        primary_key=["mailbox_id", "id"],
        partition_key=None,
        fanout=DependentEndpointConfig(
            parent_name="mailboxes",
            resolve_param="mailbox_id",
            resolve_field="id",
            include_from_parent=["id"],
            parent_field_renames={"id": "mailbox_id"},
        ),
    ),
    "organizations": HelpScoutEndpointConfig(
        name="organizations",
        path="/organizations",
        embedded_key="organizations",
        partition_key=None,
        # The default sort is lastInteractionAt desc, which reorders rows while a sync paginates.
        params={"sort": "name,asc"},
    ),
    "teams": HelpScoutEndpointConfig(
        name="teams",
        path="/teams",
        embedded_key="teams",
    ),
    "happiness_ratings": HelpScoutEndpointConfig(
        name="happiness_ratings",
        path="/reports/happiness/ratings",
        embedded_key="results",
        # `id` is the conversation id and `ratingId` is the rating value, so the rated thread
        # identifies the row.
        primary_key=["id", "threadid"],
        partition_key="ratingCreatedAt",
        params={"sortField": "number", "sortOrder": "ASC"},
        is_report=True,
    ),
}

ENDPOINTS = tuple(HELP_SCOUT_ENDPOINTS.keys())

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