Pass include and exclude lists to preprocessor and extract field_params from the request to personalise response #238
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I was writing an API and I wanted a little more control over the fields included in the response.
Currently Flask-Restless let you specify a global list of fields to include or exclude from the response. I wanted however the ability to differentiate the fields included in the get single from the get many for example.
At the moment the best way to achieve this is to remove the unwanted fields in the postprocessor. However this means that the app has to make queries and jsonify unwanted fields or relation which is not ideal for performance. Moreover there is no way to add particular fields in the postprocessor.
My solution was:
This in principle allow the user to personalise the response of the API for every method that accepts a preprocessor. It also has the advantages of keeping the current way to specify include and exclude fields unchanged which is good for coarse-grained tuning of the API.
I have also added parsing of an extra "fields" data in the get requests. This opens the possibility for the user of the API to specify which fields to include in the response from the HTTP request.
For example this request:
curl -G -H "Content-type: application/json" -d "fields=[\"name\"]" http://127.0.0.1:5000/api/computer
returns only the name field for every entry.
I'm relatively new to python so I'm not sure I implemented this in the best way. Feel free to pull it apart.