2019-03-17 21:09:07 +01:00
|
|
|
from channels.generic.websocket import AsyncWebsocketConsumer
|
2018-11-26 02:05:03 +01:00
|
|
|
from asgiref.sync import async_to_sync
|
|
|
|
import json
|
|
|
|
|
|
|
|
|
2019-03-17 21:09:07 +01:00
|
|
|
class WebhookConsumer(AsyncWebsocketConsumer):
|
|
|
|
async def connect(self):
|
2019-05-18 20:24:28 +02:00
|
|
|
"""While the connection is open, it is associated with a callback id"""
|
2018-12-22 00:17:51 +01:00
|
|
|
self.callback = self.scope["url_route"]["kwargs"]["uuid"]
|
2019-03-17 21:09:07 +01:00
|
|
|
await self.channel_layer.group_add(self.callback, self.channel_name)
|
|
|
|
await self.accept()
|
2018-11-26 02:05:03 +01:00
|
|
|
|
2019-03-17 21:09:07 +01:00
|
|
|
async def disconnect(self, close_code):
|
|
|
|
await self.channel_layer.group_discard(self.callback, self.channel_name)
|
2018-11-26 02:05:03 +01:00
|
|
|
|
2019-03-17 21:09:07 +01:00
|
|
|
async def receive(self, text_data):
|
2018-11-26 02:05:03 +01:00
|
|
|
# Discard all received data
|
|
|
|
pass
|
|
|
|
|
2019-03-17 21:09:07 +01:00
|
|
|
async def new_request(self, event):
|
2018-11-26 02:05:03 +01:00
|
|
|
"""Sends all the newly received data on the callback"""
|
2019-03-17 21:09:07 +01:00
|
|
|
await self.send(text_data=json.dumps(event["data"]))
|