Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions app/common/http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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:
Expand All @@ -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:
Expand Down
24 changes: 24 additions & 0 deletions app/common/test_http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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