: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.perigon._config import PerigonSourceConfig
from products.warehouse_sources.sources.perigon.perigon import (
PerigonResumeConfig,
perigon_source,
validate_credentials as validate_perigon_credentials,
)
from products.warehouse_sources.sources.perigon.settings import ENDPOINTS, INCREMENTAL_FIELDS, PERIGON_ENDPOINTS
from products.warehouse_sources.sources.sdk import (
CanonicalDescriptions,
DataWarehouseSourceCategory,
ExternalDataSourceType,
FieldType,
ReleaseStatus,
ResumableSource,
ResumableSourceManager,
SourceConfig,
SourceFieldInputConfig,
SourceFieldInputConfigType,
SourceInputs,
SourceRegistry,
SourceResponse,
SourceSchema,
build_endpoint_schemas,
detect,
)
@SourceRegistry.register
class PerigonSource(ResumableSource[PerigonSourceConfig, PerigonResumeConfig]):
codebase_detection = detect(
env=("^PERIGON_API_KEY$",),
npm=("@goperigon/perigon-ts",),
pypi=("perigon",),
go=("github.com/goperigon/perigon-go-sdk",),
)
lists_tables_without_credentials = True # static endpoint catalog — safe for public docs
api_docs_url = "https://docs.perigon.io/docs/overview"
@property
def source_type(self) -> ExternalDataSourceType:
return ExternalDataSourceType.PERIGON
@property
def get_source_config(self) -> SourceConfig:
return SourceConfig(
name=ExternalDataSourceType.PERIGON,
category=DataWarehouseSourceCategory.ANALYTICS,
label="Perigon",
releaseStatus=ReleaseStatus.BETA,
keywords=["news", "media", "goperigon"],
caption="""Enter your Perigon API key to sync news articles, story clusters and their history, journalists, sources, source groups, people, companies, topics, and Wikipedia pages into the PostHog Data warehouse.
You can find your API key on your [Perigon account dashboard](https://www.perigon.io/).
Datasets available to sync depend on your Perigon plan. Perigon caps each search to 10,000 results, so incremental syncs on articles, stories, and Wikipedia pages catch up across runs.""",
iconPath="/static/services/perigon.png",
docsUrl="https://posthog.com/docs/cdp/sources/perigon",
fields=cast(
list[FieldType],
[
SourceFieldInputConfig(
name="api_key",
label="API key",
type=SourceFieldInputConfigType.PASSWORD,
required=True,
placeholder="",
secret=True,
),
],
),
)
def get_non_retryable_errors(self) -> dict[str, str | None]:
# An invalid/revoked key (401) or a dataset outside the plan (403) surfaces as a
# requests HTTPError; match the stable status text and base host, not the
# per-request path/query.
return {
"401 Client Error: Unauthorized for url: https://api.perigon.io": "Your Perigon API key is invalid or has been revoked. Generate a new key on your Perigon account dashboard, then reconnect.",
"403 Client Error: Forbidden for url: https://api.perigon.io": "Your Perigon plan does not include access to this dataset. Deselect the table or upgrade your Perigon plan, then try again.",
}
def get_canonical_descriptions(self) -> CanonicalDescriptions:
from products.warehouse_sources.sources.perigon.canonical_descriptions import (
CANONICAL_DESCRIPTIONS, # noqa: PLC0415
)
return CANONICAL_DESCRIPTIONS
def get_schemas(
self,
config: PerigonSourceConfig,
team_id: int,
with_counts: bool = False,
names: list[str] | None = None,
force_refresh: bool = False,
api_version: str | None = None,
) -> list[SourceSchema]:
# Only articles, stories, story history, and Wikipedia pages have a documented
# server-side timestamp filter; the other datasets are full refresh.
return build_endpoint_schemas(ENDPOINTS, INCREMENTAL_FIELDS, names)
def validate_credentials(
self,
config: PerigonSourceConfig,
team_id: int,
schema_name: Optional[str] = None,
api_version: str | None = None,
) -> tuple[bool, str | None]:
endpoint = schema_name if schema_name in PERIGON_ENDPOINTS else None
ok, status = validate_perigon_credentials(config.api_key, endpoint=endpoint)
if ok:
return True, None
if status == 403:
if schema_name is None:
# The key is genuine; a plan may simply not include every dataset. Sync-time
# 403s on specific tables are handled by get_non_retryable_errors.
return True, None
return False, f"Your Perigon plan does not include access to {schema_name}"
if status == 401:
return False, "Perigon API key is invalid or has been revoked"
return False, "Could not validate Perigon credentials"
def get_resumable_source_manager(self, inputs: SourceInputs) -> ResumableSourceManager[PerigonResumeConfig]:
return ResumableSourceManager[PerigonResumeConfig](inputs, PerigonResumeConfig)
def source_for_pipeline(
self,
config: PerigonSourceConfig,
resumable_source_manager: ResumableSourceManager[PerigonResumeConfig],
inputs: SourceInputs,
) -> SourceResponse:
return perigon_source(
api_key=config.api_key,
endpoint=inputs.schema_name,
team_id=inputs.team_id,
job_id=inputs.job_id,
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,
logger=inputs.logger,
)