common event parsing

This commit is contained in:
Maxim Devaev
2022-07-19 16:42:46 +03:00
parent b16359c53e
commit adf4be9bf7
3 changed files with 24 additions and 32 deletions

View File

@@ -208,6 +208,13 @@ async def stream_json_exception(response: StreamResponse, err: Exception) -> Non
}, False)
async def send_ws_event(wsr: WebSocketResponse, event_type: str, event: Optional[Dict]) -> None:
await wsr.send_str(json.dumps({
"event_type": event_type,
"event": event,
}))
def parse_ws_event(msg: str) -> Tuple[str, Dict]:
data = json.loads(msg)
if not isinstance(data, dict):
@@ -246,10 +253,7 @@ class WsSession:
return f"WsSession(id={id(self)}, {self.kwargs})"
async def send_event(self, event_type: str, event: Optional[Dict]) -> None:
await self.wsr.send_str(json.dumps({
"event_type": event_type,
"event": event,
}))
await send_ws_event(self.wsr, event_type, event)
class HttpServer: