: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
import datetime
import dataclasses
from collections.abc import Iterator
from typing import Any
import requests
from structlog.types import FilteringBoundLogger
from tenacity import retry_if_exception_type, stop_after_attempt, wait_exponential_jitter
from products.warehouse_sources.sources.payfit.settings import (
INCREMENTAL_REREAD_MONTHS,
MONTHLY_HISTORY_LOOKBACK,
PAYFIT_ENDPOINTS,
PayFitEndpointConfig,
)
from products.warehouse_sources.sources.sdk import (
ResumableSourceManager,
SourceResponse,
make_tracked_session,
retry_with_deadline,
)
PAYFIT_BASE_URL = "https://partner-api.payfit.com"
# The API key only grants access to the key's own company; introspection returns its company_id.
PAYFIT_INTROSPECT_URL = "https://oauth.payfit.com/introspect"
# List endpoints accept a `maxResults` of up to 50 (default 10); the largest page minimises round trips.
PAGE_SIZE = 50
REQUEST_TIMEOUT_SECONDS = 60
# Interactive credential validation runs on the API request thread, so it gets a tighter
# per-attempt budget than sync-time calls.
VALIDATION_TIMEOUT_SECONDS = 15
INVALID_TOKEN_MESSAGE = "PayFit API key is inactive or invalid"
class PayFitRetryableError(Exception):
pass
class PayFitInvalidTokenError(Exception):
pass
@dataclasses.dataclass(frozen=True)
class PayFitResumeConfig:
# `nextPageToken` cursor for the next page. For `payslips` this is the cursor of the
# *collaborators* page being fanned out over. `None` means start from the first page. A crashed
# sync resumes from the page after the last one yielded; merge dedupes on the primary key.
next_page_token: str | None = None
# Pay period (`YYYYMM`) to resume at, for month-scoped endpoints. `next_page_token` then pages
# within that month.
month: str | None = None
def _today() -> datetime.date:
return datetime.datetime.now(datetime.UTC).date()
def _period(month: datetime.date) -> str:
return f"{month.year:04d}{month.month:02d}"
def _parse_period(period: str) -> datetime.date:
return datetime.date(int(period[:4]), int(period[4:6]), 1)
def _next_month(month: datetime.date) -> datetime.date:
return datetime.date(month.year + month.month // 12, month.month % 12 + 1, 1)
def _to_date(value: Any) -> datetime.date | None:
if value is None:
return None
if isinstance(value, datetime.datetime):
return value.date()
if isinstance(value, datetime.date):
return value
try:
return datetime.date.fromisoformat(str(value)[:10])
except ValueError:
return None
def resolve_first_month(
today: datetime.date,
history_start: datetime.datetime | None,
db_incremental_field_last_value: Any,
) -> datetime.date:
watermark = _to_date(db_incremental_field_last_value)
if watermark is not None:
month = min(watermark, today).replace(day=1)
months_back = month.year * 12 + month.month - 1 - INCREMENTAL_REREAD_MONTHS
return datetime.date(months_back // 12, months_back % 12 + 1, 1)
start = history_start.date() if history_start is not None else today - MONTHLY_HISTORY_LOOKBACK
return start.replace(day=1)
def _headers(api_key: str) -> dict[str, str]:
return {"Authorization": f"Bearer {api_key}", "Accept": "application/json"}
def _make_introspection_session(api_key: str) -> requests.Session:
# capture=False: the introspection request body carries the raw API key under the generic
# `token` field, which the name-based sample scrubbers don't recognise — keep the exchange out
# of HTTP sample capture entirely (it stays metered and logged). allow_redirects=False pins the
# credential-bearing request to the introspection host.
return make_tracked_session(
headers=_headers(api_key),
redact_values=(api_key,),
capture=False,
allow_redirects=False,
)
@retry_with_deadline(
retry=retry_if_exception_type((PayFitRetryableError, requests.ReadTimeout, requests.ConnectionError)),
stop=stop_after_attempt(5),
wait=wait_exponential_jitter(initial=1, max=30),
reraise=True,
)
def _introspect(session: requests.Session, api_key: str, timeout: float = REQUEST_TIMEOUT_SECONDS) -> dict[str, Any]:
response = session.post(
PAYFIT_INTROSPECT_URL,
json={"token": api_key},
timeout=timeout,
)
if response.status_code == 429 or response.status_code >= 500:
raise PayFitRetryableError(f"PayFit introspection error (retryable): status={response.status_code}")
# Introspection authenticates with the token itself, so a 401/403 means the key is bad — the
# same signal as an {"active": false} body.
if response.status_code in (401, 403):
return {"active": False}
if not response.ok:
response.raise_for_status()
data = response.json()
if not isinstance(data, dict):
raise PayFitRetryableError(f"PayFit introspection returned an unexpected payload: {type(data).__name__}")
return data
def get_company_id(api_key: str) -> str:
claims = _introspect(_make_introspection_session(api_key), api_key)
if not claims.get("active"):
raise PayFitInvalidTokenError(INVALID_TOKEN_MESSAGE)
company_id = claims.get("company_id")
if not isinstance(company_id, str) or not company_id:
raise PayFitInvalidTokenError(f"{INVALID_TOKEN_MESSAGE}: introspection returned no company_id")
return company_id
@retry_with_deadline(
retry=retry_if_exception_type((PayFitRetryableError, requests.ReadTimeout, requests.ConnectionError)),
stop=stop_after_attempt(5),
wait=wait_exponential_jitter(initial=1, max=30),
reraise=True,
)
def _fetch_page(
session: requests.Session,
path: str,
data_key: str,
next_page_token: str | None,
extra_params: dict[str, str],
logger: FilteringBoundLogger,
missing_ok: bool = False,
) -> tuple[list[dict[str, Any]], str | None]:
params: dict[str, Any] = {"maxResults": PAGE_SIZE, **extra_params}
if next_page_token is not None:
params["nextPageToken"] = next_page_token
response = session.get(
f"{PAYFIT_BASE_URL}{path}",
params=params,
timeout=REQUEST_TIMEOUT_SECONDS,
)
if response.status_code == 429 or response.status_code >= 500:
raise PayFitRetryableError(f"PayFit API error (retryable): status={response.status_code}, path={path}")
if response.status_code == 404 and missing_ok:
return [], None
if not response.ok:
logger.error(f"PayFit API error: status={response.status_code}, body={response.text}, path={path}")
response.raise_for_status()
data = response.json()
# List endpoints wrap rows as {"<data_key>": [...], "meta": {"nextPageToken": str | null}}.
if not isinstance(data, dict) or not isinstance(data.get(data_key), list):
raise PayFitRetryableError(f"PayFit returned an unexpected payload for {path}: {type(data).__name__}")
rows: list[dict[str, Any]] = data[data_key]
meta = data.get("meta") or {}
next_token = meta.get("nextPageToken") if isinstance(meta, dict) else None
return rows, next_token or None
@retry_with_deadline(
retry=retry_if_exception_type((PayFitRetryableError, requests.ReadTimeout, requests.ConnectionError)),
stop=stop_after_attempt(5),
wait=wait_exponential_jitter(initial=1, max=30),
reraise=True,
)
def _fetch_unpaginated(
session: requests.Session,
path: str,
params: dict[str, str],
logger: FilteringBoundLogger,
missing_ok: bool = False,
) -> list[dict[str, Any]]:
response = session.get(f"{PAYFIT_BASE_URL}{path}", params=params, timeout=REQUEST_TIMEOUT_SECONDS)
if response.status_code == 429 or response.status_code >= 500:
raise PayFitRetryableError(f"PayFit API error (retryable): status={response.status_code}, path={path}")
if response.status_code == 404 and missing_ok:
return []
if not response.ok:
logger.error(f"PayFit API error: status={response.status_code}, body={response.text}, path={path}")
response.raise_for_status()
data = response.json()
if isinstance(data, dict):
return [data]
if isinstance(data, list) and all(isinstance(row, dict) for row in data):
return data
raise PayFitRetryableError(f"PayFit returned an unexpected payload for {path}: {type(data).__name__}")
@retry_with_deadline(
retry=retry_if_exception_type((PayFitRetryableError, requests.ReadTimeout, requests.ConnectionError)),
stop=stop_after_attempt(5),
wait=wait_exponential_jitter(initial=1, max=30),
reraise=True,
)
def _fetch_payslips(
session: requests.Session,
company_id: str,
collaborator_id: str,
logger: FilteringBoundLogger,
) -> list[dict[str, Any]]:
path = f"/companies/{company_id}/collaborators/{collaborator_id}/payslips"
response = session.get(f"{PAYFIT_BASE_URL}{path}", timeout=REQUEST_TIMEOUT_SECONDS)
if response.status_code == 429 or response.status_code >= 500:
raise PayFitRetryableError(f"PayFit API error (retryable): status={response.status_code}, path={path}")
if not response.ok:
logger.error(f"PayFit API error: status={response.status_code}, body={response.text}, path={path}")
response.raise_for_status()
data = response.json()
if not isinstance(data, dict) or not isinstance(data.get("payslips"), list):
raise PayFitRetryableError(f"PayFit returned an unexpected payload for {path}: {type(data).__name__}")
# The response only carries year/month/contractId/payslipId, so stamp the parent id onto each
# row — it is part of the primary key.
return [{**payslip, "collaboratorId": collaborator_id} for payslip in data["payslips"]]
def get_rows(
api_key: str,
endpoint: str,
logger: FilteringBoundLogger,
resumable_source_manager: ResumableSourceManager[PayFitResumeConfig],
history_start: datetime.datetime | None = None,
db_incremental_field_last_value: Any = None,
) -> Iterator[list[dict[str, Any]]]:
config = PAYFIT_ENDPOINTS[endpoint]
session = make_tracked_session(headers=_headers(api_key), redact_values=(api_key,))
company_id = get_company_id(api_key)
resume = resumable_source_manager.load_state() if resumable_source_manager.can_resume() else None
next_page_token = resume.next_page_token if resume else None
if resume and resume.next_page_token is not None:
logger.debug(f"PayFit: resuming {endpoint} from page token {next_page_token}")
if config.monthly:
today = _today()
first_month = (
_parse_period(resume.month)
if resume and resume.month
else resolve_first_month(today, history_start, db_incremental_field_last_value)
)
yield from _get_monthly_rows(
session,
company_id,
config,
first_month,
today.replace(day=1),
next_page_token if resume and resume.month else None,
logger,
resumable_source_manager,
)
return
if config.fan_out_over_collaborators:
yield from _get_payslip_rows(session, company_id, next_page_token, logger, resumable_source_manager)
return
if not config.paginated or config.data_key is None:
rows = _fetch_unpaginated(session, f"/companies/{company_id}{config.path}", {}, logger)
if rows:
yield rows
return
path = f"/companies/{company_id}{config.path}"
while True:
rows, next_token = _fetch_page(session, path, config.data_key, next_page_token, config.extra_params, logger)
if rows:
yield rows
# A missing/null nextPageToken (or an empty page) means we've reached the end of the list.
if not next_token or not rows:
break
next_page_token = next_token
# Save AFTER yielding so a crash re-fetches from the next page (already-yielded pages are
# persisted); merge dedupes the re-pulled page on the primary key.
resumable_source_manager.save_state(PayFitResumeConfig(next_page_token=next_page_token))
def _get_payslip_rows(
session: requests.Session,
company_id: str,
next_page_token: str | None,
logger: FilteringBoundLogger,
resumable_source_manager: ResumableSourceManager[PayFitResumeConfig],
) -> Iterator[list[dict[str, Any]]]:
# Payslips have no list-all endpoint, so paginate collaborators and fetch each collaborator's
# payslips. Resume state tracks the collaborators page cursor: one page of collaborators is one
# yielded batch.
collaborators_path = f"/companies/{company_id}/collaborators"
while True:
collaborators, next_token = _fetch_page(
session, collaborators_path, "collaborators", next_page_token, {}, logger
)
batch: list[dict[str, Any]] = []
for collaborator in collaborators:
batch.extend(_fetch_payslips(session, company_id, collaborator["id"], logger))
if batch:
yield batch
if not next_token or not collaborators:
break
next_page_token = next_token
resumable_source_manager.save_state(PayFitResumeConfig(next_page_token=next_page_token))
def _get_monthly_rows(
session: requests.Session,
company_id: str,
config: PayFitEndpointConfig,
month: datetime.date,
last_month: datetime.date,
next_page_token: str | None,
logger: FilteringBoundLogger,
resumable_source_manager: ResumableSourceManager[PayFitResumeConfig],
) -> Iterator[list[dict[str, Any]]]:
path = f"/companies/{company_id}{config.path}"
while month <= last_month:
params = {**config.extra_params, "date": _period(month)}
# A 404 for one pay period, such as a month before the company joined PayFit, skips that
# month rather than failing the sync.
if config.paginated and config.data_key is not None:
rows, next_token = _fetch_page(
session, path, config.data_key, next_page_token, params, logger, missing_ok=True
)
else:
rows, next_token = _fetch_unpaginated(session, path, params, logger, missing_ok=True), None
batch = [
{**row, "month": month, **({"lineNumber": index} if config.number_rows else {})}
for index, row in enumerate(rows)
]
if next_token and rows:
next_page_token = next_token
else:
month, next_page_token = _next_month(month), None
resumable_source_manager.save_state(PayFitResumeConfig(next_page_token=next_page_token, month=_period(month)))
if batch:
yield batch
else:
resumable_source_manager.safe_point()
def payfit_source(
api_key: str,
endpoint: str,
logger: FilteringBoundLogger,
resumable_source_manager: ResumableSourceManager[PayFitResumeConfig],
history_start: datetime.datetime | None = None,
db_incremental_field_last_value: Any = None,
) -> SourceResponse:
config = PAYFIT_ENDPOINTS[endpoint]
return SourceResponse(
name=endpoint,
items=lambda: get_rows(
api_key=api_key,
endpoint=endpoint,
logger=logger,
resumable_source_manager=resumable_source_manager,
history_start=history_start,
db_incremental_field_last_value=db_incremental_field_last_value,
),
primary_keys=config.primary_keys,
partition_count=1,
partition_size=1,
)
def validate_credentials(api_key: str) -> tuple[bool, str | None]:
"""Validate the API key via token introspection — one cheap probe that needs no endpoint scope
and also proves we can resolve the company_id every sync depends on. Shares `_introspect` with
the sync path so a transient 429/5xx blip is retried rather than failing source creation."""
session = _make_introspection_session(api_key)
try:
claims = _introspect(session, api_key, timeout=VALIDATION_TIMEOUT_SECONDS)
except Exception as e:
return False, f"Could not validate PayFit API key: {e}"
if not claims.get("active"):
return False, "Invalid PayFit API key"
if not claims.get("company_id"):
return False, "PayFit token introspection returned no company ID"
return True, None
def check_schema_access(api_key: str, schema_name: str) -> tuple[bool, str | None]:
"""Probe a single endpoint to confirm the API key carries the scope that schema needs."""
endpoint_config = PAYFIT_ENDPOINTS[schema_name]
session = make_tracked_session(headers=_headers(api_key), redact_values=(api_key,))
try:
company_id = get_company_id(api_key)
except PayFitInvalidTokenError:
return False, "Invalid PayFit API key"
except Exception as e:
return False, f"Could not validate PayFit API key: {e}"
probe = _probe_endpoint(endpoint_config)
probe_params: dict[str, Any] = {**probe.extra_params}
if probe.paginated:
probe_params["maxResults"] = 1
if probe.monthly:
probe_params["date"] = _period(_today())
try:
response = session.get(
f"{PAYFIT_BASE_URL}/companies/{company_id}{probe.path}",
params=probe_params,
timeout=15,
)
except Exception as e:
return False, f"Could not connect to PayFit: {e}"
if response.status_code == 401:
return False, "Invalid PayFit API key"
if response.status_code == 403:
if probe.scope is None:
return False, f"Your PayFit API key cannot read `{schema_name}`"
return False, f"Your PayFit API key is missing the `{probe.scope}` scope required for `{schema_name}`"
# A month-scoped endpoint answers 404 for a pay period with no data, which still proves access.
if response.status_code == 404 and probe.monthly:
return True, None
if not response.ok:
return False, f"PayFit returned HTTP {response.status_code}"
return True, None
def _probe_endpoint(endpoint_config: PayFitEndpointConfig) -> PayFitEndpointConfig:
# Payslips are enumerated per collaborator, so probe the collaborators dependency instead of a
# per-collaborator path; a missing `contracts:payslips:read` scope still surfaces as a
# non-retryable 403 at sync time.
if endpoint_config.fan_out_over_collaborators:
return PAYFIT_ENDPOINTS["collaborators"]
return endpoint_config