From ec6ed774044f1fc77323b0ffc637d815ccdb2873 Mon Sep 17 00:00:00 2001 From: MarkusJoe Date: Sat, 9 Apr 2022 10:55:27 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Eapi=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/__init__.py | 9 ++++- app/api/errors.py | 27 ++++++++++++- app/api/views.py | 92 ++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 125 insertions(+), 3 deletions(-) diff --git a/app/api/__init__.py b/app/api/__init__.py index 739a3f0..37e97bc 100644 --- a/app/api/__init__.py +++ b/app/api/__init__.py @@ -3,4 +3,11 @@ # @Author: markushammered@gmail.com # @Development Tool: PyCharm # @Create Time: 2022/4/9 -# @File Name: __init__.py.py \ No newline at end of file +# @File Name: __init__.py.py + + +from flask import Blueprint + +api = Blueprint('api', __name__) + +from . import views, errors diff --git a/app/api/errors.py b/app/api/errors.py index 6041600..98dc2e4 100644 --- a/app/api/errors.py +++ b/app/api/errors.py @@ -3,4 +3,29 @@ # @Author: markushammered@gmail.com # @Development Tool: PyCharm # @Create Time: 2022/4/9 -# @File Name: errors.py \ No newline at end of file +# @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 diff --git a/app/api/views.py b/app/api/views.py index f8b7dbf..1625d8e 100644 --- a/app/api/views.py +++ b/app/api/views.py @@ -3,4 +3,94 @@ # @Author: markushammered@gmail.com # @Development Tool: PyCharm # @Create Time: 2022/4/9 -# @File Name: views.py \ No newline at end of file +# @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