-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathviews.py
88 lines (67 loc) · 2.87 KB
/
views.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
import logging
from django.http import HttpResponse
from rest_framework import views
from rest_framework.request import Request
from rest_framework.permissions import IsAuthenticated
from gateway import exceptions
from gateway.permissions import AllowLogicModuleGroup
from gateway.request import GatewayRequest, AsyncGatewayRequest
logger = logging.getLogger(__name__)
class APIGatewayView(views.APIView):
"""
API gateway receives API requests, enforces throttling and security
policies, passes requests to the back-end service and then passes the
response back to the requester
"""
permission_classes = (IsAuthenticated, AllowLogicModuleGroup)
schema = None
gateway_request_class = GatewayRequest
def __init__(self, *args, **kwargs):
self._logic_modules = dict()
self._specs = dict()
self._data = dict()
super().__init__(*args, **kwargs)
def get(self, request, *args, **kwargs):
return self.make_service_request(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
return self.make_service_request(request, *args, **kwargs)
def delete(self, request, *args, **kwargs):
return self.make_service_request(request, *args, **kwargs)
def put(self, request, *args, **kwargs):
return self.make_service_request(request, *args, **kwargs)
def patch(self, request, *args, **kwargs):
return self.make_service_request(request, *args, **kwargs)
def options(self, request, *args, **kwargs):
return self.make_service_request(request, *args, **kwargs)
def make_service_request(self, request, *args, **kwargs):
"""
Create a request for the defined service
"""
# validate incoming request before creating a service request
try:
self._validate_incoming_request(request, **kwargs)
except exceptions.RequestValidationError as e:
return HttpResponse(
content=e.content, status=e.status, content_type=e.content_type
)
gw_request = self.gateway_request_class(request, **kwargs)
gw_response = gw_request.perform()
return HttpResponse(
content=gw_response.content,
status=gw_response.status_code,
content_type=gw_response.headers.get('Content-Type'),
)
def _validate_incoming_request(self, request: Request, **kwargs: dict) -> None:
"""
Do certain validations to the request before starting to create a new request to services
"""
if (
request.META['REQUEST_METHOD'] in ['PUT', 'PATCH', 'DELETE']
and kwargs['pk'] is None
):
raise exceptions.RequestValidationError('The object ID is missing.', 400)
class APIAsyncGatewayView(APIGatewayView):
"""
Async version of APIGatewayView
"""
gateway_request_class = AsyncGatewayRequest