-
Feature
APIRoute has a "name" attribute but APIRouter no a "name" attribute; i want APIRouter add a "name" attribute ,so i can do as that: router = request.scope.get('router')
if router.name == '':
print(router.routes) |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments
-
|
What would be the Also, why? |
Beta Was this translation helpful? Give feedback.
-
yes, The name of the class; import uvicorn
from fastapi import FastAPI, Request, APIRouter
from fastapi.routing import APIRoute
app = FastAPI()
router = APIRouter(prefix='/user')
@router.get('/{uid}')
async def user_get(uid: int, request: Request):
print('request.url.path:', request.url.path)
router = request.scope.get('router')
print(type(router))
# why router.prefix is None ???????!!!!!
print('router.prefix:', router.prefix)
return uid
app.include_router(router)
if __name__ == '__main__':
uvicorn.run(app, host='0.0.0.0', port=5000, reload=False, debug=False, workers=1)i just want get the router_info and route_info and the current function_name by request; like flask request.endpoint |
Beta Was this translation helpful? Give feedback.
-
also, this code can not get router_info ......... #!/usr/bin/env python
# -*- coding: utf-8 -*-
import uvicorn
from fastapi import FastAPI, Response, Request, APIRouter
from fastapi.routing import APIRoute
app = FastAPI()
@app.middleware('http')
async def mid_http(request: Request, call_next):
response: Response = await call_next(request)
return response
class MyRouter(APIRouter):
def __init__(self, router_name: str, **kwargs):
super(MyRouter, self).__init__(**kwargs)
self.router_name = router_name
class MyRoute(APIRoute):
def get_route_handler(self):
super_route_handler = super().get_route_handler()
route_name = self.name
async def my_route_handler(request: Request):
# request get the APIRoute name
request.state.route_name = route_name
response: Response = await super_route_handler(request)
return response
return my_route_handler
user_router = MyRouter(router_name='user', prefix='/user', route_class=MyRoute)
@user_router.get('/{uid}')
async def user_get(*, uid: int, request: Request):
print('request.url.path:', request.url.path)
print(f'user_get request.state.route_name :{request.state.route_name}')
router = request.scope.get('router')
print('router.prefix:', router.prefix)
# raise error: APIRouter has no router_name attribute, why this router is not MyRoute??
print('router.router_name:', router.router_name)
return uid
app.include_router(user_router)
if __name__ == '__main__':
uvicorn.run(app, host='0.0.0.0', port=5000, reload=False, debug=False, workers=1) |
Beta Was this translation helpful? Give feedback.
-
|
Could you please explain your objective? What are you trying to solve? There's probably a way to do it already, but it's probably different from the way you are trying to achieve it.
|
Beta Was this translation helpful? Give feedback.
-
|
Assuming the original need was handled, this will be automatically closed now. But feel free to add more comments or create new issues or PRs. |
Beta Was this translation helpful? Give feedback.
Could you please explain your objective? What are you trying to solve? There's probably a way to do it already, but it's probably different from the way you are trying to achieve it.