2021-06-09 14:23:48 +08:00
|
|
|
import traceback
|
2021-06-05 10:25:34 +08:00
|
|
|
from utils.util_log import test_log as log
|
|
|
|
|
|
|
|
|
|
|
|
class Error:
|
|
|
|
def __init__(self, error):
|
2021-06-07 15:21:36 +08:00
|
|
|
self.code = getattr(error, 'code', -1)
|
2021-06-05 10:25:34 +08:00
|
|
|
self.message = getattr(error, 'message', str(error))
|
|
|
|
|
|
|
|
|
2021-07-05 19:14:03 +08:00
|
|
|
log_row_length = 300
|
2021-06-09 16:19:48 +08:00
|
|
|
|
|
|
|
|
2021-06-05 10:25:34 +08:00
|
|
|
def api_request_catch():
|
|
|
|
def wrapper(func):
|
|
|
|
def inner_wrapper(*args, **kwargs):
|
|
|
|
try:
|
|
|
|
res = func(*args, **kwargs)
|
2021-07-28 19:35:22 +08:00
|
|
|
res_str = str(res)
|
|
|
|
log_res = res_str[0:log_row_length] + '......' if len(res_str) > log_row_length else res_str
|
2021-07-05 19:14:03 +08:00
|
|
|
log.debug("(api_response) : %s " % log_res)
|
2021-06-05 10:25:34 +08:00
|
|
|
return res, True
|
|
|
|
except Exception as e:
|
2021-07-28 19:35:22 +08:00
|
|
|
e_str = str(e)
|
|
|
|
log_e = e_str[0:log_row_length] + '......' if len(e_str) > log_row_length else e_str
|
2021-06-09 14:23:48 +08:00
|
|
|
log.error(traceback.format_exc())
|
2021-07-05 19:14:03 +08:00
|
|
|
log.error("(api_response) : %s" % log_e)
|
2021-06-05 10:25:34 +08:00
|
|
|
return Error(e), False
|
|
|
|
return inner_wrapper
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
|
|
|
@api_request_catch()
|
|
|
|
def api_request(_list, **kwargs):
|
|
|
|
if isinstance(_list, list):
|
|
|
|
func = _list[0]
|
|
|
|
if callable(func):
|
|
|
|
arg = []
|
|
|
|
if len(_list) > 1:
|
|
|
|
for a in _list[1:]:
|
|
|
|
arg.append(a)
|
2021-07-28 19:35:22 +08:00
|
|
|
arg_str = str(arg)
|
|
|
|
log_arg = arg_str[0:log_row_length] + '......' if len(arg_str) > log_row_length else arg_str
|
2021-07-05 19:14:03 +08:00
|
|
|
log.debug("(api_request) : [%s] args: %s, kwargs: %s" % (func.__qualname__, log_arg, str(kwargs)))
|
2021-06-05 10:25:34 +08:00
|
|
|
return func(*arg, **kwargs)
|
|
|
|
return False, False
|
2021-06-11 10:02:50 +08:00
|
|
|
|