Я пытаюсь использовать колбу внутри класса, но я бы хотел использовать декораторы
Я видел эту тему Использование колбы внутри класса
Но пока все хорошо, нет использования декораторов внутри, если нет выбора, я буду использовать это решение.
На данный момент мой код выглядит так:
class DM():
def __init__(self, path=""):
self.cors = CORS(self.app, resources={r"/*": {"origins": "*"}})
self.host = "0.0.0.0"
self.port = 9001
class app(Flask):
pass
def PrintException(self):
exc_type, exc_obj, tb = sys.exc_info()
f = tb.tb_frame
lineno = tb.tb_lineno
filename = f.f_code.co_filename
linecache.checkcache(filename)
line = linecache.getline(filename, lineno, f.f_globals)
return 'EXCEPTION IN ({}, LINE {} "{}"): {}'.format(
filename, lineno, line.strip(), exc_obj
)
@app.route("/product", methods=["POST", "GET", "OPTION"])
def addProduct(self):
try:
data = request.data
document = json.loads(data.decode("utf-8"))
return jsonify({"status":"ok","_id":str(document)})
except Exception as e:
return jsonify({"status": "ko", "exception": self.PrintException() + " " + str(e),"document":document})
@app.after_request
def after_request(self,response):
response.headers.add("Access-Control-Allow-Origin", "*")
response.headers.add("Access-Control-Allow-Headers", "Content-Type,Authorization")
response.headers.add("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE,OPTIONS")
return response
def run(self):
self.app.run(self.host, self.port)
но у меня есть эта ошибка:
@app.route("/product", methods=["POST", "GET", "OPTION"])
TypeError: route() missing 1 required positional argument: 'rule'
edit:
И я не могу использовать себя в декораторах:
class DM():
def __init__(self, path=""):
self.app = Flask(__name__)
self.cors = CORS(self, resources={r"/*": {"origins": "*"}})
self.host = "0.0.0.0"
self.port = 9001
def PrintException(self):
exc_type, exc_obj, tb = sys.exc_info()
f = tb.tb_frame
lineno = tb.tb_lineno
filename = f.f_code.co_filename
linecache.checkcache(filename)
line = linecache.getline(filename, lineno, f.f_globals)
return 'EXCEPTION IN ({}, LINE {} "{}"): {}'.format(
filename, lineno, line.strip(), exc_obj
)
@app.route("/product", methods=["POST", "GET", "OPTION"])
def addProduct(self):
try:
data = request.data
document = json.loads(data.decode("utf-8"))
return jsonify({"status":"ok","_id":str(document)})
except Exception as e:
return jsonify({"status": "ko", "exception": self.PrintException() + " " + str(e),"document":document})
@app.after_request
def after_request(self,response):
response.headers.add("Access-Control-Allow-Origin", "*")
response.headers.add("Access-Control-Allow-Headers", "Content-Type,Authorization")
response.headers.add("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE,OPTIONS")
return response
def run(self):
self.app.run(self.host, self.port)
С уважением