FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

LITE-31369: Fix AsyncConnectClient ignoring environment proxies by qarlosh · Pull Request #82 · cloudblue/connect-python-openapi-client · GitHub

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
31 changes: 25 additions & 6 deletions connect/client/fluent.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

also fixed how transports are reused, because a new transport object was created every time even if the existing one for the endpoint was used.

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

Back | FazBrowse Home | New Git URL