所有HTTP、MQ、MQTT、WebSocket等数据交流方式皆采用json数据格式,格式如下
{
"code": int, # 错误码,详见
"msg": str, # 如果报错,可直接显示的报错信息(兼容多语言本地化)
"data": json # 返回的数据内容或者异常数组
}
错误码定义
| 组成 | 1位 | 1位 | 0-9 | 0-9 | 0-9 | 0-9 | 0-9 | 0-9 | 0-9 | 0-9 | 0-9 |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 定义 | 返回类型 | 来源 | |||||||||
| 取值范围 | 0~90~6:数据返回7~9:数据请求 |
0~9 |
|||||||||
| 说明 | 返回:0:正确返回 1~5:错误日志级别数据请求: 7:数据传输,不需要返回数据 8:数据请求,需要返回数据 9:服务器主动请求,需要客户端返回数据 预留: 6 |
0:未知来源 1:数据类型(数据库查询、Redis等) 2:定时任务 3:事件触发类 其他预留 |
|||||||||
| 求值方法 | get_code(code,1) |
get_code(code,2) |
def get_code(code: str or int, begin: int, count: int = 1, code_len: int = 9) -> int:
"""
获取返回码中指定位数的数值
Args:
code (str, optional): 复合返回码
begin (str, optional): 从左至数第几位,从1开始
count (str, optional): 取几位数
code_len (int, optional): 复制返回码的长度,包含前导0. Defaults to 9
Returns:
int: 以整数形式返回
"""
code = code if isinstance(code, int) else int(code)
# 检查参数,如果不符合规则,则触发参数错误异常
if code_len < len(str(code)) or code_len <= 0:
raise ValueError('Parameter `code` is invalid.')
# 同上
if begin + count - 1 > code_len:
raise ValueError('Parameter `begin` or `count` is invalid.')
# 同上
if not (code_len>=begin>0 and code_len>=count>0):
raise ValueError('Parameters conflict.')
return code // 10**(code_len - begin - count + 1) % 10**count
返回值约定(持续更新)
| 1-1000 | 系统错误 | 数据库异常、Redis异常、 |
HTTP API规范
- 预料中的返回禁止使用200以外的HTTP状态码;
- 所有数据交流都使用json格式;
Websocket API规范
- 禁止高频发送重复数据;
作者:李拜天 创建时间:2022-03-26 00:17
最后编辑:李拜天 更新时间:2022-03-27 19:44
最后编辑:李拜天 更新时间:2022-03-27 19:44