-
Notifications
You must be signed in to change notification settings - Fork 0
/
responses.py
299 lines (231 loc) · 8.2 KB
/
responses.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# -*- coding: utf-8 -*-
"""
@author: Yiğit GÜMÜŞ
This module provides a set of utility functions for generating JSON responses with different statuses and messages.
The `Responses` class provides static methods for generating JSON responses with different statuses and messages.
The statuses and messages can be customized according to the needs of the application.
The class provides methods for generating responses with the following statuses:
- OK!
- Internal Server Error!
- Service Unavailable!
- Already Exist!
- Too Large File Upload!
- Unauthorized!
- Page Not Found!
- Forbidden!
Each method takes a message and an optional error as arguments and returns a `Response` object.
Example usage:
```python
from flask import Flask, jsonify
from responses import Responses
app = Flask(__name__)
@app.route('/ok')
def ok_route():
return Responses.ok('Everything is fine')
@app.route('/internal_server_error')
def internal_server_error_route():
return Responses.internal_server_err('Something went wrong', Exception('Something went wrong'))
if __name__ == '__main__':
app.run()
"""
from flask import (
make_response,
Response,
jsonify
)
from typing import Any
class Responses:
"""
A class that provides static methods for generating JSON responses with different statuses and messages.
This class provides a set of static methods that can be used to generate JSON responses with different statuses
and messages. The statuses and messages can be customized according to the needs of the application.
The class provides methods for generating responses with the following statuses:
- OK!
- Internal Server Error!
- Service Unavailable!
- Already Exist!
- Too Large File Upload!
- Unauthorized!
- Page Not Found!
- Forbidden!
Each method takes a message and an optional error as arguments and returns a `Response` object.
Example usage:
```
from flask import Flask, jsonify
from responses import Responses # This file
app = Flask(__name__)
@app.route('/ok')
def ok_route():
return Responses.ok('Everything is fine')
@app.route('/internal_server_error')
def internal_server_error_route():
return Responses.internal_server_err('Something went wrong', Exception('Something went wrong'))
if __name__ == '__main__':
app.run()
```
"""
@staticmethod
def ok(msg: str | dict[str, Any]) -> Response:
"""
Creates a JSON response with a status of 'OK!' and the provided message.
Args:
msg (str or dict[str, Any]): The message to include in the response.
Returns:
Response: The JSON response with the provided message.
"""
return make_response(
jsonify({
"status": 'OK!',
"msg": msg,
}),
200
)
@staticmethod
def internal_server_err(msg: str, err: Exception) -> Response:
"""
Creates a JSON response with a status of 'Internal Server Error!' and the provided message and error.
Args:
msg (str): The message to include in the response.
err (Exception): The error that occurred.
Returns:
Response: The JSON response with the provided message and error.
"""
return make_response(
jsonify({
"status": 'Internal Server Error!',
"msg": msg,
"error": str(err)
}),
500
)
@staticmethod
def OK(msg: str) -> Response:
"""
Creates a JSON response with a status of 'OK!' and the provided message.
Args:
msg (str): The message to include in the response.
Returns:
Response: The JSON response with the provided message.
"""
return Response.ok(msg)
@staticmethod
def iserr(msg: str, err: Exception) -> Response:
"""
Shortcut for internal server error.
Args:
msg (str): The error message.
err (Exception): The exception that occurred.
Returns:
Response: The response object representing the internal server error.
"""
return Responses.internal_server_err(msg, err)
@staticmethod
def service_unavailable(msg: str, err: Exception) -> Response:
"""
Returns a Flask Response object with a JSON payload containing the provided message and error,
indicating that the service is unavailable.
Args:
msg (str): The message to include in the response.
err (Exception): The error that occurred.
Returns:
Response: The JSON response with the provided message and error.
"""
return make_response(
jsonify({
"status": 'Service Unavailable!',
"msg": msg,
"error": str(err)
}),
500
)
@staticmethod
def already_exist(msg: str, err: Exception) -> Response:
"""
Returns a Flask Response object with a JSON payload containing the provided message and error,
indicating that the resource already exists.
Args:
msg (str): The message to include in the response.
err (Exception): The error that occurred.
Returns:
Response: The JSON response with the provided message and error.
"""
return make_response(
jsonify({
"status": 'Already Exist!',
"msg": msg,
"error": str(err)
}),
202
)
@staticmethod
def too_large(msg: str, err: Exception) -> Response:
"""
Generates a JSON response with a status of 'Too Large File Upload!', a message, and an error.
Parameters:
msg (str): The message to be included in the response.
err (Exception): The error that occurred.
Returns:
Response: The JSON response with the specified status, message, and error.
"""
return make_response(
jsonify({
"status": 'Too Large File Upload!',
"msg": msg,
"error": str(err)
}),
413
)
@staticmethod
def unauthorized(msg: str, err: Exception) -> Response:
"""
Generates a JSON response with a status of 'Unauthorized!', a message, and an error.
Parameters:
msg (str): The message to be included in the response.
err (Exception): The error that occurred.
Returns:
Response: The JSON response with the specified status, message, and error.
"""
return make_response(
jsonify({
"status": 'Unauthorized!',
"msg": msg,
"error": str(err)
}),
401
)
@staticmethod
def not_found(msg: str, err: Exception) -> Response:
"""
Generates a JSON response with a 'Not Found!' status, a custom message, and the error message.
Parameters:
msg (str): The custom message to be included in the response.
err (Exception): The error that occurred.
Returns:
Response: The JSON response with the 'Not Found!' status, the custom message, and the error message.
"""
return make_response(
jsonify({
"status": 'Not Found!',
"msg": msg,
"error": str(err)
}),
404
)
@staticmethod
def forbidden(msg: str, err: Exception) -> Response:
"""
Generates a JSON response with a status of 'Forbidden!', a custom message, and the error message.
Parameters:
msg (str): The custom message to be included in the response.
err (Exception): The error that occurred.
Returns:
Response: The JSON response with the 'Forbidden!' status, the custom message, and the error message.
"""
return make_response(
jsonify({
"status": 'Forbidden!',
"msg": msg,
"error": str(err)
}),
403
)