(no title)
ziikutv | 7 years ago
# In this example, I use blueprint plugin
@bp.route('/', methods=['PUT'])
def create_resource():
return
vs class Resource(...):
def on_put(self, request, response):
return
Mirror: https://pastebin.com/50W8TJV2Flask's hooking logic [1] is nice but I much prefer Falcon's middleware logic [2]. This then ties into the fact that Flask's design requires working with `g` [3] (essentially a global object) where as the middleware design leads us to bake in data in the request object. The example blow demonstrates how middleware definition works and how "adding data" per request works in both of these frameworks.
# Flask; assuume `app` exists
from flask import g
@app.before_request
def before_request():
g.some_variable = 'foobar'
vs class ExampleComponent(object):
def process_request(self, req, resp):
req.some_variable = 'foobar'
Mirror: https://pastebin.com/Z3yGCfzFAdditionally, Falcon middleware API also exposes `process_resource` which is similar to above but also allows us to _pass_ arguments to the routed resource instance.
[1] http://flask.pocoo.org/docs/1.0/api/#flask.Flask.before_requ...
[2] https://falcon.readthedocs.io/en/stable/api/middleware.html
[3] http://flask.pocoo.org/docs/1.0/appcontext/#storing-data
Edit: Formatting
No comments yet.