Open sarvesh4396 opened 1 year ago
Hi @sarvesh4396
My initial thoughts was using middleware to modify the body. I looked around a bit and found this issue:
https://github.com/encode/starlette/issues/495#issuecomment-494008175
It's definitely not easy to do, but I think it's possible.
We have a couple of ways of modifying the behaviour of PiccoloCRUD
. One is hooks, but probably not useful in this situation.
The other is validators. The intended purpose is to add extra validation to endpoints, but you may be able to use these to modify the request.
async def my_validator(piccolo_crud: PiccoloCRUD, request: Request):
json = await request.json()
request._json = {**json, 'custom_field': 'hello world'}
It might be worth giving that a go.
hey @dantownsend , I tried the above solution but it doesn't work cause the data is extracted before the validators run.
@sarvesh4396 OK, interesting. When you do await request.json()
does it just hang?
@dantownsend Yes, It does, query params can be changed through middleware but not the body.
For now, I have passed the modify_body
arg for a quick patch it is working fine.
@sarvesh4396 OK, cool. Do you think this is something we could write a custom hook for?
@dantownsend I didn't get it, Do you mean by Hooks which are there in piccolo-api? or something else.
Though I believe we could write a function for it. and we can run it wherever we are doing await request.json()
such that we do not have to await again and again which may hamper performance.
accessing the request body in middleware has been an issue I couldn't find anything yet which can resolve it.
@sarvesh4396 Yeah, we have hooks for PiccoloAPI like pre_save
. Could add a new one maybe? 🤔
In our CSRFMiddleware
we access the request body, but then add it to the ASGI scope. This works OK, but the downstream app needs to know to look in the scope for the data.
@dantownsend, It should work ideally, as we can receive the request Object in hooks.
Currently, PiccoloCRUD exposes all endpoints and we can not change them internally. If we try to add middleware we can achieve that for query parameters but it can not be done for the body.
Proposed Solution
Add two callable parameters in PiccoloCRUD for manipulation of the body and parameters.
And we can pass
headers
,query
withmodify_query_params
headers
,body
withmodify_body
Then we can update the
dict
at desired placesUse Cases
It will give the ability to manipulate the query operations in the desired way for example a notes app
If we want to add a note for that user without passing the user_id. If we are using
jwt
then we can get the user_id from theAuthorization
header and pass it into the body also, It will enable us to get all the notes from that user without passing that param explicitly.@dantownsend I can do a PR, if the idea is helpful.