1
0
mirror of https://github.com/ijaric/voice_assistant.git synced 2025-05-24 14:33:26 +00:00

feat: [#38] add http client

This commit is contained in:
ksieuk 2023-10-04 23:38:52 +03:00
parent 306ae27171
commit 4f29e4de73
3 changed files with 52 additions and 1 deletions

View File

@ -57,6 +57,17 @@ class Application:
logger.info("Initializing clients") logger.info("Initializing clients")
http_yandex_tts_client = clients.AsyncHttpClient(
base_url="yandex", # todo add yandex api url from settings
proxy_settings=settings.proxy,
)
disposable_resources.append(
DisposableResource(
name="http_client yandex",
dispose_callback=http_yandex_tts_client.close(),
)
)
# Repositories # Repositories
logger.info("Initializing repositories") logger.info("Initializing repositories")

View File

@ -1,3 +1,7 @@
from .http_client import AsyncHttpClient
from .postgres import AsyncPostgresClient from .postgres import AsyncPostgresClient
__all__ = ["AsyncPostgresClient"] __all__ = [
"AsyncHttpClient",
"AsyncPostgresClient",
]

View File

@ -0,0 +1,36 @@
import httpx
import lib.app.split_settings as app_split_settings
class AsyncHttpClient:
def __init__(
self,
proxy_settings: app_split_settings.ProxySettings,
base_url: str | None = None,
**client_params: dict[str, str],
) -> None:
self.base_url = base_url if base_url else ""
self.proxy_settings = proxy_settings
self.proxies = self._get_proxies_from_settings()
self.client_params = client_params
self.client = self._get_client()
def _get_proxies_from_settings(self) -> dict[str, str] | None:
if not self.proxy_settings.enable:
return None
proxies = {"all://": self.proxy_settings.dsn}
return proxies
def _get_client(self):
return httpx.AsyncClient(
base_url=self.base_url,
proxies=self.proxies, # type: ignore
**self.client_params,
)
async def close(self):
if not self.client:
return
await self.client.aclose()