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

from __future__ import annotations

import os
import typing

import httpx
from .core.api_error import ApiError
from .core.client_wrapper import AsyncClientWrapper, SyncClientWrapper
from .environment import DeepgramClientEnvironment

if typing.TYPE_CHECKING:
    from .agent.client import AgentClient, AsyncAgentClient
    from .auth.client import AsyncAuthClient, AuthClient
    from .listen.client import AsyncListenClient, ListenClient
    from .manage.client import AsyncManageClient, ManageClient
    from .read.client import AsyncReadClient, ReadClient
    from .self_hosted.client import AsyncSelfHostedClient, SelfHostedClient
    from .speak.client import AsyncSpeakClient, SpeakClient


class BaseClient:
    """
    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
    ----------
    environment : DeepgramClientEnvironment
        The environment to use for requests from the client. from .environment import DeepgramClientEnvironment



        Defaults to DeepgramClientEnvironment.PRODUCTION



    api_key : typing.Optional[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.

    Examples
    --------
    from deepgram import DeepgramClient

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

    def __init__(
        self,
        *,
        environment: DeepgramClientEnvironment = DeepgramClientEnvironment.PRODUCTION,
        api_key: typing.Optional[str] = os.getenv("DEEPGRAM_API_KEY"),
        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,
    ):
        _defaulted_timeout = (
            timeout if timeout is not None else 60 if httpx_client is None else httpx_client.timeout.read
        )
        if api_key is None:
            raise ApiError(
                body="The client must be instantiated be either passing in api_key or setting DEEPGRAM_API_KEY"
            )
        self._client_wrapper = SyncClientWrapper(
            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,
        )
        self._agent: typing.Optional[AgentClient] = None
        self._auth: typing.Optional[AuthClient] = None
        self._listen: typing.Optional[ListenClient] = None
        self._manage: typing.Optional[ManageClient] = None
        self._read: typing.Optional[ReadClient] = None
        self._self_hosted: typing.Optional[SelfHostedClient] = None
        self._speak: typing.Optional[SpeakClient] = None

    @property
    def agent(self):
        if self._agent is None:
            from .agent.client import AgentClient  # noqa: E402

            self._agent = AgentClient(client_wrapper=self._client_wrapper)
        return self._agent

    @property
    def auth(self):
        if self._auth is None:
            from .auth.client import AuthClient  # noqa: E402

            self._auth = AuthClient(client_wrapper=self._client_wrapper)
        return self._auth

    @property
    def listen(self):
        if self._listen is None:
            from .listen.client import ListenClient  # noqa: E402

            self._listen = ListenClient(client_wrapper=self._client_wrapper)
        return self._listen

    @property
    def manage(self):
        if self._manage is None:
            from .manage.client import ManageClient  # noqa: E402

            self._manage = ManageClient(client_wrapper=self._client_wrapper)
        return self._manage

    @property
    def read(self):
        if self._read is None:
            from .read.client import ReadClient  # noqa: E402

            self._read = ReadClient(client_wrapper=self._client_wrapper)
        return self._read

    @property
    def self_hosted(self):
        if self._self_hosted is None:
            from .self_hosted.client import SelfHostedClient  # noqa: E402

            self._self_hosted = SelfHostedClient(client_wrapper=self._client_wrapper)
        return self._self_hosted

    @property
    def speak(self):
        if self._speak is None:
            from .speak.client import SpeakClient  # noqa: E402

            self._speak = SpeakClient(client_wrapper=self._client_wrapper)
        return self._speak


class AsyncBaseClient:
    """
    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
    ----------
    environment : DeepgramClientEnvironment
        The environment to use for requests from the client. from .environment import DeepgramClientEnvironment



        Defaults to DeepgramClientEnvironment.PRODUCTION



    api_key : typing.Optional[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.

    Examples
    --------
    from deepgram import AsyncDeepgramClient

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

    def __init__(
        self,
        *,
        environment: DeepgramClientEnvironment = DeepgramClientEnvironment.PRODUCTION,
        api_key: typing.Optional[str] = os.getenv("DEEPGRAM_API_KEY"),
        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,
    ):
        _defaulted_timeout = (
            timeout if timeout is not None else 60 if httpx_client is None else httpx_client.timeout.read
        )
        if api_key is None:
            raise ApiError(
                body="The client must be instantiated be either passing in api_key or setting DEEPGRAM_API_KEY"
            )
        self._client_wrapper = AsyncClientWrapper(
            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,
        )
        self._agent: typing.Optional[AsyncAgentClient] = None
        self._auth: typing.Optional[AsyncAuthClient] = None
        self._listen: typing.Optional[AsyncListenClient] = None
        self._manage: typing.Optional[AsyncManageClient] = None
        self._read: typing.Optional[AsyncReadClient] = None
        self._self_hosted: typing.Optional[AsyncSelfHostedClient] = None
        self._speak: typing.Optional[AsyncSpeakClient] = None

    @property
    def agent(self):
        if self._agent is None:
            from .agent.client import AsyncAgentClient  # noqa: E402

            self._agent = AsyncAgentClient(client_wrapper=self._client_wrapper)
        return self._agent

    @property
    def auth(self):
        if self._auth is None:
            from .auth.client import AsyncAuthClient  # noqa: E402

            self._auth = AsyncAuthClient(client_wrapper=self._client_wrapper)
        return self._auth

    @property
    def listen(self):
        if self._listen is None:
            from .listen.client import AsyncListenClient  # noqa: E402

            self._listen = AsyncListenClient(client_wrapper=self._client_wrapper)
        return self._listen

    @property
    def manage(self):
        if self._manage is None:
            from .manage.client import AsyncManageClient  # noqa: E402

            self._manage = AsyncManageClient(client_wrapper=self._client_wrapper)
        return self._manage

    @property
    def read(self):
        if self._read is None:
            from .read.client import AsyncReadClient  # noqa: E402

            self._read = AsyncReadClient(client_wrapper=self._client_wrapper)
        return self._read

    @property
    def self_hosted(self):
        if self._self_hosted is None:
            from .self_hosted.client import AsyncSelfHostedClient  # noqa: E402

            self._self_hosted = AsyncSelfHostedClient(client_wrapper=self._client_wrapper)
        return self._self_hosted

    @property
    def speak(self):
        if self._speak is None:
            from .speak.client import AsyncSpeakClient  # noqa: E402

            self._speak = AsyncSpeakClient(client_wrapper=self._client_wrapper)
        return self._speak
