from __future__ import annotations

"""
API DataSource 模块

提供基于 HTTP API 的数据源实现。
"""
from typing import TYPE_CHECKING

from ..type_registry import register_datasource_type

from .client import ApiClient
from .datasource import ApiDataSource
from .handler import BaseApiHandler

if TYPE_CHECKING:
    from ...config.datasource_config import ApiDataSourceConfig


def build_api_datasource(config: ApiDataSourceConfig, endpoint_name: str, **_: object):
    del endpoint_name
    return ApiDataSource(
        base_url=config.api_base_url,
        uid=config.api_uid,
        secret=config.api_secret,
        debug=config.api_debug,
        poll_max_retries=config.poll_max_retries,
        poll_interval=config.poll_interval,
        rate_limit_max_requests=config.api_rate_limit_max_requests,
        rate_limit_window_seconds=config.api_rate_limit_window_seconds,
    )


def register(*, replace: bool = False) -> None:
    from ...config.datasource_config import ApiDataSourceConfig

    register_datasource_type(
        "api",
        config_model=ApiDataSourceConfig,
        factory=build_api_datasource,
        replace=replace,
    )

__all__ = [
    "ApiClient",
    "ApiDataSource",
    "BaseApiHandler",
    "build_api_datasource",
    "register",
]
