Skip to content

Commit

Permalink
LITE-31369: Fix AsyncConnectClient ignoring environment proxies
Browse files Browse the repository at this point in the history
  • Loading branch information
Carlos Herrero committed Nov 15, 2024
1 parent df5f65d commit 510b1fe
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions connect/client/fluent.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
#
import contextvars
import threading
from functools import cache
from json.decoder import JSONDecodeError
from typing import Union

import httpx
import requests
from httpx._config import Proxy
from httpx._utils import get_environment_proxies
from requests.adapters import HTTPAdapter

from connect.client.constants import CONNECT_ENDPOINT_URL, CONNECT_SPECS_URL
Expand Down Expand Up @@ -237,6 +240,20 @@ def _get_namespace_class(self):
_SSL_CONTEXT = httpx.create_ssl_context()


@cache
def _get_async_mounts():
"""
This code based on how httpx.Client mounts proxies from environment.
This is cached to allow reusing the created transport objects.
"""
return {
key: None
if url is None
else httpx.AsyncHTTPTransport(verify=_SSL_CONTEXT, proxy=Proxy(url=url))
for key, url in get_environment_proxies().items()
}


class AsyncConnectClient(_ConnectClientBase, AsyncClientMixin):
"""
Create a new instance of the AsyncConnectClient.
Expand Down Expand Up @@ -274,12 +291,14 @@ def __init__(self, *args, **kwargs):
def session(self):
value = self._session.get()
if not value:
value = httpx.AsyncClient(
transport=_ASYNC_TRANSPORTS.setdefault(
self.endpoint,
httpx.AsyncHTTPTransport(verify=_SSL_CONTEXT),
),
)
transport = _ASYNC_TRANSPORTS.get(self.endpoint)
if not transport:
transport = _ASYNC_TRANSPORTS[self.endpoint] = httpx.AsyncHTTPTransport(
verify=_SSL_CONTEXT,
)
# When passing a transport to httpx a Client/AsyncClient, proxies defined in environment
# (like HTTP_PROXY) are ignored, so let's pass them using mounts parameter.
value = httpx.AsyncClient(transport=transport, mounts=_get_async_mounts())
self._session.set(value)
return value

Expand Down

0 comments on commit 510b1fe

Please sign in to comment.