forked from 1Panel-dev/MaxKB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresult.py
165 lines (139 loc) · 9.38 KB
/
result.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
from typing import List
from django.http import JsonResponse
from drf_yasg import openapi
from rest_framework import status
class Page(dict):
"""
分页对象
"""
def __init__(self, total: int, records: List, current_page: int, page_size: int, **kwargs):
super().__init__(**{'total': total, 'records': records, 'current': current_page, 'size': page_size})
class Result(JsonResponse):
"""
接口统一返回对象
"""
def __init__(self, code=200, message="成功", data=None, response_status=status.HTTP_200_OK, **kwargs):
back_info_dict = {"code": code, "message": message, 'data': data}
super().__init__(data=back_info_dict, status=response_status, **kwargs)
def get_page_request_params(other_request_params=None):
if other_request_params is None:
other_request_params = []
current_page = openapi.Parameter(name='current_page',
in_=openapi.IN_PATH,
type=openapi.TYPE_INTEGER,
required=True,
description='当前页')
page_size = openapi.Parameter(name='page_size',
in_=openapi.IN_PATH,
type=openapi.TYPE_INTEGER,
required=True,
description='每页大小')
result = [current_page, page_size]
for other_request_param in other_request_params:
result.append(other_request_param)
return result
def get_page_api_response(response_data_schema: openapi.Schema):
"""
获取统一返回 响应Api
"""
return openapi.Responses(responses={200: openapi.Response(description="响应参数",
schema=openapi.Schema(
type=openapi.TYPE_OBJECT,
properties={
'code': openapi.Schema(
type=openapi.TYPE_INTEGER,
title="响应码",
default=200,
description="成功:200 失败:其他"),
"message": openapi.Schema(
type=openapi.TYPE_STRING,
title="提示",
default='成功',
description="错误提示"),
"data": openapi.Schema(
type=openapi.TYPE_OBJECT,
properties={
'total': openapi.Schema(
type=openapi.TYPE_INTEGER,
title="总条数",
default=1,
description="数据总条数"),
"records": openapi.Schema(
type=openapi.TYPE_ARRAY,
items=response_data_schema),
"current": openapi.Schema(
type=openapi.TYPE_INTEGER,
title="当前页",
default=1,
description="当前页"),
"size": openapi.Schema(
type=openapi.TYPE_INTEGER,
title="每页大小",
default=10,
description="每页大小")
}
)
}
),
)})
def get_api_response(response_data_schema: openapi.Schema):
"""
获取统一返回 响应Api
"""
return openapi.Responses(responses={200: openapi.Response(description="响应参数",
schema=openapi.Schema(
type=openapi.TYPE_OBJECT,
properties={
'code': openapi.Schema(
type=openapi.TYPE_INTEGER,
title="响应码",
default=200,
description="成功:200 失败:其他"),
"message": openapi.Schema(
type=openapi.TYPE_STRING,
title="提示",
default='成功',
description="错误提示"),
"data": response_data_schema
}
),
)})
def get_default_response():
return get_api_response(openapi.Schema(type=openapi.TYPE_BOOLEAN))
def get_api_array_response(response_data_schema: openapi.Schema):
"""
获取统一返回 响应Api
"""
return openapi.Responses(responses={200: openapi.Response(description="响应参数",
schema=openapi.Schema(
type=openapi.TYPE_OBJECT,
properties={
'code': openapi.Schema(
type=openapi.TYPE_INTEGER,
title="响应码",
default=200,
description="成功:200 失败:其他"),
"message": openapi.Schema(
type=openapi.TYPE_STRING,
title="提示",
default='成功',
description="错误提示"),
"data": openapi.Schema(type=openapi.TYPE_ARRAY,
items=response_data_schema)
}
),
)})
def success(data, **kwargs):
"""
获取一个成功的响应对象
:param data: 接口响应数据
:return: 请求响应对象
"""
return Result(data=data, **kwargs)
def error(message):
"""
获取一个失败的响应对象
:param message: 错误提示
:return: 接口响应对象
"""
return Result(code=500, message=message)