# This file was auto-generated by Fern from our API Definition.

from __future__ import annotations

import typing

import httpx
from .core.client_wrapper import AsyncClientWrapper, SyncClientWrapper
from .core.logging import LogConfig, Logger
from .environment import TradewatchApiEnvironment

if typing.TYPE_CHECKING:
    from .account.client import AccountClient, AsyncAccountClient
    from .commodities.client import AsyncCommoditiesClient, CommoditiesClient
    from .crypto.client import AsyncCryptoClient, CryptoClient
    from .currencies.client import AsyncCurrenciesClient, CurrenciesClient
    from .indices.client import AsyncIndicesClient, IndicesClient
    from .stocks.client import AsyncStocksClient, StocksClient


class TradewatchApi:
    """
    Use this class to access the different functions within the SDK. You can instantiate any number of clients with different configuration that will propagate to these functions.

    Parameters
    ----------
    base_url : typing.Optional[str]
        The base url to use for requests from the client.

    environment : TradewatchApiEnvironment
        The environment to use for requests from the client. from .environment import TradewatchApiEnvironment



        Defaults to TradewatchApiEnvironment.DEFAULT



    api_key : str
    headers : typing.Optional[typing.Dict[str, str]]
        Additional headers to send with every request.

    timeout : typing.Optional[float]
        The timeout to be used, in seconds, for requests. By default the timeout is 60 seconds, unless a custom httpx client is used, in which case this default is not enforced.

    follow_redirects : typing.Optional[bool]
        Whether the default httpx client follows redirects or not, this is irrelevant if a custom httpx client is passed in.

    httpx_client : typing.Optional[httpx.Client]
        The httpx client to use for making requests, a preconfigured client is used by default, however this is useful should you want to pass in any custom httpx configuration.

    logging : typing.Optional[typing.Union[LogConfig, Logger]]
        Configure logging for the SDK. Accepts a LogConfig dict with 'level' (debug/info/warn/error), 'logger' (custom logger implementation), and 'silent' (boolean, defaults to True) fields. You can also pass a pre-configured Logger instance.

    Examples
    --------
    from tradewatch import TradewatchApi

    client = TradewatchApi(
        api_key="YOUR_API_KEY",
    )
    """

    def __init__(
        self,
        *,
        base_url: typing.Optional[str] = None,
        environment: TradewatchApiEnvironment = TradewatchApiEnvironment.DEFAULT,
        api_key: str,
        headers: typing.Optional[typing.Dict[str, str]] = None,
        timeout: typing.Optional[float] = None,
        follow_redirects: typing.Optional[bool] = True,
        httpx_client: typing.Optional[httpx.Client] = None,
        logging: typing.Optional[typing.Union[LogConfig, Logger]] = None,
    ):
        _defaulted_timeout = (
            timeout if timeout is not None else 60 if httpx_client is None else httpx_client.timeout.read
        )
        self._client_wrapper = SyncClientWrapper(
            base_url=_get_base_url(base_url=base_url, environment=environment),
            api_key=api_key,
            headers=headers,
            httpx_client=httpx_client
            if httpx_client is not None
            else httpx.Client(timeout=_defaulted_timeout, follow_redirects=follow_redirects)
            if follow_redirects is not None
            else httpx.Client(timeout=_defaulted_timeout),
            timeout=_defaulted_timeout,
            logging=logging,
        )
        self._account: typing.Optional[AccountClient] = None
        self._currencies: typing.Optional[CurrenciesClient] = None
        self._crypto: typing.Optional[CryptoClient] = None
        self._indices: typing.Optional[IndicesClient] = None
        self._stocks: typing.Optional[StocksClient] = None
        self._commodities: typing.Optional[CommoditiesClient] = None

    @property
    def account(self):
        if self._account is None:
            from .account.client import AccountClient  # noqa: E402

            self._account = AccountClient(client_wrapper=self._client_wrapper)
        return self._account

    @property
    def currencies(self):
        if self._currencies is None:
            from .currencies.client import CurrenciesClient  # noqa: E402

            self._currencies = CurrenciesClient(client_wrapper=self._client_wrapper)
        return self._currencies

    @property
    def crypto(self):
        if self._crypto is None:
            from .crypto.client import CryptoClient  # noqa: E402

            self._crypto = CryptoClient(client_wrapper=self._client_wrapper)
        return self._crypto

    @property
    def indices(self):
        if self._indices is None:
            from .indices.client import IndicesClient  # noqa: E402

            self._indices = IndicesClient(client_wrapper=self._client_wrapper)
        return self._indices

    @property
    def stocks(self):
        if self._stocks is None:
            from .stocks.client import StocksClient  # noqa: E402

            self._stocks = StocksClient(client_wrapper=self._client_wrapper)
        return self._stocks

    @property
    def commodities(self):
        if self._commodities is None:
            from .commodities.client import CommoditiesClient  # noqa: E402

            self._commodities = CommoditiesClient(client_wrapper=self._client_wrapper)
        return self._commodities


