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

Commit d5720f4

Browse files
Tobotimusfscherf
authored andcommitted
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 <tobyharradine@gmail.com>
1 parent 6dd5011 commit d5720f4

File tree

9 files changed

+42
-71
lines changed

9 files changed

+42
-71
lines changed

README.rst

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ The following code implements a simple RPC server that serves the method ``ping`
4545

4646
.. code-block:: python
4747
48-
from aiohttp.web import Application
48+
from aiohttp.web import Application, run_app
4949
from aiohttp_json_rpc import JsonRpc
5050
import asyncio
5151
@@ -63,14 +63,9 @@ The following code implements a simple RPC server that serves the method ``ping`
6363
)
6464
6565
app = Application(loop=loop)
66-
app.router.add_route('*', '/', rpc)
66+
app.router.add_route('*', '/', rpc.handle_request)
6767
68-
handler = app.make_handler()
69-
70-
server = loop.run_until_complete(
71-
loop.create_server(handler, '0.0.0.0', 8080))
72-
73-
loop.run_forever()
68+
run_app(app, host='0.0.0.0', port=8080)
7469
7570
7671
Client (JS)

aiohttp_json_rpc/pytest.py

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import pytest
44
import os
55

6-
from aiohttp.web import Application
6+
from aiohttp.web import Application, AppRunner, TCPSite
77

88
from aiohttp_json_rpc import JsonRpc, JsonRpcClient
99

@@ -63,17 +63,18 @@ async def finish_connections(self):
6363
def gen_rpc_context(loop, host, port, rpc, rpc_route, routes=(),
6464
RpcContext=RpcContext):
6565
# make app
66-
app = Application(loop=loop)
66+
app = Application()
6767

6868
app.router.add_route(*rpc_route)
6969

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

73-
# make handler
74-
handler = app.make_handler()
75-
76-
server = loop.run_until_complete(loop.create_server(handler, host, port))
73+
# make app runner
74+
runner = AppRunner(app)
75+
loop.run_until_complete(runner.setup())
76+
site = TCPSite(runner, host, port)
77+
loop.run_until_complete(site.start())
7778

7879
# create RpcContext
7980
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=(),
8485
loop.run_until_complete(rpc_context.finish_connections())
8586

8687
# teardown server
87-
async def teardown_server():
88-
try:
89-
server.close()
90-
await server.wait_closed()
91-
await app.shutdown()
92-
await handler.shutdown(60.0)
93-
94-
finally:
95-
await app.cleanup()
96-
97-
loop.run_until_complete(teardown_server())
88+
loop.run_until_complete(runner.cleanup())
9889

9990

10091
@pytest.yield_fixture
10192
def rpc_context(event_loop, unused_tcp_port):
10293
rpc = JsonRpc(loop=event_loop, max_workers=4)
103-
rpc_route = ('*', '/rpc', rpc)
94+
rpc_route = ('*', '/rpc', rpc.handle_request)
10495

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

119-
rpc_route = ('*', '/rpc', rpc)
110+
rpc_route = ('*', '/rpc', rpc.handle_request)
120111

121112
routes = [
122113
('*', '/{path_info:.*}', WSGIHandler(django_wsgi_application)),

aiohttp_json_rpc/rpc.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,10 @@ def func(request):
247247

248248
self.topics[name] = func
249249

250-
async def __call__(self, request):
250+
def __call__(self, request):
251+
return self.handle_request(request)
252+
253+
async def handle_request(self, request):
251254
# prepare request
252255
request.rpc = self
253256
self.auth_backend.prepare_request(request)

dev-server/dev-server

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@ app.router.add_static('/static', 'dev-server/static', show_index=True)
5050
app.router.add_get('/rpc', rpc)
5151
app.router.add_get('/', index)
5252

53-
loop.run_until_complete(
54-
loop.create_server(app.make_handler(), 'localhost', 8080))
53+
runner = web.AppRunner(app)
54+
loop.run_until_complete(runner.setup())
55+
site = web.TCPSite(runner, 'localhost', 8080)
56+
loop.run_until_complete(site.start())
5557

5658
loop.run_until_complete(setup_client(client))
5759

examples/base_example.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# -*- coding: utf-8 -*-
33
# Author: Florian Scherf <f.scherf@pengutronix.de>
44

5-
from aiohttp.web import Application
5+
from aiohttp.web import Application, run_app
66
from aiohttp_json_rpc import JsonRpc
77
import asyncio
88

@@ -21,11 +21,6 @@ def ping(request):
2121
)
2222

