"""{{ "Asynchronous Meraki" if is_async else "Meraki" }} dashboard API SDK.

This file is automatically @generated from the Meraki API specification.
Do not edit this file manually.
"""
{% set api_path = "aio._api" if is_async else "_api" %}
{% set session_path = "aio._session" if is_async else "_session" %}

import os
import sys
{% if is_async %}
from types import TracebackType
from typing import Self
{% endif %}

from meraki_client import types
{% for module in modules %}
from meraki_client.{{ api_path }}.{{ module.snake_name }} import {{ module.class_name }}
{% endfor %}
from meraki_client.{{ session_path }} import {{ "AsyncPaginatedResponse" if is_async else "PaginatedResponse" }}, Session
from meraki_client._api.batch import Batch
from meraki_client._common import BaseURL

if sys.version_info < {{ min_python_version }}:  # noqa: UP036
    raise RuntimeError(
        "Python {{ min_python_version[0] }}.{{ min_python_version[1] }} or higher is required. "
        f"You are using Python {sys.version_info.major}.{sys.version_info.minor}."
    )


__all__ = [
    {% if is_async %}
    "AsyncMerakiClient",
    "AsyncPaginatedResponse",
    {% endif %}
    "BaseURL",
    {% if not is_async %}
    "MerakiClient",
    "PaginatedResponse",
    {% endif %}
    "types",
]
__version__ = "{{ version }}"
__api_version__ = "{{ api_version }}"


class {{ "Async" if is_async else "" }}MerakiClient:
    """{{ "Asynchronous client" if is_async else "Client" }} class for the Meraki dashboard API.

    Args:
        api_key: API key generated in dashboard; can also be set as an environment variable
            `MERAKI_DASHBOARD_API_KEY`.
        base_url: Base URL preceding all endpoint resources.
        single_request_timeout: Maximum number of seconds for each API call. Defaults to 30.
        total_request_timeout: Total time in seconds to wait for a request to complete including
            retries. Defaults to 60.
        certificate_path: Path for TLS/SSL certificate verification if behind local proxy.
        requests_proxy: Proxy server and port, if needed, for HTTPS.
        wait_on_rate_limit: Retry if 429 rate limit error encountered? Defaults to True.
        maximum_retries: Retry up to this many times when encountering 429s or other server-side
            errors. Defaults to 2.
        caller: Optional identifier for API usage tracking; can also be set as an environment
            variable `MERAKI_PYTHON_SDK_CALLER`.
{% if is_async %}
        maximum_concurrent_requests: Number of concurrent API requests. Defaults to 8.
{% endif %}

    """

    def __init__(
        self,
        *,
        api_key: str | None = None,
        base_url: BaseURL = BaseURL.DEFAULT,
        single_request_timeout: int = 30,
        total_request_timeout: int = 60,
        wait_on_rate_limit: bool = True,
        maximum_retries: int = 2,
        certificate_path: str | None = None,
        requests_proxy: str | None = None,
        caller: str | None = None,
{% if is_async %}
        maximum_concurrent_requests: int = 8,
{% endif %}
    ) -> None:
        # Check that the API key is defined
        api_key = api_key or os.environ.get("MERAKI_DASHBOARD_API_KEY")
        if not api_key:
            raise ValueError(
                "API key needs to be defined. Give the API key as an argument "
                "or set the MERAKI_DASHBOARD_API_KEY environment variable."
            )
        caller = caller or os.environ.get("MERAKI_PYTHON_SDK_CALLER")

        # Creates the API session
        self._session = Session(
            api_key=api_key,
            base_url=base_url,
            single_request_timeout=single_request_timeout,
            total_request_timeout=total_request_timeout,
            certificate_path=certificate_path,
            proxy=requests_proxy,
            wait_on_rate_limit=wait_on_rate_limit,
            maximum_retries=maximum_retries,
            caller=caller,
            version=__version__,
{% if is_async %}
            maximum_concurrent_requests=maximum_concurrent_requests,
{% endif %}
        )

        # API endpoints by section
{% for module in modules %}
        self.{{ module.attr_name }} = {{ module.class_name }}(self._session)
{% endfor %}

        # Batch definitions
        self.batch = Batch()
{% if is_async %}

    async def __aenter__(self) -> Self:
        return self

    async def __aexit__(
        self,
        exc_type: type[BaseException] | None,
        exc: BaseException | None,
        tb: TracebackType | None,
    ) -> None:
        await self._session.close()
{% endif %}
