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.

8.2 KB
Raw Blame
from typing import Optional, cast

from products.warehouse_sources.sources.aviationstack._config import AviationstackSourceConfig
from products.warehouse_sources.sources.aviationstack.aviationstack import (
    AviationstackResumeConfig,
    aviationstack_source,
    parse_iata_codes,
    validate_credentials as validate_aviationstack_credentials,
)
from products.warehouse_sources.sources.aviationstack.settings import AVIATIONSTACK_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 AviationstackSource(ResumableSource[AviationstackSourceConfig, AviationstackResumeConfig]):
    codebase_detection = detect(env=("^AVIATIONSTACK_",))

    lists_tables_without_credentials = True  # static endpoint catalog — safe for public docs
    api_docs_url = "https://aviationstack.com/documentation"

    @property
    def source_type(self) -> ExternalDataSourceType:
        return ExternalDataSourceType.AVIATIONSTACK

    def get_non_retryable_errors(self) -> dict[str, str | None]:
        return {
            # Bad/blocked key surfaces as an HTTP 401 (missing or invalid access_key) — retrying can
            # never satisfy a credential problem. Match the stable status text and base host.
            "401 Client Error: Unauthorized for url: https://api.aviationstack.com": "Your aviationstack access key is invalid or has been deactivated. Generate a new key in your aviationstack dashboard, then reconnect.",
            "403 Client Error: Forbidden for url: https://api.aviationstack.com": "Your aviationstack plan does not grant access to this data. Upgrade your aviationstack plan or deselect the restricted tables, then reconnect.",
            # aviationstack also returns HTTP 200 with a body-level error envelope; these are raised as
            # AviationstackAPIError with a stable `[code]` token (see aviationstack._fetch_page).
            "aviationstack API error [invalid_access_key]": "Your aviationstack access key is invalid or has been deactivated. Generate a new key in your aviationstack dashboard, then reconnect.",
            "aviationstack API error [missing_access_key]": "No aviationstack access key was supplied. Reconnect the source with a valid access key.",
            "aviationstack API error [inactive_user]": "Your aviationstack account is inactive. Reactivate it in your aviationstack dashboard, then reconnect.",
            "aviationstack API error [function_access_restricted]": "Your aviationstack plan does not grant access to this data. Upgrade your aviationstack plan or deselect the restricted tables, then reconnect.",
            "aviationstack API error [https_access_restricted]": "Your aviationstack plan does not allow HTTPS access. Upgrade your aviationstack plan, then reconnect.",
            "aviationstack API error [usage_limit_reached]": "Your aviationstack monthly request quota has been reached. Upgrade your plan or wait for the quota to reset, then resync.",
        }

    def get_canonical_descriptions(self) -> CanonicalDescriptions:
        from products.warehouse_sources.sources.aviationstack.canonical_descriptions import CANONICAL_DESCRIPTIONS

        return CANONICAL_DESCRIPTIONS

    def get_schemas(
        self,
        config: AviationstackSourceConfig,
        team_id: int,
        with_counts: bool = False,
        names: list[str] | None = None,
        force_refresh: bool = False,
        api_version: str | None = None,
    ) -> list[SourceSchema]:
        # aviationstack has no server-side updated-at cursor, so every table is full refresh only.
        schemas = [
            SourceSchema(
                name=endpoint.name,
                supports_incremental=False,
                supports_append=False,
                incremental_fields=[],
                description=endpoint.description,
            )
            for endpoint in AVIATIONSTACK_ENDPOINTS.values()
        ]

        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: AviationstackSourceConfig,
        team_id: int,
        schema_name: Optional[str] = None,
        api_version: str | None = None,
    ) -> tuple[bool, str | None]:
        endpoint = AVIATIONSTACK_ENDPOINTS.get(schema_name) if schema_name else None
        if endpoint is not None and endpoint.per_airport and not parse_iata_codes(config.airport_iata_codes):
            return False, f"The {endpoint.name} table needs at least one airport IATA code, for example JFK."

        if validate_aviationstack_credentials(config.access_key):
            return True, None

        return False, "Invalid aviationstack access key"

    def get_resumable_source_manager(self, inputs: SourceInputs) -> ResumableSourceManager[AviationstackResumeConfig]:
        return ResumableSourceManager[AviationstackResumeConfig](inputs, AviationstackResumeConfig)

    def source_for_pipeline(
        self,
        config: AviationstackSourceConfig,
        resumable_source_manager: ResumableSourceManager[AviationstackResumeConfig],
        inputs: SourceInputs,
    ) -> SourceResponse:
        return aviationstack_source(
            access_key=config.access_key,
            endpoint=inputs.schema_name,
            team_id=inputs.team_id,
            job_id=inputs.job_id,
            resumable_source_manager=resumable_source_manager,
            db_incremental_field_last_value=None,  # aviationstack has no server-side cursor; full refresh only
            airport_iata_codes=config.airport_iata_codes,
            flights_future_days=config.flights_future_days,
        )

    @property
    def get_source_config(self) -> SourceConfig:
        return SourceConfig(
            name=ExternalDataSourceType.AVIATIONSTACK,
            category=DataWarehouseSourceCategory.ANALYTICS,
            label="Aviationstack",
            releaseStatus=ReleaseStatus.BETA,
            caption="""Enter your aviationstack access key to pull real-time, scheduled, and historical flight data plus aviation reference tables into the PostHog Data warehouse.

You can find your access key in your [aviationstack dashboard](https://aviationstack.com/dashboard).

The `timetable` and `flights_future` tables are per-airport: list the airport IATA codes you want, and those tables sync for those airports only. aviationstack serves future flights from 8 days out, so `flights_future` starts there and covers the number of days you set.

Note: aviationstack pricing is a monthly request quota tied to your plan. Some tables (e.g. historical flights, timetable, and future flights) require a paid plan.""",
            iconPath="/static/services/aviationstack.png",
            docsUrl="https://posthog.com/docs/cdp/sources/aviationstack",
            fields=cast(
                list[FieldType],
                [
                    SourceFieldInputConfig(
                        name="access_key",
                        label="Access key",
                        type=SourceFieldInputConfigType.PASSWORD,
                        required=True,
                        placeholder="",
                        secret=True,
                    ),
                    SourceFieldInputConfig(
                        name="airport_iata_codes",
                        label="Airport IATA codes",
                        type=SourceFieldInputConfigType.TEXT,
                        required=False,
                        placeholder="JFK, DXB",
                        secret=False,
                    ),
                    SourceFieldInputConfig(
                        name="flights_future_days",
                        sync_setting=True,
                        label="Days of future flights to sync",
                        type=SourceFieldInputConfigType.NUMBER,
                        required=False,
                        placeholder="7",
                        secret=False,
                    ),
                ],
            ),
        )