diff --git a/app/common/http_client.py b/app/common/http_client.py index e721b3c..3edbba6 100644 --- a/app/common/http_client.py +++ b/app/common/http_client.py @@ -7,15 +7,23 @@ logger = getLogger(__name__) -async_proxy_mounts = { - "http://": httpx.AsyncHTTPTransport(proxy=config.http_proxy), - "https://": httpx.AsyncHTTPTransport(proxy=config.http_proxy) -} if config.http_proxy else {} - -sync_proxy_mounts = { - "http://": httpx.HTTPTransport(proxy=config.http_proxy), - "https://": httpx.HTTPTransport(proxy=config.http_proxy) -} if config.http_proxy else {} +async_proxy_mounts = ( + { + "http://": httpx.AsyncHTTPTransport(proxy=str(config.http_proxy)), + "https://": httpx.AsyncHTTPTransport(proxy=str(config.http_proxy)), + } + if config.http_proxy + else {} +) + +sync_proxy_mounts = ( + { + "http://": httpx.HTTPTransport(proxy=str(config.http_proxy)), + "https://": httpx.HTTPTransport(proxy=str(config.http_proxy)), + } + if config.http_proxy + else {} +) async def async_hook_request_tracing(request): @@ -42,7 +50,7 @@ def create_async_client(request_timeout: int = 30) -> httpx.AsyncClient: """ client_kwargs = { "timeout": request_timeout, - "event_hooks": {"request": [async_hook_request_tracing]} + "event_hooks": {"request": [async_hook_request_tracing]}, } if config.http_proxy: @@ -63,7 +71,7 @@ def create_client(request_timeout: int = 30) -> httpx.Client: """ client_kwargs = { "timeout": request_timeout, - "event_hooks": {"request": [hook_request_tracing]} + "event_hooks": {"request": [hook_request_tracing]}, } if config.http_proxy: diff --git a/app/common/test_http_client.py b/app/common/test_http_client.py index 8fe6baa..c8a5a43 100644 --- a/app/common/test_http_client.py +++ b/app/common/test_http_client.py @@ -27,3 +27,27 @@ def test_trace_id_set(): ) resp = client.get("http://localhost:1234/test") assert resp.text == "trace-id-value" + + +def test_create_client_with_proxy(monkeypatch): + import importlib + + from pydantic import HttpUrl + + # Set http_proxy config before reloading http_client + monkeypatch.setattr( + "app.config.config.http_proxy", HttpUrl("http://proxy.example.com:8080") + ) + + # Reload the http_client module to trigger creation of proxy_mounts with HttpUrl set + # This would fail with the old code (AttributeError: 'HttpUrl' object has no attribute 'url') + import app.common.http_client + + importlib.reload(app.common.http_client) + + # Verify clients can be created + client = app.common.http_client.create_client() + assert client is not None + + async_client = app.common.http_client.create_async_client() + assert async_client is not None