Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow passing trust_env to aiohttp.ClientSession #438

Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
31 changes: 31 additions & 0 deletions USER_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 changes: 3 additions & 0 deletions opensearchpy/_async/http_aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ def __init__(
http_compress=None,
opaque_id=None,
loop=None,
trust_env=False,
**kwargs
):
"""
Expand Down Expand Up @@ -219,6 +220,7 @@ def __init__(
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
Expand Down Expand Up @@ -367,6 +369,7 @@ async def _create_aiohttp_session(self):
connector=aiohttp.TCPConnector(
limit=self._limit, use_dns_cache=True, ssl=self._ssl_context
),
trust_env=self._trust_env,
)


Expand Down
1 change: 1 addition & 0 deletions opensearchpy/_async/http_aiohttp.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,6 @@ class AIOHttpConnection(AsyncConnection):
http_compress: Optional[bool] = ...,
opaque_id: Optional[str] = ...,
loop: Any = ...,
trust_env: bool = ...,
**kwargs: Any
) -> None: ...
14 changes: 14 additions & 0 deletions test_opensearchpy/test_async/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,20 @@ async def test_uses_no_ca_certs(self, load_verify_locations):
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})
Expand Down