jfinkels / flask-restless

NO LONGER MAINTAINED - A Flask extension for creating simple ReSTful JSON APIs from SQLAlchemy models.
https://flask-restless.readthedocs.io
GNU Affero General Public License v3.0
1.02k stars 301 forks source link

more contexts in pre/postprocessors #619

Open tarekziade opened 7 years ago

tarekziade commented 7 years ago

It would be great to have more context when a pre/postprocessor function is called.

Having access to the model class, and other info parsed by the manager would be very useful.

my use case is to implement the If-Modified header so I need to query the model and check a requests header before allowing a data change.

Happy to build a PR for this if needed

jfinkels commented 7 years ago

Hi @tarekziade! This is probably a good idea. Access to the model class is an easy win. What other information might a preprocessor function need?

My concern is that after adding a new keyword argument for the preprocessor, (then another, then another...), we may end up with an unwieldy user interface. Perhaps it makes sense to collect the information into a single object, maybe a context dictionary or object, that has a key for each of the keyword arguments that the preprocessors currently accept. Then the preprocessor function just modifies the context in-place, and the Flask-Restless view function extracts the info it needs from the context object after the preprocessor returns. I think I should open a separate issue for that, however.

tarekziade commented 7 years ago

What other information might a preprocessor function need?

Maybe the SA session and instance ?

maybe a context dictionary or object

Sounds like a good idea. Another option would be to pass the API instance directly maybe. Since it has all the mentioned info + more, like the primary_key etc

tarekziade commented 7 years ago

@jfinkels One quick hack that does not require an API change is to add the API instance in flask.g

tarekziade commented 7 years ago

This is my ugly and lame hack to make it work - I add in flask.g the api class :

https://github.com/mozilla/servicebook/blob/master/servicebook/server.py#L19

Then my postprocessor can implement the 412 & 428 :

https://github.com/mozilla/servicebook/blob/master/servicebook/server.py#L49