Coverage for model/utils/response.py: 100%
22 statements
« prev ^ index » next coverage.py v7.3.2, created at 2024-11-05 04:22 +0000
« prev ^ index » next coverage.py v7.3.2, created at 2024-11-05 04:22 +0000
1from flask import jsonify, redirect
3__all__ = ['HTTPResponse', 'HTTPRedirect', 'HTTPError']
6class HTTPBaseResponese(tuple):
8 def __new__(
9 cls,
10 resp,
11 status_code=200,
12 cookies={},
13 ):
14 for c in cookies:
15 if cookies[c] == None:
16 resp.delete_cookie(c)
17 else:
18 d = c.split('_httponly')
19 resp.set_cookie(d[0], cookies[c], httponly=bool(d[1:]))
20 return super().__new__(tuple, (resp, status_code))
23class HTTPResponse(HTTPBaseResponese):
25 def __new__(
26 cls,
27 message='',
28 status_code=200,
29 status='ok',
30 data=None,
31 cookies={},
32 ):
33 resp = jsonify({
34 'status': status,
35 'message': message,
36 'data': data,
37 })
38 return super().__new__(
39 HTTPBaseResponese,
40 resp,
41 status_code,
42 cookies,
43 )
46class HTTPRedirect(HTTPBaseResponese):
48 def __new__(
49 cls,
50 location,
51 status_code=302,
52 cookies={},
53 ):
54 resp = redirect(location)
55 return super().__new__(
56 HTTPBaseResponese,
57 resp,
58 status_code,
59 cookies,
60 )
63class HTTPError(HTTPResponse):
65 def __new__(
66 cls,
67 message,
68 status_code,
69 data=None,
70 logout=False,
71 ):
72 cookies = {'piann': None, 'jwt': None} if logout else {}
73 return super().__new__(
74 HTTPResponse,
75 message,
76 status_code,
77 'err',
78 data,
79 cookies,
80 )