ApiTemplate.py
1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# coding=utf-8
#author: 4N
#createtime: 2021/5/18
#email: nheweijun@sina.com
from app.util.component.ParameterUtil import ParameterUtil
from flask import current_app,request
import traceback
import json
from app.modules.auth.models import OAuth2Token,User,db
import app
class ApiTemplate:
#模板方法
para = dict()
api_doc = dict()
api_name="[未命名]"
def __init__(self):
pass
def get_para(self):
self.para = ParameterUtil.get_parameter()
token = request.headers.get('Authorization')
if self.para.get("token"):
token = self.para.get("token")
if token:
token = token.split(" ")[-1]
if app.GLOBAL_DIC.get(token):
self.para["creator"] = app.GLOBAL_DIC.get(token)
else:
user = User.query.join(OAuth2Token).filter(OAuth2Token.access_token == token).one_or_none()
if user:
self.para["creator"] = user.username
app.GLOBAL_DIC[token] = user.username
def para_check(self):
pass
def process(self):
pass
def log(self):
ip = request.remote_addr
current_app.logger.info("[{}][{}]调用了:{},参数为:{}".format(ip,self.para.get("creator","非法用户"),self.api_name,json.dumps(self.para,ensure_ascii=False)))
# 接口模板
@property
def result(self):
try:
self.get_para()
self.para_check()
self.log()
res = self.process()
except Exception as e:
res=dict()
res["result"]=False
res["msg"]=e.__str__()
current_app.logger.error(e.__str__()+":"+ traceback.format_exc())
return res