接口规范总结
1.view层接口
1.1 函数式实现
"""
1.接口的请求方式有POST,GET
2.接口的操作有增删改查
3.对不同请求方式的接口做验证
4.根据接口的操作方式对接口命名
5.对接口传入的参数进行校验
6.接口返回值
{
"code":xx,
"msg":"xxx",
"data":[]
}
响应码:
200 返回成功,
400 返回请求错误
500 返回服务器内部错误
返回值信息
msg: 记录异常的信息
参数校验
定义各种异常的全局状态码
"""
def add_xx(request):
# model 层 insert
result = {
"data": [],
"msg": "success",
"status": 200
}
try:
if request.method == "POST":
try:
# 参数校验
params = check_params()
if params:
res = MODEL.fun()
reslut["data"] = res
return HttpResponse(json.dumps(result), content_type="application/json", status=200)
else:
result["msg"] = params["msg"]
result["status"] = params["retCode"]
except Exception,e:
logger.error(e)
logger.error(traceback.print_exc())
result["msg"] = traceback.print_exc()
result["status"] = 400
else:
result["msg"] = "不支持的请求方式"
result["status"] = 400
return HttpResponse(json.dumps(result), content_type="application/json", status=400)
except Exception as e:
logger.error(e)
logger.error(traceback.print_exc())
result["msg"] = "请求处理发生错误,请稍后重试"
result["status"] = 500
return HttpResponse(json.dumps(result), content_type="application/json", status=500)
def del_xx():
# model 层 delete
def edit_xx():
# model 层 update
def get_xx():
# model 层 query
1.2 rest接口实现
1.3 类实现
2.model层接口