2323
app = Application(loop=loop)
24-
app.router.add_route('*', '/', rpc)
24+
app.router.add_route('*', '/', rpc.handle_request)
2525

26-
handler = app.make_handler()
27-
28-
server = loop.run_until_complete(
29-
loop.create_server(handler, '0.0.0.0', 8080))
30-
31-
loop.run_forever()
26+
run_app(app, host='0.0.0.0', port=8080)

examples/django_example_project/server.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Author: Florian Scherf <f.scherf@pengutronix.de>
44

55
from django_example_project.wsgi import application as django_app
6-
from aiohttp.web import Application
6+
from aiohttp.web import Application, run_app
77
from aiohttp_wsgi import WSGIHandler
88
from aiohttp_json_rpc import JsonRpc
99
from aiohttp_json_rpc.auth.django import DjangoAuthBackend
@@ -20,12 +20,7 @@
2020
('', 'django_example_app.rpc')
2121
)
2222

23-
app.router.add_route('*', '/rpc', rpc)
23+
app.router.add_route('*', '/rpc', rpc.handle_request)
2424
app.router.add_route('*', '/{path_info:.*}', wsgi_handler.handle_request)
2525

26-
handler = app.make_handler()
27-
28-
server = loop.run_until_complete(
29-
loop.create_server(handler, '0.0.0.0', 8080))
30-
31-
loop.run_forever()
26+
run_app(app, host='0.0.0.0', port=8080)

examples/passwd_example.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,17 @@
55
from aiohttp_json_rpc.auth.passwd import PasswdAuthBackend
66
from aiohttp_json_rpc.auth import login_required
77
from aiohttp_json_rpc import JsonRpc
8-
from aiohttp.web import Application
8+
from aiohttp.web import Application, run_app
99
import asyncio
1010
import os
1111

1212

13-
@asyncio.coroutine
14-
def ping(request):
13+
async def ping(request):
1514
return 'pong'
1615

1716

1817
@login_required
19-
@asyncio.coroutine
20-
def pong(request):
18+
async def pong(request):
2119
return 'ping'
2220

2321

@@ -34,11 +32,6 @@ def pong(request):
3432
)
3533

3634
app = Application(loop=loop)
37-
app.router.add_route('*', '/', rpc)
35+
app.router.add_route('*', '/', rpc.handle_request)
3836

39-
handler = app.make_handler()
40-
41-
server = loop.run_until_complete(
42-
loop.create_server(handler, '0.0.0.0', 8080))
43-
44-
loop.run_forever()
37+
run_app(app, host='0.0.0.0', port=8080)

examples/publish_subscribe_example.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# -*- coding: utf-8 -*-
33
# Author: Florian Scherf <f.scherf@pengutronix.de>
44

5-
from aiohttp.web import Application
5+
from aiohttp.web import Application, run_app
66
from aiohttp_json_rpc import JsonRpc
77
import datetime
88
import asyncio
@@ -29,11 +29,6 @@ def clock(rpc):
2929
loop.create_task(clock(rpc))
3030

3131
app = Application(loop=loop)
32-
app.router.add_route('*', '/', rpc)
32+
app.router.add_route('*', '/', rpc.handle_request)
3333

34-
handler = app.make_handler()
35-
36-
server = loop.run_until_complete(
37-
loop.create_server(handler, '0.0.0.0', 8080))
38-
39-
loop.run_forever()
34+
run_app(app, host='0.0.0.0', port=8080)

tests/test_django_transactions.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from aiohttp import WSMsgType
2-
from aiohttp.web import Application
2+
from aiohttp.web import Application, AppRunner, TCPSite
33
from aiohttp_json_rpc import JsonRpc, RpcInvalidParamsError
44
import aiohttp
55
import asyncio
@@ -148,17 +148,19 @@ def create_client(client_id, *args, **kwargs):
148148
assert Item.objects.count() == 0
149149

150150
# setup rpc
151-
app = Application(loop=event_loop)
151+
app = Application()
152152
rpc = JsonRpc()
153153

154154
rpc.add_methods(
155155
('', add),
156156
)
157157

158-
app.router.add_route('*', '/', rpc)
158+
app.router.add_route('*', '/', rpc.handle_request)
159159

160-
await event_loop.create_server(
161-
app.make_handler(), 'localhost', unused_tcp_port)
160+
runner = AppRunner(app)
161+
await runner.setup()
162+
site = TCPSite(runner, 'localhost', unused_tcp_port)
163+
await site.start()
162164

163165
# setup clients and watchdog
164166
tasks = [

0 commit comments

Comments
 (0)