Skip to content
This repository has been archived by the owner on Oct 1, 2021. It is now read-only.

Commit

Permalink
Update from deprecated techniques
Browse files Browse the repository at this point in the history
- `loop` parameter to `Application.__init__()` is deprecated, and its usage has been removed from examples and tests.
- Passing a request handler to `Application.add_route` which is not a coroutine function is deprecated, and thus a warning is raised when passing a `JsonRpc` instance to it - now to avoid this warning, the `JsonRpc.handle_request` instance method can be passed to it, which is identical the previous `__call__` function. The `__call__` function now acts as an alias for `JsonRpc.handle_request` for backwards compatibility.
- Usage of `Application.make_handler` to asynchronously start the server is deprecated in favour of the `AppRunner` API.

Signed-off-by: Toby Harradine <tobyharradine@gmail.com>
  • Loading branch information
Tobotimus authored and fscherf committed Feb 11, 2019
1 parent 6dd5011 commit d5720f4
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 71 deletions.
11 changes: 3 additions & 8 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ The following code implements a simple RPC server that serves the method ``ping`

.. code-block:: python
from aiohttp.web import Application
from aiohttp.web import Application, run_app
from aiohttp_json_rpc import JsonRpc
import asyncio
Expand All @@ -63,14 +63,9 @@ The following code implements a simple RPC server that serves the method ``ping`
)
app = Application(loop=loop)
app.router.add_route('*', '/', rpc)
app.router.add_route('*', '/', rpc.handle_request)
handler = app.make_handler()
server = loop.run_until_complete(
loop.create_server(handler, '0.0.0.0', 8080))
loop.run_forever()
run_app(app, host='0.0.0.0', port=8080)
Client (JS)
Expand Down
29 changes: 10 additions & 19 deletions aiohttp_json_rpc/pytest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest
import os

from aiohttp.web import Application
from aiohttp.web import Application, AppRunner, TCPSite

from aiohttp_json_rpc import JsonRpc, JsonRpcClient

Expand Down Expand Up @@ -63,17 +63,18 @@ async def finish_connections(self):
def gen_rpc_context(loop, host, port, rpc, rpc_route, routes=(),
RpcContext=RpcContext):
# make app
app = Application(loop=loop)
app = Application()

app.router.add_route(*rpc_route)

for route in routes:
app.router.add_route(*route)

# make handler
handler = app.make_handler()

server = loop.run_until_complete(loop.create_server(handler, host, port))
# make app runner
runner = AppRunner(app)
loop.run_until_complete(runner.setup())
site = TCPSite(runner, host, port)
loop.run_until_complete(site.start())

# create RpcContext
rpc_context = RpcContext(app, rpc, host, port, rpc_route[1])
Expand All @@ -84,23 +85,13 @@ def gen_rpc_context(loop, host, port, rpc, rpc_route, routes=(),
loop.run_until_complete(rpc_context.finish_connections())

# teardown server
async def teardown_server():
try:
server.close()
await server.wait_closed()
await app.shutdown()
await handler.shutdown(60.0)

finally:
await app.cleanup()

loop.run_until_complete(teardown_server())
loop.run_until_complete(runner.cleanup())


@pytest.yield_fixture
def rpc_context(event_loop, unused_tcp_port):
rpc = JsonRpc(loop=event_loop, max_workers=4)
rpc_route = ('*', '/rpc', rpc)
rpc_route = ('*', '/rpc', rpc.handle_request)

for context in gen_rpc_context(event_loop, 'localhost', unused_tcp_port,
rpc, rpc_route):
Expand All @@ -116,7 +107,7 @@ def django_rpc_context(db, event_loop, unused_tcp_port):
auth_backend=DjangoAuthBackend(generic_orm_methods=True),
max_workers=4)

rpc_route = ('*', '/rpc', rpc)
rpc_route = ('*', '/rpc', rpc.handle_request)

routes = [
('*', '/{path_info:.*}', WSGIHandler(django_wsgi_application)),
Expand Down
5 changes: 4 additions & 1 deletion aiohttp_json_rpc/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,10 @@ def func(request):

self.topics[name] = func

async def __call__(self, request):
def __call__(self, request):
return self.handle_request(request)

async def handle_request(self, request):
# prepare request
request.rpc = self
self.auth_backend.prepare_request(request)
Expand Down
6 changes: 4 additions & 2 deletions dev-server/dev-server
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ app.router.add_static('/static', 'dev-server/static', show_index=True)
app.router.add_get('/rpc', rpc)
app.router.add_get('/', index)

loop.run_until_complete(
loop.create_server(app.make_handler(), 'localhost', 8080))
runner = web.AppRunner(app)
loop.run_until_complete(runner.setup())
site = web.TCPSite(runner, 'localhost', 8080)
loop.run_until_complete(site.start())

loop.run_until_complete(setup_client(client))

Expand Down
11 changes: 3 additions & 8 deletions examples/base_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# -*- coding: utf-8 -*-
# Author: Florian Scherf <f.scherf@pengutronix.de>

from aiohttp.web import Application
from aiohttp.web import Application, run_app
from aiohttp_json_rpc import JsonRpc
import asyncio

Expand All @@ -21,11 +21,6 @@ def ping(request):
)

