This repository has been archived by the owner on Nov 13, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
restful.py
134 lines (108 loc) · 4.02 KB
/
restful.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
"""
This file is part of Vapour.
Vapour is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Vapour is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with Vapour. If not, see <http://www.gnu.org/licenses/>.
"""
import mimeparse
from tornado.web import HTTPError
class JSONMixin:
def head(self, *args, **kwargs):
mime = mimeparse.best_match(['application/json', 'text/html'],
self.request.headers.get("Accept"))
if mime == "text/html":
self.head_html(*args, **kwargs)
elif mime == "application/json":
self.head_json(*args, **kwargs)
else:
raise HTTPError(405)
def get(self, *args, **kwargs):
mime = mimeparse.best_match(['application/json', 'text/html'],
self.request.headers.get("Accept"))
print(mime)
if mime == "text/html":
self.get_html(*args, **kwargs)
elif mime == "application/json":
self.get_json(*args, **kwargs)
else:
raise HTTPError(405)
def post(self, *args, **kwargs):
mime = mimeparse.best_match(['application/json', 'text/html'],
self.request.headers.get("Accept"))
if mime == "text/html":
self.post_html(*args, **kwargs)
elif mime == "application/json":
self.post_json(*args, **kwargs)
else:
raise HTTPError(405)
def delete(self, *args, **kwargs):
mime = mimeparse.best_match(['application/json', 'text/html'],
self.request.headers.get("Accept"))
if mime == "text/html":
self.delete_html(*args, **kwargs)
elif mime == "application/json":
self.delete_json(*args, **kwargs)
else:
raise HTTPError(405)
def patch(self, *args, **kwargs):
mime = mimeparse.best_match(['application/json', 'text/html'],
self.request.headers.get("Accept"))
if mime == "text/html":
self.patch_html(*args, **kwargs)
elif mime == "application/json":
self.patch_json(*args, **kwargs)
else:
raise HTTPError(405)
def put(self, *args, **kwargs):
mime = mimeparse.best_match(['application/json', 'text/html'],
self.request.headers.get("Accept"))
if mime == "text/html":
self.put_html(*args, **kwargs)
elif mime == "application/json":
self.put_json(*args, **kwargs)
else:
raise HTTPError(405)
def options(self, *args, **kwargs):
mime = mimeparse.best_match(['application/json', 'text/html'],
self.request.headers.get("Accept"))
if mime == "text/html":
self.options_html(*args, **kwargs)
elif mime == "application/json":
self.options_json(*args, **kwargs)
else:
raise HTTPError(405)
def head_html(self):
raise HTTPError(405)
def head_json(self):
raise HTTPError(405)
def get_html(self):
raise HTTPError(405)
def get_json(self):
raise HTTPError(405)
def post_html(self):
raise HTTPError(405)
def post_json(self):
raise HTTPError(405)
def delete_html(self):
raise HTTPError(405)
def delete_json(self):
raise HTTPError(405)
def patch_html(self):
raise HTTPError(405)
def patch_json(self):
raise HTTPError(405)
def put_html(self):
raise HTTPError(405)
def put_json(self):
raise HTTPError(405)
def options_html(self):
raise HTTPError(405)
def options_json(self):
raise HTTPError(405)