From d5720f4a54a5c67c9db9e6e1b663c6cf3e6f2b84 Mon Sep 17 00:00:00 2001 From: Toby Harradine Date: Sun, 10 Feb 2019 21:35:19 +1100 Subject: [PATCH] Update from deprecated techniques - `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 --- README.rst | 11 +++------ aiohttp_json_rpc/pytest.py | 29 ++++++++--------------- aiohttp_json_rpc/rpc.py | 5 +++- dev-server/dev-server | 6 +++-- examples/base_example.py | 11 +++------ examples/django_example_project/server.py | 11 +++------ examples/passwd_example.py | 17 ++++--------- examples/publish_subscribe_example.py | 11 +++------ tests/test_django_transactions.py | 12 ++++++---- 9 files changed, 42 insertions(+), 71 deletions(-) diff --git a/README.rst b/README.rst index c17a947..37267f7 100644 --- a/README.rst +++ b/README.rst @@ -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 @@ -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) diff --git a/aiohttp_json_rpc/pytest.py b/aiohttp_json_rpc/pytest.py index 5831d04..e57c26f 100644 --- a/aiohttp_json_rpc/pytest.py +++ b/aiohttp_json_rpc/pytest.py @@ -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 @@ -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]) @@ -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): @@ -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)), diff --git a/aiohttp_json_rpc/rpc.py b/aiohttp_json_rpc/rpc.py index 84e6cef..1b4d978 100644 --- a/aiohttp_json_rpc/rpc.py +++ b/aiohttp_json_rpc/rpc.py @@ -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) diff --git a/dev-server/dev-server b/dev-server/dev-server index 8c9562a..1f0b4d9 100755 --- a/dev-server/dev-server +++ b/dev-server/dev-server @@ -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)) diff --git a/examples/base_example.py b/examples/base_example.py index 3129c83..bd5ad64 100755 --- a/examples/base_example.py +++ b/examples/base_example.py @@ -2,7 +2,7 @@ # -*- coding: utf-8 -*- # Author: Florian Scherf -from aiohttp.web import Application +from aiohttp.web import Application, run_app from aiohttp_json_rpc import JsonRpc import asyncio @@ -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) diff --git a/examples/django_example_project/server.py b/examples/django_example_project/server.py index d07ab51..c148913 100755 --- a/examples/django_example_project/server.py +++ b/examples/django_example_project/server.py @@ -3,7 +3,7 @@ # Author: Florian Scherf 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 @@ -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) diff --git a/examples/passwd_example.py b/examples/passwd_example.py index 14bf060..788298f 100755 --- a/examples/passwd_example.py +++ b/examples/passwd_example.py @@ -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' @@ -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) diff --git a/examples/publish_subscribe_example.py b/examples/publish_subscribe_example.py index 83780b4..1eaee0f 100755 --- a/examples/publish_subscribe_example.py +++ b/examples/publish_subscribe_example.py @@ -2,7 +2,7 @@ # -*- coding: utf-8 -*- # Author: Florian Scherf -from aiohttp.web import Application +from aiohttp.web import Application, run_app from aiohttp_json_rpc import JsonRpc import datetime import asyncio @@ -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) diff --git a/tests/test_django_transactions.py b/tests/test_django_transactions.py index 7828b16..c930000 100644 --- a/tests/test_django_transactions.py +++ b/tests/test_django_transactions.py @@ -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 @@ -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 = [