Move api_wrapper to decorators.py

This commit is contained in:
James Wang 2015-12-26 19:19:31 -05:00
parent 08b9a84c84
commit 1e911c3f20
6 changed files with 23 additions and 26 deletions

View file

@ -1,4 +1,4 @@
from flask import Blueprint from flask import Blueprint
from utils import api_wrapper from decorators import api_wrapper
blueprint = Blueprint("admin", __name__) blueprint = Blueprint("admin", __name__)

View file

@ -19,3 +19,20 @@ def check_csrf(f):
def wrapper(*args, **kwds): def wrapper(*args, **kwds):
return f(*args, **kwds) return f(*args, **kwds)
return wrapper return wrapper
def api_wrapper(f):
@wraps(f)
def wrapper(*args, **kwds):
web_result = {}
response = 200
try:
web_result = f(*args, **kwds)
except WebException as error:
response = 200
web_result = { "success": 0, "message": str(error) }
except Exception as error:
response = 200
traceback.print_exc()
web_result = { "success": 0, "message": "Something went wrong! Please notify us about this immediately.", error: traceback.format_exc() }
return json.dumps(web_result), response, { "Content-Type": "application/json; charset=utf-8" }
return wrapper

View file

@ -2,7 +2,7 @@ from flask import Blueprint, session, request
from flask import current_app as app from flask import current_app as app
from models import db, Users from models import db, Users
from utils import api_wrapper from decorators import api_wrapper
import logging import logging
import requests import requests

View file

@ -24,20 +24,3 @@ def unix_time_millis(dt):
def get_time_since_epoch(): def get_time_since_epoch():
return unix_time_millis(datetime.datetime.now()) return unix_time_millis(datetime.datetime.now())
def api_wrapper(f):
@wraps(f)
def wrapper(*args, **kwds):
web_result = {}
response = 200
try:
web_result = f(*args, **kwds)
except WebException as error:
response = 200
web_result = { "success": 0, "message": str(error) }
except Exception as error:
response = 200
traceback.print_exc()
web_result = { "success": 0, "message": "Something went wrong! Please notify us about this immediately.", error: traceback.format_exc() }
return json.dumps(web_result), response, { "Content-Type": "application/json; charset=utf-8" }
return wrapper

View file

@ -5,9 +5,6 @@ import config
import json import json
import api import api
from api.api import api as api_blueprint
from api.user import blueprint as user_blueprint
app = Flask(__name__) app = Flask(__name__)
with app.app_context(): with app.app_context():