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.1 KB
Raw Blame
from typing import cast

from products.warehouse_sources.sources.mono._config import MonoSourceConfig
from products.warehouse_sources.sources.mono.canonical_descriptions import CANONICAL_DESCRIPTIONS
from products.warehouse_sources.sources.mono.mono import (
    MonoResumeConfig,
    mono_source,
    validate_credentials as validate_mono_credentials,
)
from products.warehouse_sources.sources.mono.settings import AUTH_ERROR, ENDPOINTS, INCREMENTAL_FIELDS, PERMISSION_ERROR
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 MonoSource(ResumableSource[MonoSourceConfig, MonoResumeConfig]):
    codebase_detection = detect(
        script_hosts=("connect.withmono.com",),
        npm=("@mono.co/connect.js", "@mono.co/connect-react-native", "mono-node"),
        pub=("mono_connect", "mono_prove"),
    )

    lists_tables_without_credentials = True
    supported_versions = ("v2",)
    default_version = "v2"
    api_docs_url = "https://docs.mono.co/docs/changelog"

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

    def get_non_retryable_errors(self) -> dict[str, str | None]:
        return {
            "401 Client Error": AUTH_ERROR,
            "403 Client Error": PERMISSION_ERROR,
            "Unauthorized request.": AUTH_ERROR,
            "Invalid API key": AUTH_ERROR,
        }

    def get_canonical_descriptions(self) -> CanonicalDescriptions:
        return CANONICAL_DESCRIPTIONS

    def get_schemas(
        self,
        config: MonoSourceConfig,
        team_id: int,
        with_counts: bool = False,
        names: list[str] | None = None,
        force_refresh: bool = False,
        api_version: str | None = None,
    ) -> list[SourceSchema]:
        return build_endpoint_schemas(
            ENDPOINTS, INCREMENTAL_FIELDS, names, merge_only={"transactions", "payments", "mandate_debits"}
        )

    def validate_credentials(
        self,
        config: MonoSourceConfig,
        team_id: int,
        schema_name: str | None = None,
        api_version: str | None = None,
    ) -> tuple[bool, str | None]:
        return validate_mono_credentials(config, self.resolve_api_version(api_version), team_id)

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

    def source_for_pipeline(
        self,
        config: MonoSourceConfig,
        resumable_source_manager: ResumableSourceManager[MonoResumeConfig],
        inputs: SourceInputs,
    ) -> SourceResponse:
        return mono_source(
            config=config,
            endpoint=inputs.schema_name,
            team_id=inputs.team_id,
            job_id=inputs.job_id,
            api_version=self.resolve_api_version(inputs.api_version),
            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,
        )

    @property
    def get_source_config(self) -> SourceConfig:
        return SourceConfig(
            name=ExternalDataSourceType.MONO,
            category=DataWarehouseSourceCategory.FINANCE___ACCOUNTING,
            label="Mono",
            iconPath="/static/services/mono.png",
            keywords=["open banking", "transactions", "fintech", "africa"],
            caption=(
                "Copy the secret API key from your app in the [Mono dashboard](https://app.mono.co). "
                "Customers must link their bank accounts through Mono before you can import transactions. "
                "Choose the earliest transaction date to import. Imports use data already stored by Mono. "
                "Use a full refresh to import older transactions from newly linked accounts."
            ),
            fields=cast(
                list[FieldType],
                [
                    SourceFieldInputConfig(
                        name="api_key",
                        label="Secret API key",
                        type=SourceFieldInputConfigType.PASSWORD,
                        required=True,
                        placeholder="",
                        secret=True,
                    ),
                    SourceFieldInputConfig(
                        name="start_date",
                        sync_setting=True,
                        label="Transaction start date",
                        type=SourceFieldInputConfigType.TEXT,
                        required=True,
                        placeholder="YYYY-MM-DD",
                        secret=False,
                    ),
                ],
            ),
            releaseStatus=ReleaseStatus.BETA,
        )