I am trying to implement an interceptor to intercept request coming in an end point. In this interceptor, I need to access headers before applying function. This interceptor should work with flask, django or any other frame work. Here I have written an example interceptor which is dependent on flask. Can any body suggest me how can I make it generic so that it can run with any web framework?
from flask import Flask, request, make_response, jsonify
from functools import wraps
app = Flask(__name__)
def interceptor2(func):
print('this is executed at function definition time (def my_func)')
@wraps(func)
def wrapper(*args, **kwargs):
print('this is executed before function call interceptor')
print(request.headers) # accessing Headers request from flask, making this code dependent on flask
# print(**kwargs)
result = func(*args, **kwargs)
print('this is executed after function call interceptor')
return result
return wrapper
@app.route('/', methods=['GET', 'POST'])
@interceptor2
def hello():
# print(request.headers)
return make_response(jsonify({'resp': " ok done"}), 200)
if __name__ == "__main__":
app.run('127.0.0.1', '5000', debug=True)
How can I write generic interceptor which can run with any web framework?