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

import typing

import httpx
from ..environment import DeepgramClientEnvironment
from .http_client import AsyncHttpClient, HttpClient


class BaseClientWrapper:
    def __init__(
        self,
        *,
        api_key: str,
        headers: typing.Optional[typing.Dict[str, str]] = None,
        environment: DeepgramClientEnvironment,
        timeout: typing.Optional[float] = None,
    ):
        self.api_key = api_key
        self._headers = headers
        self._environment = environment
        self._timeout = timeout

    def get_headers(self) -> typing.Dict[str, str]:
        import platform

        headers: typing.Dict[str, str] = {
            "User-Agent": "deepgram-sdk/6.0.1",
            "X-Fern-Language": "Python",
            "X-Fern-Runtime": f"python/{platform.python_version()}",
            "X-Fern-Platform": f"{platform.system().lower()}/{platform.release()}",
            "X-Fern-SDK-Name": "deepgram-sdk",
            "X-Fern-SDK-Version": "6.0.1",
            **(self.get_custom_headers() or {}),
        }
        headers["Authorization"] = f"Token {self.api_key}"
        return headers

    def get_custom_headers(self) -> typing.Optional[typing.Dict[str, str]]:
        return self._headers

    def get_environment(self) -> DeepgramClientEnvironment:
        return self._environment

    def get_timeout(self) -> typing.Optional[float]:
        return self._timeout


class SyncClientWrapper(BaseClientWrapper):
    def __init__(
        self,
        *,
        api_key: str,
        headers: typing.Optional[typing.Dict[str, str]] = None,
        environment: DeepgramClientEnvironment,
        timeout: typing.Optional[float] = None,
        httpx_client: httpx.Client,
    ):
        super().__init__(api_key=api_key, headers=headers, environment=environment, timeout=timeout)
        self.httpx_client = HttpClient(
            httpx_client=httpx_client, base_headers=self.get_headers, base_timeout=self.get_timeout
        )


class AsyncClientWrapper(BaseClientWrapper):
    def __init__(
        self,
        *,
        api_key: str,
        headers: typing.Optional[typing.Dict[str, str]] = None,
        environment: DeepgramClientEnvironment,
        timeout: typing.Optional[float] = None,
        async_token: typing.Optional[typing.Callable[[], typing.Awaitable[str]]] = None,
        httpx_client: httpx.AsyncClient,
    ):
        super().__init__(api_key=api_key, headers=headers, environment=environment, timeout=timeout)
        self._async_token = async_token
        self.httpx_client = AsyncHttpClient(
            httpx_client=httpx_client,
            base_headers=self.get_headers,
            base_timeout=self.get_timeout,
            async_base_headers=self.async_get_headers,
        )

    async def async_get_headers(self) -> typing.Dict[str, str]:
        headers = self.get_headers()
        if self._async_token is not None:
            token = await self._async_token()
            headers["Authorization"] = f"Bearer {token}"
        return headers
