Whenever you place a route as part of a class, it does not allow this to be possible, unless you remove te response argument, leaving just self (which creates problems as self is actually response).
Provide the webpage as it would like this:
from aiohttp import web
import asyncio
routes = web.RouteTableDef()
app = web.Application()
@routes.get("/")
async def root(response):
return web.Response(text="5")
app.add_routes(routes)
web.run_app(app)
Errors with
Error handling request
Traceback (most recent call last):
File "D:\Program Files\Python3\lib\site-packages\aiohttp\web_protocol.py", line 385, in start
resp = await self._request_handler(request)
File "D:\Program Files\Python3\lib\site-packages\aiohttp\web_app.py", line 338, in _handle
resp = await handler(request)
TypeError: root() missing 1 required positional argument: 'response'
Create and run this script
from aiohttp import web
import asyncio
routes = web.RouteTableDef()
app = web.Application()
class Example:
@routes.get("/")
async def root(self, response):
return web.Response(text="5")
app.add_routes(routes)
web.run_app(app)
Latest version of aiohttp (3.2.1)
Windows 10
In your example, web handler is not instantiated on decorator appliance time.
The decorated function is just a function, not bound method.
Maybe we can master something but I don't see a clean implementation yet.
Any idea is welcome.
Closing the issue.
If somebody wants to propose a robust implementation -- please open a new one.
I have been thinking of an implementation, I just haven't got round to implementing it yet. Here's the basic outline:
add_routes(ClassWhichContainsTheRouteObjects())Sorry for the late reply.
Feel free to make a PR with an implementation of your idea.
I will when done 馃槣!
Did anyone ever end up working on this? Would be a nice feature for sure.
I've got some in-progress code sitting on my desktop, I've mostly forgotten about it.
EDIT: This seems similar to https://github.com/dask/dask/issues/3317.
I've got a working implementation, will attempt some documentation (and possibly tests) and then do a PR.
The dask/dask#3317 issue only appears to be an issue for decorators that are classes and when actually replacing the decorated function.