Refactor proxy configuration into a separate function

This commit is contained in:
Yannik Korzikowski 2026-03-25 19:13:55 +08:00 committed by GitHub
parent 9b23767784
commit 2e42b59ffd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -51,6 +51,24 @@ try:
except Exception:
version = "0.0.0"
def create_proxy_mount_config() -> Optional[dict]:
"""
Creates the configuration for using httpx with http/https proxy.
Returns the dict with proxy options if HTTP_PROXY or/and HTTPS_PROXY environment variable is set. Using http_proxy/https_proxy as fallback.
Returns empty dict if unset
"""
proxy_mounts = {}
http_proxy = os.getenv("HTTP_PROXY", os.getenv("http_proxy", ""))
if http_proxy:
proxy_mounts["http://"] = httpx.HTTPTransport(proxy=http_proxy)
https_proxy = os.getenv("HTTPS_PROXY", os.getenv("https_proxy", ""))
if https_proxy:
proxy_mounts["https://"] = httpx.HTTPTransport(proxy=https_proxy)
return proxy_mounts
def get_default_headers() -> dict:
"""
@ -389,7 +407,7 @@ class AsyncHTTPHandler:
# Get default headers (User-Agent, overridable via LITELLM_USER_AGENT)
default_headers = get_default_headers()
proxy_mounts = self._create_proxy_mount_config()
proxy_mounts = create_proxy_mount_config()
return httpx.AsyncClient(
mounts=proxy_mounts or None,
@ -942,7 +960,7 @@ class HTTPHandler:
if client is None:
transport = self._create_sync_transport()
proxy_mounts = self._create_proxy_mount_config()
proxy_mounts = create_proxy_mount_config()
# Create a client with a connection pool
self.client = httpx.Client(
@ -1193,24 +1211,6 @@ class HTTPHandler:
self.close()
except Exception:
pass
def _create_proxy_mount_config(self) -> Optional[dict]:
"""
Creates the configuration for using httpx with http/https proxy.
Returns the dict with proxy options if HTTP_PROXY or/and HTTPS_PROXY environment variable is set. Using http_proxy/https_proxy as fallback.
Returns empty dict if unset
"""
proxy_mounts = {}
http_proxy = os.getenv("HTTP_PROXY", os.getenv("http_proxy", ""))
if http_proxy:
proxy_mounts["http://"] = httpx.HTTPTransport(proxy=http_proxy)
https_proxy = os.getenv("HTTPS_PROXY", os.getenv("https_proxy", ""))
if https_proxy:
proxy_mounts["https://"] = httpx.HTTPTransport(proxy=https_proxy)
return proxy_mounts
def _create_sync_transport(self) -> Optional[HTTPTransport]:
"""