: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
from typing import Optional, cast
from products.warehouse_sources.sources.pylon._config import PylonSourceConfig
from products.warehouse_sources.sources.pylon.pylon import (
PYLON_EU_BASE_URL,
PYLON_US_BASE_URL,
PylonResumeConfig,
base_url_for_token,
pylon_source,
validate_credentials as validate_pylon_credentials,
)
from products.warehouse_sources.sources.pylon.settings import ENDPOINTS, INCREMENTAL_FIELDS, PYLON_ENDPOINTS
from products.warehouse_sources.sources.sdk import (
CanonicalDescriptions,
DataWarehouseSourceCategory,
ExternalDataSourceType,
FieldType,
ReleaseStatus,
ResumableSource,
ResumableSourceManager,
SourceConfig,
SourceFieldInputConfig,
SourceFieldInputConfigType,
SourceInputs,
SourceRegistry,
SourceResponse,
SourceSchema,
detect,
)
@SourceRegistry.register
class PylonSource(ResumableSource[PylonSourceConfig, PylonResumeConfig]):
codebase_detection = detect(env=("^PYLON_",), script_hosts=("widget.usepylon.com", "widget.eu.usepylon.com"))
api_docs_url = "https://docs.usepylon.com"
@property
def source_type(self) -> ExternalDataSourceType:
return ExternalDataSourceType.PYLON
@property
def get_source_config(self) -> SourceConfig:
return SourceConfig(
name=ExternalDataSourceType.PYLON,
category=DataWarehouseSourceCategory.CUSTOMER_SUPPORT,
label="Pylon",
releaseStatus=ReleaseStatus.GA,
caption="""Enter your Pylon API token to pull your Pylon support data into the PostHog Data warehouse.
You can create an API token from your Pylon dashboard under **Settings > API tokens** (admin only). US and EU workspaces are detected from the token, so there is no region to pick.""",
iconPath="/static/services/pylon.png",
docsUrl="https://posthog.com/docs/cdp/sources/pylon",
fields=cast(
list[FieldType],
[
SourceFieldInputConfig(
name="api_token",
label="API token",
type=SourceFieldInputConfigType.PASSWORD,
required=True,
placeholder="",
secret=True,
),
],
),
)
def get_non_retryable_errors(self) -> dict[str, str | None]:
invalid_token = "Your Pylon API token is invalid or has been revoked. Create a new token in Settings > API tokens and reconnect."
missing_permissions = "Your Pylon API token is missing the permissions needed to sync this data. Recreate the token with the required access and reconnect."
# A bad/revoked token surfaces as an HTTPError from `raise_for_status()`; retrying can never
# fix a credential problem. Match the stable status text and base host, not the per-request
# path/query. Both regional hosts need a key, since the URL in the error is whichever host the
# token routed to.
errors: dict[str, str | None] = {}
for base_url in (PYLON_US_BASE_URL, PYLON_EU_BASE_URL):
errors[f"401 Client Error: Unauthorized for url: {base_url}"] = invalid_token
errors[f"403 Client Error: Forbidden for url: {base_url}"] = missing_permissions
return errors
def get_schemas(
self,
config: PylonSourceConfig,
team_id: int,
with_counts: bool = False,
names: list[str] | None = None,
force_refresh: bool = False,
api_version: str | None = None,
) -> list[SourceSchema]:
def _build_schema(endpoint: str) -> SourceSchema:
endpoint_config = PYLON_ENDPOINTS[endpoint]
return SourceSchema(
name=endpoint,
supports_incremental=endpoint_config.supports_incremental,
supports_append=endpoint_config.supports_incremental,
incremental_fields=INCREMENTAL_FIELDS.get(endpoint, []),
should_sync_default=endpoint_config.should_sync_default,
)
schemas = [_build_schema(endpoint) for endpoint in ENDPOINTS]
if names is not None:
names_set = set(names)
schemas = [s for s in schemas if s.name in names_set]
return schemas
def validate_credentials(
self, config: PylonSourceConfig, team_id: int, schema_name: Optional[str] = None, api_version: str | None = None
) -> tuple[bool, str | None]:
if validate_pylon_credentials(config.api_token):
return True, None
return (
False,
f"Pylon rejected this API token at {base_url_for_token(config.api_token)}. "
"PostHog picks that host from the token's prefix, so check that you copied the whole token "
"from the Pylon workspace you want to sync. You can create a new one under Settings > API tokens.",
)
def get_canonical_descriptions(self) -> CanonicalDescriptions:
from products.warehouse_sources.sources.pylon.canonical_descriptions import CANONICAL_DESCRIPTIONS
return CANONICAL_DESCRIPTIONS
def get_resumable_source_manager(self, inputs: SourceInputs) -> ResumableSourceManager[PylonResumeConfig]:
return ResumableSourceManager[PylonResumeConfig](inputs, PylonResumeConfig)
def source_for_pipeline(
self,
config: PylonSourceConfig,
resumable_source_manager: ResumableSourceManager[PylonResumeConfig],
inputs: SourceInputs,
) -> SourceResponse:
return pylon_source(
api_token=config.api_token,
endpoint=inputs.schema_name,
logger=inputs.logger,
resumable_source_manager=resumable_source_manager,
should_use_incremental_field=inputs.should_use_incremental_field,
db_incremental_field_last_value=inputs.db_incremental_field_last_value
if inputs.should_use_incremental_field
else None,
)