Allow passing trust_env to aiohttp.ClientSession (#438)

Fixes #368

Signed-off-by: Michael Oliver <michael@michaeloliver.dev>
This commit is contained in:
Michael Oliver
2023-07-19 14:02:04 +01:00
committed by GitHub
parent 49d75c2e7d
commit 4dba35deea
5 changed files with 51 additions and 0 deletions
+2
View File
@@ -14,6 +14,8 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- Enhanced YAML test runner to use OpenSearch rest-api-spec YAML tests ([#414](https://github.com/opensearch-project/opensearch-py/pull/414)
- Added `Search#collapse` ([#409](https://github.com/opensearch-project/opensearch-py/issues/409))
- Added support for the ISM API ([#398](https://github.com/opensearch-project/opensearch-py/pull/398))
- Added `trust_env` to `AIOHttpConnection` ([#398](https://github.com/opensearch-project/opensearch-py/pull/438))
### Changed
- Upgrading pytest-asyncio to latest version - 0.21.0 ([#339](https://github.com/opensearch-project/opensearch-py/pull/339))
- Fixed flaky CI tests by replacing httpbin with a simple http_server ([#395](https://github.com/opensearch-project/opensearch-py/pull/395))
+31
View File
@@ -35,6 +35,7 @@
- [Pre-requisites to use `AWSV4SignerAuth`](#pre-requisites-to-use-awsv4signerauth)
- [Using IAM authentication with an async client](#using-iam-authentication-with-an-async-client)
- [Using Kerberos](#using-kerberos)
- [Using environment settings for proxy configuration](#using-environment-settings-for-proxy-configuration)
# User guide of OpenSearch Python client
@@ -686,3 +687,33 @@ client = OpenSearch(
health = client.cluster.health()
```
## Using environment settings for proxy configuration
Tell connection to get proxy information from `HTTP_PROXY` / `HTTPS_PROXY` environment variables or `~/.netrc` file if present.
```python
from opensearchpy import OpenSearch, RequestsHttpConnection
OpenSearch(
hosts=["htps://..."],
use_ssl=True,
verify_certs=True,
connection_class=RequestsHttpConnection,
trust_env=True,
)
```
```python
from opensearchpy import AsyncOpenSearch, AIOHttpConnection
client = AsyncOpenSearch(
hosts=["htps://..."],
use_ssl=True,
verify_certs=True,
connection_class=AIOHttpConnection,
trust_env=True,
)
```
+3
View File
@@ -91,6 +91,7 @@ class AIOHttpConnection(AsyncConnection):
http_compress=None,
opaque_id=None,
loop=None,
trust_env=False,
**kwargs
):
"""
@@ -219,6 +220,7 @@ class AIOHttpConnection(AsyncConnection):
self._limit = maxsize
self._http_auth = http_auth
self._ssl_context = ssl_context
self._trust_env = trust_env
async def perform_request(
self, method, url, params=None, body=None, timeout=None, ignore=(), headers=None
@@ -367,6 +369,7 @@ class AIOHttpConnection(AsyncConnection):
connector=aiohttp.TCPConnector(
limit=self._limit, use_dns_cache=True, ssl=self._ssl_context
),
trust_env=self._trust_env,
)
+1
View File
@@ -66,5 +66,6 @@ class AIOHttpConnection(AsyncConnection):
http_compress: Optional[bool] = ...,
opaque_id: Optional[str] = ...,
loop: Any = ...,
trust_env: bool = ...,
**kwargs: Any
) -> None: ...
@@ -274,6 +274,20 @@ class TestAIOHttpConnection:
AIOHttpConnection(use_ssl=True, verify_certs=False)
load_verify_locations.assert_not_called()
async def test_trust_env(self):
con = AIOHttpConnection(trust_env=True)
await con._create_aiohttp_session()
assert con._trust_env is True
assert con.session.trust_env is True
async def test_trust_env_default_value_is_false(self):
con = AIOHttpConnection()
await con._create_aiohttp_session()
assert con._trust_env is False
assert con.session.trust_env is False
@patch("opensearchpy.connection.base.logger")
async def test_uncompressed_body_logged(self, logger):
con = await self._get_mock_connection(connection_params={"http_compress": True})