新增api接口

This commit is contained in:
MarkusJoe
2022-04-09 10:55:27 +08:00
parent 9abc73962b
commit ec6ed77404
3 files changed
+125 -3

No files matched your search

+8 -1
View File
@@ -3,4 +3,11 @@
# @Author: markushammered@gmail.com # @Author: markushammered@gmail.com
# @Development Tool: PyCharm # @Development Tool: PyCharm
# @Create Time: 2022/4/9 # @Create Time: 2022/4/9
# @File Name: __init__.py.py # @File Name: __init__.py.py
from flask import Blueprint
api = Blueprint('api', __name__)
from . import views, errors
+26 -1
View File
@@ -3,4 +3,29 @@
# @Author: markushammered@gmail.com # @Author: markushammered@gmail.com
# @Development Tool: PyCharm # @Development Tool: PyCharm
# @Create Time: 2022/4/9 # @Create Time: 2022/4/9
# @File Name: errors.py # @File Name: errors.py
from . import api
from flask import jsonify
@api.errorhandler(404)
def page_not_found(e):
return jsonify(
{
'code': 404,
'msg': '页面未找到',
'data': None
}
), 404
@api.errorhandler(500)
def internal_server_error(e):
return jsonify(
{
'code': 500,
'msg': '服务器内部错误',
'data': None
}
), 500
+91 -1
View File
@@ -3,4 +3,94 @@
# @Author: markushammered@gmail.com # @Author: markushammered@gmail.com
# @Development Tool: PyCharm # @Development Tool: PyCharm
# @Create Time: 2022/4/9 # @Create Time: 2022/4/9
# @File Name: views.py # @File Name: views.py
from flask import jsonify
from flask import request
from ..db.db import SQLite as db
from . import api
@api.route('/overall/', methods=['GET', 'POST'])
def overall():
limit = request.args.get('limit', type=int)
try:
data = db().exec('select * from reqcount;')
format_data = []
for i in data:
format_data.append({'name': i[0], 'times': i[1]})
return jsonify(
{
'code': 200,
'msg': 'Success. Total: %s row(s)' % len(data),
'data': format_data[:limit]
}
), 200
except Exception as e:
return jsonify(
{
'code': -200,
'msg': 'Error',
'data': e
}
), 500
@api.route('/query/', methods=['GET', 'POST'])
def query():
name = request.args.get('name', type=str)
nochange = request.args.get('nochange', type=bool)
if nochange:
nochange = True
if not db().exists_name(name):
return jsonify(
{
'code': -200,
'msg': 'Failed. No such name',
'data': None
}
), 404
else:
data = db().fetch(name, nochange)
return jsonify(
{
'code': 200,
'msg': 'Success',
'data': data
}
), 200
@api.route('/theme/', methods=['GET', 'POST'])
def theme():
_theme = request.args.get('name', type=str)
if not db().exists_table(_theme):
return jsonify(
{
'code': 404,
'msg': 'Failed. No such theme',
'data': None
}
), 404
else:
data = db().fetching_table(_theme)
return jsonify(
{
'code': 200,
'msg': 'Success',
'data': data
}
), 200
@api.route('/alltables/', methods=['GET', 'POST'])
def all_tables():
data = db().show_tables
return jsonify(
{
'code': 200,
'msg': 'Success',
'data': data
}
), 200