-
-
Notifications
You must be signed in to change notification settings - Fork 82
/
consumers.py
289 lines (226 loc) · 9.18 KB
/
consumers.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
import asyncio
import json
import typing
from functools import partial
from typing import List, Type
from channels.consumer import AsyncConsumer
from channels.db import database_sync_to_async
from channels.generic.websocket import AsyncJsonWebsocketConsumer
from djangochannelsrestframework.permissions import BasePermission
from djangochannelsrestframework.settings import api_settings
from django.http import HttpRequest, HttpResponse
from django.http.response import Http404
from django.template.response import SimpleTemplateResponse
from rest_framework.exceptions import PermissionDenied, MethodNotAllowed, APIException
from rest_framework.response import Response
class APIConsumerMetaclass(type):
"""
Metaclass that records action methods
"""
def __new__(mcs, name, bases, body):
cls = type.__new__(mcs, name, bases, body)
cls.available_actions = {}
for method_name in dir(cls):
attr = getattr(cls, method_name)
is_action = getattr(attr, "action", False)
if is_action:
kwargs = getattr(attr, "kwargs", {})
name = kwargs.get("name", method_name)
cls.available_actions[name] = method_name
return cls
def ensure_async(method: typing.Callable):
if asyncio.iscoroutinefunction(method):
return method
return database_sync_to_async(method)
class AsyncAPIConsumer(AsyncJsonWebsocketConsumer, metaclass=APIConsumerMetaclass):
"""
Be very inspired by django rest framework ViewSets
"""
# use django rest framework permissions
# The following policies may be set at either globally, or per-view.
# take the default values set for django rest framework!
permission_classes = (
api_settings.DEFAULT_PERMISSION_CLASSES
) # type: List[Type[BasePermission]]
groups = {}
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.groups = set(self.groups or [])
async def add_group(self, name: str):
if not isinstance(self.groups, set):
self.groups = set(self.groups)
if name not in self.groups:
await self.channel_layer.group_add(name, self.channel_name)
self.groups.add(name)
async def remove_group(self, name: str):
if not isinstance(self.groups, set):
self.groups = set(self.groups)
if name in self.groups:
await self.channel_layer.group_discard(name, self.channel_name)
self.groups.remove(name)
async def get_permissions(self, action: str, **kwargs):
"""
Instantiates and returns the list of permissions that this view requires.
"""
return [permission() for permission in self.permission_classes]
async def check_permissions(self, action: str, **kwargs):
"""
Check if the action should be permitted.
Raises an appropriate exception if the request is not permitted.
"""
for permission in await self.get_permissions(action=action, **kwargs):
if not await ensure_async(permission.has_permission)(
scope=self.scope, consumer=self, action=action, **kwargs
):
raise PermissionDenied()
async def handle_exception(self, exc: Exception, action: str, request_id):
"""
Handle any exception that occurs, by sending an appropriate message
"""
if isinstance(exc, APIException):
await self.reply(
action=action,
errors=self._format_errors(exc.detail),
status=exc.status_code,
request_id=request_id,
)
elif exc == Http404 or isinstance(exc, Http404):
await self.reply(
action=action,
errors=self._format_errors("Not found"),
status=404,
request_id=request_id,
)
else:
raise exc
def _format_errors(self, errors):
if isinstance(errors, list):
return errors
elif isinstance(errors, str):
return [errors]
elif isinstance(errors, dict):
return [errors]
async def handle_action(self, action: str, request_id: str, **kwargs):
"""
run the action.
"""
try:
await self.check_permissions(action, **kwargs)
if action not in self.available_actions:
raise MethodNotAllowed(method=action)
method_name = self.available_actions[action]
method = getattr(self, method_name)
reply = partial(self.reply, action=action, request_id=request_id)
# the @action decorator will wrap non-async action into async ones.
response = await method(request_id=request_id, action=action, **kwargs)
if isinstance(response, tuple):
data, status = response
await reply(data=data, status=status)
except Exception as exc:
await self.handle_exception(exc, action=action, request_id=request_id)
async def receive_json(self, content: typing.Dict, **kwargs):
"""
Called with decoded JSON content.
"""
# TODO assert format, if does not match return message.
request_id = content.pop("request_id")
action = content.pop("action")
await self.handle_action(action, request_id=request_id, **content)
async def reply(
self, action: str, data=None, errors=None, status=200, request_id=None
):
if errors is None:
errors = []
payload = {
"errors": errors,
"data": data,
"action": action,
"response_status": status,
"request_id": request_id,
}
await self.send_json(payload)
class DjangoViewAsConsumer(AsyncAPIConsumer):
view = None
@property
def dumpy_url_config(self):
return
# maps actions to HTTP methods
actions = {} # type: Dict[str, str]
async def receive_json(self, content: typing.Dict, **kwargs):
"""
Called with decoded JSON content.
"""
# TODO assert format, if does not match return message.
request_id = content.pop("request_id")
action = content.pop("action")
await self.handle_action(action, request_id=request_id, **content)
async def handle_action(self, action: str, request_id: str, **kwargs):
"""
run the action.
"""
try:
await self.check_permissions(action, **kwargs)
if action not in self.actions:
raise MethodNotAllowed(method=action)
content, status = await self.call_view(action=action, **kwargs)
await self.reply(
action=action, request_id=request_id, data=content, status=status
)
except Exception as exc:
await self.handle_exception(exc, action=action, request_id=request_id)
@database_sync_to_async
def call_view(self, action: str, **kwargs):
request = HttpRequest()
request.path = self.scope.get("path")
request.session = self.scope.get("session", None)
request.META["HTTP_CONTENT_TYPE"] = "application/json"
request.META["HTTP_ACCEPT"] = "application/json"
for (header_name, value) in self.scope.get("headers", []):
request.META[header_name.decode("utf-8")] = value.decode("utf-8")
args, view_kwargs = self.get_view_args(action=action, **kwargs)
request.method = self.actions[action]
request.POST = json.dumps(kwargs.get("data", {}))
if self.scope.get("cookies"):
request.COOKIES = self.scope.get("cookies")
view = getattr(self.__class__, "view")
response = view(request, *args, **view_kwargs)
status = response.status_code
if isinstance(response, Response):
data = response.data
try:
# check if we can json encode it!
# there must be a better way fo doing this?
json.dumps(data)
return data, status
except Exception as e:
pass
if isinstance(response, SimpleTemplateResponse):
response.render()
response_content = response.content
if isinstance(response_content, bytes):
try:
response_content = response_content.decode("utf-8")
except Exception as e:
response_content = response_content.hex()
return response_content, status
def get_view_args(self, action: str, **kwargs):
return [], {}
def view_as_consumer(
wrapped_view: typing.Callable[[HttpRequest], HttpResponse],
mapped_actions: typing.Optional[typing.Dict[str, str]] = None,
) -> Type[AsyncConsumer]:
"""
Wrap a django View so that it will be triggered by actions over this json
websocket consumer.
"""
if mapped_actions is None:
mapped_actions = {
"create": "PUT",
"update": "PATCH",
"list": "GET",
"retrieve": "GET",
}
class DjangoViewWrapper(DjangoViewAsConsumer):
view = wrapped_view
actions = mapped_actions
return DjangoViewWrapper