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.

5.8 KB
Raw Blame
from typing import Optional, cast

from products.warehouse_sources.sources.gridly._config import GridlySourceConfig
from products.warehouse_sources.sources.gridly.gridly import (
    GridlyResumeConfig,
    gridly_source,
    validate_credentials as validate_gridly_credentials,
)
from products.warehouse_sources.sources.gridly.settings import ENDPOINTS, GRIDLY_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 GridlySource(ResumableSource[GridlySourceConfig, GridlyResumeConfig]):
    codebase_detection = detect(env=("^GRIDLY_",))

    lists_tables_without_credentials = True  # static endpoint catalog — safe for public docs
    api_docs_url = "https://www.gridly.com/docs/api/"

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

    @property
    def get_source_config(self) -> SourceConfig:
        return SourceConfig(
            name=ExternalDataSourceType.GRIDLY,
            category=DataWarehouseSourceCategory.PRODUCTIVITY,
            label="Gridly",
            releaseStatus=ReleaseStatus.BETA,
            caption="""Enter your Gridly API key and a View ID to pull that view's records into the PostHog Data warehouse.

Create an API key in your Gridly company settings under **Settings → API keys** (Owner or Admin access is required). Use a **Full Access** or **Read-only** key.

Find the **View ID** in Gridly by opening your grid, selecting a view, and opening the **API** panel — it looks like `v1v9jwwk1lwnkz`. For the default Master branch this is the same as the Grid ID.
""",
            iconPath="/static/services/gridly.png",
            docsUrl="https://posthog.com/docs/cdp/sources/gridly",
            fields=cast(
                list[FieldType],
                [
                    SourceFieldInputConfig(
                        name="api_key",
                        label="API key",
                        type=SourceFieldInputConfigType.PASSWORD,
                        required=True,
                        placeholder="",
                        secret=True,
                    ),
                    SourceFieldInputConfig(
                        name="view_id",
                        label="View ID",
                        type=SourceFieldInputConfigType.TEXT,
                        required=True,
                        placeholder="v1v9jwwk1lwnkz",
                        secret=False,
                    ),
                ],
            ),
        )

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

        return CANONICAL_DESCRIPTIONS

    def get_non_retryable_errors(self) -> dict[str, str | None]:
        return {
            # An invalid or insufficiently-scoped API key surfaces as an HTTPError when a page fetch
            # calls `raise_for_status()`. Retrying can never satisfy a credential problem, so stop the
            # sync. Match the stable status text and base host, not the per-request path/query.
            "401 Client Error: Unauthorized for url: https://api.gridly.com": "Your Gridly API key is invalid or has been revoked. Create a new API key in your Gridly company settings, then reconnect.",
            "403 Client Error: Forbidden for url: https://api.gridly.com": "Your Gridly API key can't access this view or the projects, databases, and grids this table reads. Grant the key access (or use a Full Access key), then reconnect.",
        }

    def get_schemas(
        self,
        config: GridlySourceConfig,
        team_id: int,
        with_counts: bool = False,
        names: list[str] | None = None,
        force_refresh: bool = False,
        api_version: str | None = None,
    ) -> list[SourceSchema]:
        # Gridly exposes no server-side timestamp filter, so every table is full refresh only.
        schemas = [
            SourceSchema(
                name=endpoint,
                supports_incremental=False,
                supports_append=False,
                incremental_fields=[],
                detected_primary_keys=GRIDLY_ENDPOINTS[endpoint].primary_keys,
                should_sync_default=GRIDLY_ENDPOINTS[endpoint].should_sync_default,
                description=GRIDLY_ENDPOINTS[endpoint].description,
            )
            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: GridlySourceConfig,
        team_id: int,
        schema_name: Optional[str] = None,
        api_version: str | None = None,
    ) -> tuple[bool, str | None]:
        return validate_gridly_credentials(config.api_key, config.view_id)

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

    def source_for_pipeline(
        self,
        config: GridlySourceConfig,
        resumable_source_manager: ResumableSourceManager[GridlyResumeConfig],
        inputs: SourceInputs,
    ) -> SourceResponse:
        return gridly_source(
            api_key=config.api_key,
            view_id=config.view_id,
            endpoint=inputs.schema_name,
            logger=inputs.logger,
            resumable_source_manager=resumable_source_manager,
        )