: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.elasticemail._config import ElasticemailSourceConfig
from products.warehouse_sources.sources.elasticemail.elasticemail import (
ElasticEmailResumeConfig,
elasticemail_source,
validate_credentials as validate_elasticemail_credentials,
)
from products.warehouse_sources.sources.elasticemail.settings import (
ELASTICEMAIL_ENDPOINTS,
ENDPOINTS,
INCREMENTAL_FIELDS,
)
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 ElasticemailSource(ResumableSource[ElasticemailSourceConfig, ElasticEmailResumeConfig]):
codebase_detection = detect(
env=("^ELASTICEMAIL_", "^ELASTIC_EMAIL_"),
npm=("@elasticemail/elasticemail-client", "@elasticemail/elasticemail-client-ts-axios"),
pypi=("elasticemail",),
composer=("elasticemail/elasticemail-php",),
)
supported_versions = ("v4",)
default_version = "v4"
api_docs_url = "https://elasticemail.com/developers/api-documentation/rest-api"
@property
def source_type(self) -> ExternalDataSourceType:
return ExternalDataSourceType.ELASTICEMAIL
@property
def get_source_config(self) -> SourceConfig:
return SourceConfig(
name=ExternalDataSourceType.ELASTICEMAIL,
category=DataWarehouseSourceCategory.MARKETING___EMAIL,
label="Elastic Email",
releaseStatus=ReleaseStatus.BETA,
caption="""Enter your Elastic Email API key to pull your Elastic Email data into the PostHog Data warehouse.
You can create an API key in your [Elastic Email account settings](https://app.elasticemail.com/marketing/settings/new/manage-api).
Grant the key read access to the data you want to sync (Contacts, Campaigns, Templates, Reports, etc.).""",
iconPath="/static/services/elasticemail.png",
docsUrl="https://posthog.com/docs/cdp/sources/elasticemail",
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]:
return {
# Elastic Email rejects bad/expired/under-scoped keys with HTTP 400; `_fetch_page` raises an
# ElasticEmailAuthError carrying this marker. Retrying can never fix a credential problem.
"Elastic Email API authentication failed": "Your Elastic Email API key is invalid, expired, or missing the required read permissions. Create a new key in your Elastic Email account settings, then reconnect.",
}
def get_canonical_descriptions(self) -> CanonicalDescriptions:
from products.warehouse_sources.sources.elasticemail.canonical_descriptions import CANONICAL_DESCRIPTIONS
return CANONICAL_DESCRIPTIONS
def get_schemas(
self,
config: ElasticemailSourceConfig,
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 = ELASTICEMAIL_ENDPOINTS[endpoint]
has_incremental_field = bool(INCREMENTAL_FIELDS.get(endpoint))
return SourceSchema(
name=endpoint,
# Events are immutable, so they sync append-only via the server-side `from` filter. Daily
# statistics merge on Date via the `from`/`to` range. No other list endpoint exposes a
# server-side timestamp filter, so they are full refresh only.
supports_incremental=has_incremental_field and not endpoint_config.append_only,
supports_append=has_incremental_field and endpoint_config.append_only,
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: ElasticemailSourceConfig,
team_id: int,
schema_name: Optional[str] = None,
api_version: str | None = None,
) -> tuple[bool, str | None]:
# At source-create (schema_name=None) probe a cheap generic endpoint; for a specific schema, probe
# that endpoint's path so we surface a per-endpoint permission problem early.
if schema_name is not None and schema_name in ELASTICEMAIL_ENDPOINTS:
endpoint_config = ELASTICEMAIL_ENDPOINTS[schema_name]
# A fan-out child's path has a placeholder, so probe the parent listing it reads first.
if endpoint_config.fanout is not None:
endpoint_config = ELASTICEMAIL_ENDPOINTS[endpoint_config.fanout.parent_name]
valid = validate_elasticemail_credentials(
config.api_key, endpoint_config.path, endpoint_config.extra_params
)
else:
valid = validate_elasticemail_credentials(config.api_key)
if valid:
return True, None
return False, "Invalid Elastic Email API key"
def get_resumable_source_manager(self, inputs: SourceInputs) -> ResumableSourceManager[ElasticEmailResumeConfig]:
return ResumableSourceManager[ElasticEmailResumeConfig](inputs, ElasticEmailResumeConfig)
def source_for_pipeline(
self,
config: ElasticemailSourceConfig,
resumable_source_manager: ResumableSourceManager[ElasticEmailResumeConfig],
inputs: SourceInputs,
) -> SourceResponse:
return elasticemail_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,
)