-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.py
50 lines (37 loc) · 1.37 KB
/
base.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
"""
Base classes for views.
"""
from rest_framework.exceptions import ApiException
class BaseApiView(object):
"""
Base api view.
"""
# For settings run dispatch method
use_dispatch = True
# Class for create Response object. Default Interface: Response(data: dict, status: int)
# Data - dict json data for return. status - response server status code.
response_class = None
# Response Content Type, default: application/json
response_content_type = 'application/json'
def fail(self, detail=None, status=400):
"""
Raise ApiException for return api exception response.
:param Optional[dict, str, int, float, list] detail: Body request data. Valid json objects.
:param int status: Response status code.
:raise ApiException:
"""
raise ApiException(detail=detail, status=status)
def _dispatch(self, method, *args, **kwargs):
"""
Code after, before call request handler.
:param Callable method: Method handler for call.
"""
try:
return method(*args, **kwargs)
except ApiException as e:
# TODO: Not security. e.detail maybe anything
return self.response_class(
{'errors': e.detail},
status=e.status or 400,
content_type=self.response_content_type
)