app = Application(loop=loop)
app.router.add_route('*', '/', rpc)
app.router.add_route('*', '/', rpc.handle_request)

handler = app.make_handler()

server = loop.run_until_complete(
loop.create_server(handler, '0.0.0.0', 8080))

loop.run_forever()
run_app(app, host='0.0.0.0', port=8080)
11 changes: 3 additions & 8 deletions examples/django_example_project/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Author: Florian Scherf <f.scherf@pengutronix.de>

from django_example_project.wsgi import application as django_app
from aiohttp.web import Application
from aiohttp.web import Application, run_app
from aiohttp_wsgi import WSGIHandler
from aiohttp_json_rpc import JsonRpc
from aiohttp_json_rpc.auth.django import DjangoAuthBackend
Expand All @@ -20,12 +20,7 @@
('', 'django_example_app.rpc')
)

app.router.add_route('*', '/rpc', rpc)
app.router.add_route('*', '/rpc', rpc.handle_request)
app.router.add_route('*', '/{path_info:.*}', wsgi_handler.handle_request)

handler = app.make_handler()

server = loop.run_until_complete(
loop.create_server(handler, '0.0.0.0', 8080))

loop.run_forever()
run_app(app, host='0.0.0.0', port=8080)
17 changes: 5 additions & 12 deletions examples/passwd_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,17 @@
from aiohttp_json_rpc.auth.passwd import PasswdAuthBackend
from aiohttp_json_rpc.auth import login_required
from aiohttp_json_rpc import JsonRpc
from aiohttp.web import Application
from aiohttp.web import Application, run_app
import asyncio
import os


@asyncio.coroutine
def ping(request):
async def ping(request):
return 'pong'


@login_required
@asyncio.coroutine
def pong(request):
async def pong(request):
return 'ping'


Expand All @@ -34,11 +32,6 @@ def pong(request):
)

app = Application(loop=loop)
app.router.add_route('*', '/', rpc)
app.router.add_route('*', '/', rpc.handle_request)

handler = app.make_handler()

server = loop.run_until_complete(
loop.create_server(handler, '0.0.0.0', 8080))

loop.run_forever()
run_app(app, host='0.0.0.0', port=8080)
11 changes: 3 additions & 8 deletions examples/publish_subscribe_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# -*- coding: utf-8 -*-
# Author: Florian Scherf <f.scherf@pengutronix.de>

from aiohttp.web import Application
from aiohttp.web import Application, run_app
from aiohttp_json_rpc import JsonRpc
import datetime
import asyncio
Expand All @@ -29,11 +29,6 @@ def clock(rpc):
loop.create_task(clock(rpc))

app = Application(loop=loop)
app.router.add_route('*', '/', rpc)
app.router.add_route('*', '/', rpc.handle_request)

handler = app.make_handler()

server = loop.run_until_complete(
loop.create_server(handler, '0.0.0.0', 8080))

loop.run_forever()
run_app(app, host='0.0.0.0', port=8080)
12 changes: 7 additions & 5 deletions tests/test_django_transactions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from aiohttp import WSMsgType
from aiohttp.web import Application
from aiohttp.web import Application, AppRunner, TCPSite
from aiohttp_json_rpc import JsonRpc, RpcInvalidParamsError
import aiohttp
import asyncio
Expand Down Expand Up @@ -148,17 +148,19 @@ def create_client(client_id, *args, **kwargs):
assert Item.objects.count() == 0

# setup rpc
app = Application(loop=event_loop)
app = Application()
rpc = JsonRpc()

rpc.add_methods(
('', add),
)

app.router.add_route('*', '/', rpc)
app.router.add_route('*', '/', rpc.handle_request)

await event_loop.create_server(
app.make_handler(), 'localhost', unused_tcp_port)
runner = AppRunner(app)
await runner.setup()
site = TCPSite(runner, 'localhost', unused_tcp_port)
await site.start()

# setup clients and watchdog
tasks = [
Expand Down

0 comments on commit d5720f4

Please sign in to comment.