Package: aiohttp_route_decorator
aiohttp_route_decoratorThe library provides@routedecorator foraiohttp.web, resembling the contract ofFlask@app.route.The imaginaryaiohttp@app.routedecorator isdiscouragedformultiplereasons; this one tries to solve part of those problems (theappdoesn’t need to be global at the very least).Installationpip install aiohttp_route_decoratorUsageCreate arouteobject in each of your handler modules, and decorate the handlers:# myapp/handlers.pyfromaiohttp_route_decoratorimportRouteCollectorroute=RouteCollector()@route('/')asyncdefindex(request):returnweb.Response(body=b'OK')@route('/publish',method='POST')asyncdefpublish(request):returnweb.Response(body=b'OK')@route('/login',methods=['GET','POST'],name='login')asyncdeflogin(request):ifrequest.method=='POST':returnweb.Response(body=b'OK')returnweb.Response(body=b'Login')When you init the application, push the collectedroutesintoapp.router:fromaiohttpimportwebfrommyappimporthandlersdefrun():app=web.Application()handlers.route.add_to_router(app.router)web.run_app(app)Non-decorator useIf you prefer to keep your routes together, you can construct the list manually after your handers:fromaiohttp_route_decoratorimportRouteCollector,Routeasyncdefindex(request):returnweb.Response(body=b'OK')asyncdefpublish(request):returnweb.Response(body=b'OK')asyncdeflogin(request):ifrequest.method=='POST':returnweb.Response(body=b'OK')returnweb.Response(body=b'Login')routes=RouteCollector([Route('/',index),Route('/publish',publish,method='POST'),Route('/login',login,methods=['GET','POST'],name='login'),])Prefixed routesYou can provide common route prefix that will be prepended to all routes:fromaiohttp_route_decoratorimportRouteCollectorroutes=RouteCollector(prefix='/app')@route('/')asyncdefindex(request):returnweb.Response(body=b'OK')@route('/publish',method='POST')asyncdefpublish(request):returnweb.Response(body=b'OK')...handlers.route.add_to_router(app.router)# /app/ -> index# /app/publish -> publishYou can also provide the prefix withinadd_to_router()call instead:fromaiohttp_route_decoratorimportRouteCollectorroutes=RouteCollector()@route('/')asyncdefindex(request):returnweb.Response(body=b'OK')@route('/publish',method='POST')asyncdefpublish(request):returnweb.Response(body=b'OK')...handlers.route.add_to_router(app.router,prefix='/app')# /app/ -> index# /app/publish -> publish…or use both:fromaiohttp_route_decoratorimportRouteCollectorroutes=RouteCollector(prefix='/app')@route('/')asyncdefindex(request):returnweb.Response(body=b'OK')@route('/publish',method='POST')asyncdefpublish(request):returnweb.Response(body=b'OK')...handlers.route.add_to_router(app.router,prefix='/project')# /project/app/ -> index# /project/app/publish -> publishThe non-decorator version ofRouteCollectorcan also accept prefix:fromaiohttp_route_decoratorimportRouteCollector,Routeasyncdefindex(request):returnweb.Response(body=b'OK')asyncdefpublish(request):returnweb.Response(body=b'OK')routes=RouteCollector(prefix='/app',routes=[Route('/',index),Route('/publish',publish,method='POST'),])Parameters referenceroute(path, *,method='GET',methods=None, name=None, **kwargs)path(str) — route path. Should be started with slash ('/').method(str) — HTTP method for route. Should be one of'GET','POST','PUT','DELETE','PATCH','HEAD','OPTIONS'or'*'for any method.methods(List[str]) — optional shortcut for creating several routes with different HTTP methods at once. If used, should be a list of acceptable values formethodargument.name(str) — optional route name.kwargs— other parameters to be passed toaiohttp.web.Resource.add_route().