统一response结构

This commit is contained in:
chenzuoqing 2021-12-01 14:23:58 +08:00
parent b2e7e18f8a
commit 9f50ff389c
3 changed files with 26 additions and 4 deletions

View File

@ -5,6 +5,9 @@ BASE_DIR = os.path.dirname(os.path.abspath(__file__))
SECRET_KEY = 'cf036e50524a11ecb4a618c04d9eb245' SECRET_KEY = 'cf036e50524a11ecb4a618c04d9eb245'
JSONIFY_PRETTYPRINT_REGULAR = False
RESTFUL_JSON = {'separators': (',', ':'), 'indent': 0}
# mongodb 地址 # mongodb 地址
MONGODB_SETTINGS = { MONGODB_SETTINGS = {
'host': 'mongodb://admin:111111@10.2.2.10:27017/ops_api?authSource=admin', 'host': 'mongodb://admin:111111@10.2.2.10:27017/ops_api?authSource=admin',

18
src/common/utils.py Normal file
View File

@ -0,0 +1,18 @@
from flask import jsonify
def make_response(status, code, msg, **kwargs):
"""返回一个错误,响应内容是 `json`
:param status: http 状态
:param code: 操作状态码通常 `1000` 是成功
:param msg: 消息内容
:param kwargs: 其他需要写入响应的内容注意需要 `json` 可以解析
"""
content = {
"code": code,
"msg": msg,
}
content.update(**kwargs)
response = jsonify(content)
response.status_code = status
return response

View File

@ -1,13 +1,14 @@
import datetime import datetime
from flask import current_app as app from flask import current_app as app
from flask_restful import reqparse, abort from flask_restful import reqparse
from asset.models import Host from asset.models import Host
from project import fields from project import fields
from project.models import Project, Channel, Server, Version from project.models import Project, Channel, Server, Version
from common.views import ListMixin, CreateMixin, ListCreateViewSet, DetailViewSet from common.views import ListMixin, CreateMixin, ListCreateViewSet, DetailViewSet
from common.permission import token_header_required from common.permission import token_header_required
from common.utils import make_response
class ProjectViews(ListMixin, CreateMixin): class ProjectViews(ListMixin, CreateMixin):
@ -93,7 +94,7 @@ class ServerSyncView(CreateMixin):
data = args["data"] data = args["data"]
if count != len(data): if count != len(data):
abort(400, msg="quantity mismatch", code=1001) return make_response(400, 1001, "quantity mismatch")
hosts = {} hosts = {}
channels = {} channels = {}
@ -140,6 +141,6 @@ class ServerSyncView(CreateMixin):
continue continue
if errors: if errors:
return {"msg": "有区服同步出错!", "code": 1020, "errors": errors} return make_response(200, 1020, "有区服同步出错", errors=errors)
return {"msg": "ok", "code": 1000} return make_response(200, 1000, "success")