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

Add support for async-with #10

Merged
merged 3 commits into from
Jul 10, 2023
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: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
flake8 jsonrpc_async tests.py
- name: Test with pytest
run: |
pytest --cov-report term-missing --cov=jsonrpc_async tests.py
pytest --cov-report term-missing --cov=jsonrpc_async --asyncio-mode=auto tests.py
- name: Coveralls
uses: AndreMiras/coveralls-python-action@develop
with:
Expand Down
30 changes: 10 additions & 20 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,11 @@ Execute remote JSON-RPC functions
from jsonrpc_async import Server

async def routine():
server = Server('http://localhost:8080')
try:
async with Server('http://localhost:8080') as server:
await server.foo(1, 2)
await server.foo(bar=1, baz=2)
await server.foo({'foo': 'bar'})
await server.foo.bar(baz=1, qux=2)
finally:
await server.session.close()

asyncio.get_event_loop().run_until_complete(routine())

Expand All @@ -47,11 +44,8 @@ A notification
from jsonrpc_async import Server

async def routine():
server = Server('http://localhost:8080')
try:
async with Server('http://localhost:8080') as server:
await server.foo(bar=1, _notification=True)
finally:
await server.session.close()

asyncio.get_event_loop().run_until_complete(routine())

Expand All @@ -64,14 +58,12 @@ Pass through arguments to aiohttp (see also `aiohttp documentation <http://aioh
from jsonrpc_async import Server

async def routine():
server = Server(
async with Server(
'http://localhost:8080',
auth=aiohttp.BasicAuth('user', 'pass'),
headers={'x-test2': 'true'})
try:
headers={'x-test2': 'true'}
) as server:
await server.foo()
finally:
await server.session.close()

asyncio.get_event_loop().run_until_complete(routine())

Expand All @@ -84,13 +76,11 @@ Pass through aiohttp exceptions
from jsonrpc_async import Server

async def routine():
server = Server('http://unknown-host')
try:
await server.foo()
except TransportError as transport_error:
print(transport_error.args[1]) # this will hold a aiohttp exception instance
finally:
await server.session.close()
async with Server('http://unknown-host') as server:
try:
await server.foo()
except TransportError as transport_error:
print(transport_error.args[1]) # this will hold a aiohttp exception instance

asyncio.get_event_loop().run_until_complete(routine())

Expand Down
7 changes: 7 additions & 0 deletions jsonrpc_async/jsonrpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,10 @@ async def send_message(self, message):
'Cannot deserialize response body', message, value_error)

return message.parse_response(response_data)

async def __aenter__(self):
await self.session.__aenter__()
return self

async def __aexit__(self, exc_type, exc, tb):
return await self.session.__aexit__(exc_type, exc, tb)
18 changes: 18 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,3 +304,21 @@ def create_app():

assert await server.subtract(42, 23) == 19
assert loads_mock.call_count == 1


async def test_context_manager(aiohttp_client):
# catch non-json responses
async def handler1(request):
return aiohttp.web.Response(
text='not json', content_type='application/json')

def create_app():
app = aiohttp.web.Application()
app.router.add_route('POST', '/', handler1)
return app

client = await aiohttp_client(create_app())
async with Server('/', client) as server:
assert isinstance(server, Server)
assert not server.session.session.closed
assert server.session.session.closed
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ envlist =
setenv =
PYTHONPATH = {toxinidir}:{toxinidir}/jsonrpc_async
commands =
pytest --cov=jsonrpc_async tests.py
pytest --cov=jsonrpc_async --asyncio-mode=auto tests.py
coverage report
deps =
-r{toxinidir}/requirements-test.txt
Expand Down