Skip to content
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

Pass include and exclude lists to preprocessor and extract field_params from the request to personalise response #238

Closed
wants to merge 2 commits into from

Conversation

dedalusj
Copy link

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:

  1. At the moment the API is created, global versions of the include and exclude lists are created.
  2. For every request whose output can be personalised, local copies of the include and exclude lists are created and passed to the preprocessors
  3. The preprocessor receives the lists of the include and exclude columns and the user has the ability to remove or add fields to the lists in-place. This is similar to the way search_params currently works.
  4. The response is generated from this user modified copies of the include and exclude lists

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.

@Svenito
Copy link

Svenito commented Sep 19, 2013

This is interesting, as I have approached this in a different way. I have added the ability to exclude/include relations and columns from instances at creation level. Here's the commit that adds this feature https://github.com/Svenito/flask-restless/commit/2f4e462b81e67624af889fcd633cbdef837ae5ef

Not sure which is better in the end, but I certainly like the idea of having this data in the pre-processor and also being able to specify the fields in the request

@jfinkels
Copy link
Owner

This was a solution for issue #87. The code has changed (i.e. improved) quite a bit since this pull request; I have provided a better solution in the comments in issue #87 using a custom serializer class.

@jfinkels jfinkels closed this Apr 28, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants