Skip to content

Commit e87b6ab

Browse files
tinolionelz
authored andcommitted
Enable async with AsyncClient(..) as client construct (#16)
1 parent 6f713d8 commit e87b6ab

File tree

3 files changed

+49
-8
lines changed

3 files changed

+49
-8
lines changed

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ client = async_hvac.AsyncClient(url='https://localhost:8200',
4040
client = async_hvac.AsyncClient(url='https://localhost:8200', verify=False)
4141
```
4242

43+
Note that you will have to close the client with `client.close()` in order to
44+
avoid lingering open aiohttp.client.ClientSession's. An alternative is to open
45+
the client in a `with`-statement:
46+
47+
```python
48+
async with async_hvac.AsyncClient(url='https://localhost:8200') as client:
49+
print(await client.read('secret/foo'))
50+
```
51+
4352
### Read and write to secret backends
4453

4554
```python

async_hvac/tests/test_client.py

+24-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
from asynctest import TestCase
2-
3-
from parameterized import parameterized
4-
51
from async_hvac import AsyncClient
62
from async_hvac.tests.util import RequestsMocker
3+
from asynctest import TestCase
4+
from parameterized import parameterized
75

86

97
class TestClient(TestCase):
@@ -31,3 +29,25 @@ async def test___request(self, test_label, test_url, requests_mocker):
3129
second=response.status,
3230
)
3331
await client.close()
32+
33+
@parameterized.expand([
34+
("standard Vault address", 'https://localhost:8200'),
35+
("Vault address with route", 'https://example.com/vault'),
36+
])
37+
@RequestsMocker()
38+
async def test_async_with_request(self, test_label, test_url, requests_mocker):
39+
test_path = 'v1/sys/health'
40+
expected_status_code = 200
41+
mock_url = '{0}/{1}'.format(test_url, test_path)
42+
requests_mocker.register_uri(
43+
method='GET',
44+
url=mock_url,
45+
)
46+
async with AsyncClient(url=test_url) as client:
47+
response = await client._get(
48+
url='v1/sys/health',
49+
)
50+
self.assertEqual(
51+
first=expected_status_code,
52+
second=response.status,
53+
)

async_hvac/v1/__init__.py

+16-4
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@
44
import ssl
55
from base64 import b64encode
66

7+
import aiohttp
8+
from async_hvac import aws_utils, exceptions
9+
710
try:
811
import hcl
912

1013
has_hcl_parser = True
1114
except ImportError:
1215
has_hcl_parser = False
13-
import aiohttp
14-
15-
from async_hvac import aws_utils
16-
from async_hvac import exceptions
1716

1817

1918
class AsyncClient(object):
@@ -37,6 +36,19 @@ def __init__(self, url='http://127.0.0.1:8200', token=None,
3736
else:
3837
self._sslcontext = False
3938

39+
def __enter__(self):
40+
raise TypeError("Use async with instead")
41+
42+
def __exit__(self, exc_type, exc_val, exc_tb):
43+
# __exit__ should exist in pair with __enter__ but never executed
44+
pass # pragma: no cover
45+
46+
async def __aenter__(self):
47+
return self
48+
49+
async def __aexit__(self, exc_type, exc_val, exc_tb):
50+
await self.close()
51+
4052
@property
4153
def session(self):
4254
if not self._session:

0 commit comments

Comments
 (0)