mirror of
				https://github.com/ijaric/voice_assistant.git
				synced 2025-10-30 23:43:25 +00:00 
			
		
		
		
	feat: [#38] add http client
This commit is contained in:
		
							parent
							
								
									306ae27171
								
							
						
					
					
						commit
						4f29e4de73
					
				|  | @ -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") | ||||||
|  |  | ||||||
|  | @ -1,3 +1,7 @@ | ||||||
|  | from .http_client import AsyncHttpClient | ||||||
| from .postgres import AsyncPostgresClient | from .postgres import AsyncPostgresClient | ||||||
| 
 | 
 | ||||||
| __all__ = ["AsyncPostgresClient"] | __all__ = [ | ||||||
|  |     "AsyncHttpClient", | ||||||
|  |     "AsyncPostgresClient", | ||||||
|  | ] | ||||||
|  |  | ||||||
							
								
								
									
										36
									
								
								src/assistant/lib/clients/http_client.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								src/assistant/lib/clients/http_client.py
									
									
									
									
									
										Normal 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() | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user