guarantee-admin-api-v0.2/Utils/RouteUtil.py

36 lines
1.0 KiB
Python
Raw Normal View History

2022-05-24 02:25:59 +08:00
from Utils.ErrorUtil import APIReturnError
2022-05-26 17:15:30 +08:00
class RouteParamsCheck(object):
2022-05-24 02:25:59 +08:00
"""路由工具"""
2022-05-26 17:15:30 +08:00
def __init__(self, req, params):
self.req = req
self.params = params
2022-05-30 16:56:01 +08:00
if not isinstance(self.req, dict):
assert False, "参数异常"
2022-05-26 17:15:30 +08:00
def required(self):
2022-05-24 02:25:59 +08:00
"""必需参数检查"""
2022-05-26 17:15:30 +08:00
if not isinstance(self.params, list):
2022-05-30 16:56:01 +08:00
assert False, "参数异常"
2022-05-24 02:25:59 +08:00
2022-05-26 17:15:30 +08:00
params_in_req_body = list(self.req.keys())
for param in self.params:
2022-05-24 02:25:59 +08:00
if param not in params_in_req_body:
error_info = "缺失必需参数: {}".format(param)
raise APIReturnError(error_info=error_info, status_code=200)
2022-05-30 16:56:01 +08:00
def enum(self):
"""枚举类型检查"""
if not isinstance(self.params, dict):
assert False, "参数异常"
for k, v in self.params.items():
if self.req[k] not in v:
error_info = "参数值错误: {}".format(k)
raise APIReturnError(error_info=error_info, status_code=200)