elevenlabs-python / data /src /elevenlabs /core /client_wrapper.py
introvoyz045's picture
Migrated from GitHub
9410704 verified
Raw History Blame Contribute Delete
3.53 kB
# This file was auto-generated by Fern from our API Definition.
import typing
import httpx
from .http_client import AsyncHttpClient, HttpClient
from .logging import LogConfig, Logger
class BaseClientWrapper:
def __init__(
self,
*,
api_key: typing.Optional[str] = None,
headers: typing.Optional[typing.Dict[str, str]] = None,
base_url: str,
timeout: typing.Optional[float] = None,
logging: typing.Optional[typing.Union[LogConfig, Logger]] = None,
):
self._api_key = api_key
self._headers = headers
self._base_url = base_url
self._timeout = timeout
self._logging = logging
def get_headers(self) -> typing.Dict[str, str]:
import platform
headers: typing.Dict[str, str] = {
"User-Agent": "elevenlabs/2.74.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": "elevenlabs",
"X-Fern-SDK-Version": "2.74.1",
**(self.get_custom_headers() or {}),
}
if self._api_key is not None:
headers["xi-api-key"] = self._api_key
return headers
def get_custom_headers(self) -> typing.Optional[typing.Dict[str, str]]:
return self._headers
def get_base_url(self) -> str:
return self._base_url
def get_timeout(self) -> typing.Optional[float]:
return self._timeout
class SyncClientWrapper(BaseClientWrapper):
def __init__(
self,
*,
api_key: typing.Optional[str] = None,
headers: typing.Optional[typing.Dict[str, str]] = None,
base_url: str,
timeout: typing.Optional[float] = None,
logging: typing.Optional[typing.Union[LogConfig, Logger]] = None,
httpx_client: httpx.Client,
):
super().__init__(api_key=api_key, headers=headers, base_url=base_url, timeout=timeout, logging=logging)
self.httpx_client = HttpClient(
httpx_client=httpx_client,
base_headers=self.get_headers,
base_timeout=self.get_timeout,
base_url=self.get_base_url,
logging_config=self._logging,
)
class AsyncClientWrapper(BaseClientWrapper):
def __init__(
self,
*,
api_key: typing.Optional[str] = None,
headers: typing.Optional[typing.Dict[str, str]] = None,
base_url: str,
timeout: typing.Optional[float] = None,
logging: typing.Optional[typing.Union[LogConfig, Logger]] = None,
async_token: typing.Optional[typing.Callable[[], typing.Awaitable[str]]] = None,
httpx_client: httpx.AsyncClient,
):
super().__init__(api_key=api_key, headers=headers, base_url=base_url, timeout=timeout, logging=logging)
self._async_token = async_token
self.httpx_client = AsyncHttpClient(
httpx_client=httpx_client,
base_headers=self.get_headers,
base_timeout=self.get_timeout,
base_url=self.get_base_url,
async_base_headers=self.async_get_headers,
logging_config=self._logging,
)
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