class AsyncTradewatchApi:
    """
    Use this class to access the different functions within the SDK. You can instantiate any number of clients with different configuration that will propagate to these functions.

    Parameters
    ----------
    base_url : typing.Optional[str]
        The base url to use for requests from the client.

    environment : TradewatchApiEnvironment
        The environment to use for requests from the client. from .environment import TradewatchApiEnvironment



        Defaults to TradewatchApiEnvironment.DEFAULT



    api_key : str
    headers : typing.Optional[typing.Dict[str, str]]
        Additional headers to send with every request.

    timeout : typing.Optional[float]
        The timeout to be used, in seconds, for requests. By default the timeout is 60 seconds, unless a custom httpx client is used, in which case this default is not enforced.

    follow_redirects : typing.Optional[bool]
        Whether the default httpx client follows redirects or not, this is irrelevant if a custom httpx client is passed in.

    httpx_client : typing.Optional[httpx.AsyncClient]
        The httpx client to use for making requests, a preconfigured client is used by default, however this is useful should you want to pass in any custom httpx configuration.

    logging : typing.Optional[typing.Union[LogConfig, Logger]]
        Configure logging for the SDK. Accepts a LogConfig dict with 'level' (debug/info/warn/error), 'logger' (custom logger implementation), and 'silent' (boolean, defaults to True) fields. You can also pass a pre-configured Logger instance.

    Examples
    --------
    from tradewatch import AsyncTradewatchApi

    client = AsyncTradewatchApi(
        api_key="YOUR_API_KEY",
    )
    """

    def __init__(
        self,
        *,
        base_url: typing.Optional[str] = None,
        environment: TradewatchApiEnvironment = TradewatchApiEnvironment.DEFAULT,
        api_key: str,
        headers: typing.Optional[typing.Dict[str, str]] = None,
        timeout: typing.Optional[float] = None,
        follow_redirects: typing.Optional[bool] = True,
        httpx_client: typing.Optional[httpx.AsyncClient] = None,
        logging: typing.Optional[typing.Union[LogConfig, Logger]] = None,
    ):
        _defaulted_timeout = (
            timeout if timeout is not None else 60 if httpx_client is None else httpx_client.timeout.read
        )
        self._client_wrapper = AsyncClientWrapper(
            base_url=_get_base_url(base_url=base_url, environment=environment),
            api_key=api_key,
            headers=headers,
            httpx_client=httpx_client
            if httpx_client is not None
            else httpx.AsyncClient(timeout=_defaulted_timeout, follow_redirects=follow_redirects)
            if follow_redirects is not None
            else httpx.AsyncClient(timeout=_defaulted_timeout),
            timeout=_defaulted_timeout,
            logging=logging,
        )
        self._account: typing.Optional[AsyncAccountClient] = None
        self._currencies: typing.Optional[AsyncCurrenciesClient] = None
        self._crypto: typing.Optional[AsyncCryptoClient] = None
        self._indices: typing.Optional[AsyncIndicesClient] = None
        self._stocks: typing.Optional[AsyncStocksClient] = None
        self._commodities: typing.Optional[AsyncCommoditiesClient] = None

    @property
    def account(self):
        if self._account is None:
            from .account.client import AsyncAccountClient  # noqa: E402

            self._account = AsyncAccountClient(client_wrapper=self._client_wrapper)
        return self._account

    @property
    def currencies(self):
        if self._currencies is None:
            from .currencies.client import AsyncCurrenciesClient  # noqa: E402

            self._currencies = AsyncCurrenciesClient(client_wrapper=self._client_wrapper)
        return self._currencies

    @property
    def crypto(self):
        if self._crypto is None:
            from .crypto.client import AsyncCryptoClient  # noqa: E402

            self._crypto = AsyncCryptoClient(client_wrapper=self._client_wrapper)
        return self._crypto

    @property
    def indices(self):
        if self._indices is None:
            from .indices.client import AsyncIndicesClient  # noqa: E402

            self._indices = AsyncIndicesClient(client_wrapper=self._client_wrapper)
        return self._indices

    @property
    def stocks(self):
        if self._stocks is None:
            from .stocks.client import AsyncStocksClient  # noqa: E402

            self._stocks = AsyncStocksClient(client_wrapper=self._client_wrapper)
        return self._stocks

    @property
    def commodities(self):
        if self._commodities is None:
            from .commodities.client import AsyncCommoditiesClient  # noqa: E402

            self._commodities = AsyncCommoditiesClient(client_wrapper=self._client_wrapper)
        return self._commodities


def _get_base_url(*, base_url: typing.Optional[str] = None, environment: TradewatchApiEnvironment) -> str:
    if base_url is not None:
        return base_url
    elif environment is not None:
        return environment.value
    else:
        raise Exception("Please pass in either base_url or environment to construct the client")
