Skip to content

Commit 08f9b06

Browse files
authored
load mounds from environment variable for httpx (#5297)
* load mounds from environment variable for httpx * only call the function once
1 parent 54f93b0 commit 08f9b06

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

reflex/utils/net.py

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import httpx
99

1010
from reflex.utils.decorator import once
11+
from reflex.utils.types import Unset
1112

1213
from . import console
1314

@@ -63,6 +64,30 @@ def wrapper(*args: _P.args, **kwargs: _P.kwargs) -> _T:
6364
return wrapper
6465

6566

67+
def _wrap_https_lazy_func(
68+
func: Callable[[], Callable[_P, _T]],
69+
) -> Callable[_P, _T]:
70+
"""Wrap an HTTPS function with logging.
71+
72+
Args:
73+
func: The function to wrap.
74+
75+
Returns:
76+
The wrapped function.
77+
"""
78+
unset = Unset()
79+
f: Callable[_P, _T] | Unset = unset
80+
81+
def wrapper(*args: _P.args, **kwargs: _P.kwargs) -> _T:
82+
nonlocal f
83+
if isinstance(f, Unset):
84+
f = _wrap_https_func(func())
85+
functools.update_wrapper(wrapper, f)
86+
return f(*args, **kwargs)
87+
88+
return wrapper
89+
90+
6691
def _is_ipv4_supported() -> bool:
6792
"""Determine if the system supports IPv4.
6893
@@ -120,12 +145,20 @@ def _httpx_client() -> httpx.Client:
120145
Returns:
121146
An HTTPX client.
122147
"""
148+
from httpx._utils import get_environment_proxies
149+
123150
return httpx.Client(
124151
transport=httpx.HTTPTransport(
125152
local_address=_httpx_local_address_kwarg(),
126153
verify=_httpx_verify_kwarg(),
127-
)
154+
),
155+
mounts={
156+
key: (
157+
None if url is None else httpx.HTTPTransport(proxy=httpx.Proxy(url=url))
158+
)
159+
for key, url in get_environment_proxies().items()
160+
},
128161
)
129162

130163

131-
get = _wrap_https_func(_httpx_client().get)
164+
get = _wrap_https_lazy_func(lambda: _httpx_client().get)

0 commit comments

Comments
 (0)