-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to get the api path in middleware? #486
Comments
app.url_path_for('get_analytics_data') maybe?
Le ven. 30 août 2019 à 3:48 PM, devtud <notifications@github.com> a écrit :
… *Description*
I want to store some analytics data for the hits in my API, and, among the
data I need to extract for each request, I have: response status code (eg
200), the resolved path (eg /users/{user_id}/books) etc.
I imagined that the best place to do that is in a middleware, after the
response is computed:
@app.middleware("http")async def get_analytics_data(request: Request, call_next):
response = await call_next(request)
analytics_data = {
'response_code': response.status_code,
'path': ?
}
save(analytics_data)
return response
Inside the middleware I can access the request.url but it's not what I
want. The path as I define it in the routes is what I need.
Is there a way to obtain the path of the request in a single place? It
doesn't have to happen at the middleware level as long as it is in a single
place.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#486?email_source=notifications&email_token=AAINSPV2EJLDM5LX6MRFASLQHEQK7A5CNFSM4ISNQZIKYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4HIOSC7A>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAINSPX55YURAWAR6JSJ2BDQHEQK7ANCNFSM4ISNQZIA>
.
|
@euri10 , I've slightly updated the description hoping it's a bit more clear. |
A temporary (or not) solution for my need implies using So, to get the matched path of a request, I use this snippet in my middleware. ...
response = await call_next(request)
path = [route for route in request.scope['router'].routes if route.endpoint == request.scope['endpoint']][0].path
print(f'Path is: {path}')
# Path is: /users/{user_id}/books
return response The downside is that I have to iterate through all the routes for each request, but this can be improved quickly with a cache global variable. |
I'm glad you solved your problem @devtud ! You might also want to check how to add custom classes for |
Assuming the original issue was solved, it will be automatically closed now. But feel free to add more comments or create new issues. |
Is there a way to get the path before the
|
Hi, and how to figure out the path template if several routes are connected to a single endpoint? dummy example: @router.get("/x/{param1}/y/{param2}")
@router.get("/z/{param3}")
def endpoint(request: Request):
# here, how to figure out if it's "/x/{param1}/y/{param2}" or "/z/{param3}"? is regex my only option? |
@Kludex @gwaramadze @app.middleware("http")
async def do_something(request: Request, call_next):
path = request.scope['path'] # get the request route
if "xxx" in path:
do_something_before_path_operation()
response = await call_next(request)
if "xxx" in path:
do_something_after_path_operation()
return response Hope this helps. |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
Description
I want to store some analytics data for each hit in my API.
Among the data I need to extract for each request, I have:
/users/{user_id}/books
)I imagined that the best place to do that is in a middleware, after the response is computed:
Inside the middleware I can access the
request.url
but it's not what I want. The path as I define it in the routes is what I need.Is there a way to obtain the path of the request in a single place? It doesn't have to happen at the middleware level as long as it is in a single place.
EDIT:
Example:
Let's say I have defined:
/users/10/books
, so the path/users/{user_id}/books
is matched./users/{user_id}/books
).The text was updated successfully, but these errors were encountered: