|
| 1 | +import asyncio |
| 2 | +import json |
| 3 | +from typing import Dict, Optional, Tuple |
| 4 | + |
| 5 | +from graphql import DocumentNode, ExecutionResult, print_ast |
| 6 | +from websockets.exceptions import ConnectionClosed |
| 7 | + |
| 8 | +from .exceptions import ( |
| 9 | + TransportProtocolError, |
| 10 | + TransportQueryError, |
| 11 | + TransportServerError, |
| 12 | +) |
| 13 | +from .websockets import WebsocketsTransport |
| 14 | + |
| 15 | + |
| 16 | +class PhoenixChannelWebsocketsTransport(WebsocketsTransport): |
| 17 | + def __init__( |
| 18 | + self, channel_name: str, heartbeat_interval: float = 30, *args, **kwargs |
| 19 | + ) -> None: |
| 20 | + self.channel_name = channel_name |
| 21 | + self.heartbeat_interval = heartbeat_interval |
| 22 | + self.subscription_ids_to_query_ids: Dict[str, int] = {} |
| 23 | + super(PhoenixChannelWebsocketsTransport, self).__init__(*args, **kwargs) |
| 24 | + """Initialize the transport with the given request parameters. |
| 25 | +
|
| 26 | + :param channel_name Channel on the server this transport will join |
| 27 | + :param heartbeat_interval Interval in second between each heartbeat messages |
| 28 | + sent by the client |
| 29 | + """ |
| 30 | + |
| 31 | + async def _send_init_message_and_wait_ack(self) -> None: |
| 32 | + """Join the specified channel and wait for the connection ACK. |
| 33 | +
|
| 34 | + If the answer is not a connection_ack message, we will return an Exception. |
| 35 | + """ |
| 36 | + |
| 37 | + query_id = self.next_query_id |
| 38 | + self.next_query_id += 1 |
| 39 | + |
| 40 | + init_message = json.dumps( |
| 41 | + { |
| 42 | + "topic": self.channel_name, |
| 43 | + "event": "phx_join", |
| 44 | + "payload": {}, |
| 45 | + "ref": query_id, |
| 46 | + } |
| 47 | + ) |
| 48 | + |
| 49 | + await self._send(init_message) |
| 50 | + |
| 51 | + # Wait for the connection_ack message or raise a TimeoutError |
| 52 | + init_answer = await asyncio.wait_for(self._receive(), self.ack_timeout) |
| 53 | + |
| 54 | + answer_type, answer_id, execution_result = self._parse_answer(init_answer) |
| 55 | + |
| 56 | + if answer_type != "reply": |
| 57 | + raise TransportProtocolError( |
| 58 | + "Websocket server did not return a connection ack" |
| 59 | + ) |
| 60 | + |
| 61 | + async def heartbeat_coro(): |
| 62 | + while True: |
| 63 | + await asyncio.sleep(self.heartbeat_interval) |
| 64 | + try: |
| 65 | + query_id = self.next_query_id |
| 66 | + self.next_query_id += 1 |
| 67 | + |
| 68 | + await self._send( |
| 69 | + json.dumps( |
| 70 | + { |
| 71 | + "topic": "phoenix", |
| 72 | + "event": "heartbeat", |
| 73 | + "payload": {}, |
| 74 | + "ref": query_id, |
| 75 | + } |
| 76 | + ) |
| 77 | + ) |
| 78 | + except ConnectionClosed: # pragma: no cover |
| 79 | + return |
| 80 | + |
| 81 | + self.heartbeat_task = asyncio.ensure_future(heartbeat_coro()) |
| 82 | + |
| 83 | + async def _send_stop_message(self, query_id: int) -> None: |
| 84 | + try: |
| 85 | + await self.listeners[query_id].put(("complete", None)) |
| 86 | + except KeyError: # pragma: no cover |
| 87 | + pass |
| 88 | + |
| 89 | + async def _send_connection_terminate_message(self) -> None: |
| 90 | + """Send a phx_leave message to disconnect from the provided channel. |
| 91 | + """ |
| 92 | + |
| 93 | + query_id = self.next_query_id |
| 94 | + self.next_query_id += 1 |
| 95 | + |
| 96 | + connection_terminate_message = json.dumps( |
| 97 | + { |
| 98 | + "topic": self.channel_name, |
| 99 | + "event": "phx_leave", |
| 100 | + "payload": {}, |
| 101 | + "ref": query_id, |
| 102 | + } |
| 103 | + ) |
| 104 | + |
| 105 | + await self._send(connection_terminate_message) |
| 106 | + |
| 107 | + async def _send_query( |
| 108 | + self, |
| 109 | + document: DocumentNode, |
| 110 | + variable_values: Optional[Dict[str, str]] = None, |
| 111 | + operation_name: Optional[str] = None, |
| 112 | + ) -> int: |
| 113 | + """Send a query to the provided websocket connection. |
| 114 | +
|
| 115 | + We use an incremented id to reference the query. |
| 116 | +
|
| 117 | + Returns the used id for this query. |
| 118 | + """ |
| 119 | + |
| 120 | + query_id = self.next_query_id |
| 121 | + self.next_query_id += 1 |
| 122 | + |
| 123 | + query_str = json.dumps( |
| 124 | + { |
| 125 | + "topic": self.channel_name, |
| 126 | + "event": "doc", |
| 127 | + "payload": { |
| 128 | + "query": print_ast(document), |
| 129 | + "variables": variable_values or {}, |
| 130 | + }, |
| 131 | + "ref": query_id, |
| 132 | + } |
| 133 | + ) |
| 134 | + |
| 135 | + await self._send(query_str) |
| 136 | + |
| 137 | + return query_id |
| 138 | + |
| 139 | + def _parse_answer( |
| 140 | + self, answer: str |
| 141 | + ) -> Tuple[str, Optional[int], Optional[ExecutionResult]]: |
| 142 | + """Parse the answer received from the server |
| 143 | +
|
| 144 | + Returns a list consisting of: |
| 145 | + - the answer_type (between: |
| 146 | + 'heartbeat', 'data', 'reply', 'error', 'close') |
| 147 | + - the answer id (Integer) if received or None |
| 148 | + - an execution Result if the answer_type is 'data' or None |
| 149 | + """ |
| 150 | + |
| 151 | + event: str = "" |
| 152 | + answer_id: Optional[int] = None |
| 153 | + answer_type: str = "" |
| 154 | + execution_result: Optional[ExecutionResult] = None |
| 155 | + |
| 156 | + try: |
| 157 | + json_answer = json.loads(answer) |
| 158 | + |
| 159 | + event = str(json_answer.get("event")) |
| 160 | + |
| 161 | + if event == "subscription:data": |
| 162 | + payload = json_answer.get("payload") |
| 163 | + |
| 164 | + if not isinstance(payload, dict): |
| 165 | + raise ValueError("payload is not a dict") |
| 166 | + |
| 167 | + subscription_id = str(payload.get("subscriptionId")) |
| 168 | + try: |
| 169 | + answer_id = self.subscription_ids_to_query_ids[subscription_id] |
| 170 | + except KeyError: |
| 171 | + raise ValueError( |
| 172 | + f"subscription '{subscription_id}' has not been registerd" |
| 173 | + ) |
| 174 | + |
| 175 | + result = payload.get("result") |
| 176 | + |
| 177 | + if not isinstance(result, dict): |
| 178 | + raise ValueError("result is not a dict") |
| 179 | + |
| 180 | + answer_type = "data" |
| 181 | + |
| 182 | + execution_result = ExecutionResult( |
| 183 | + errors=payload.get("errors"), data=result.get("data") |
| 184 | + ) |
| 185 | + |
| 186 | + elif event == "phx_reply": |
| 187 | + answer_id = int(json_answer.get("ref")) |
| 188 | + payload = json_answer.get("payload") |
| 189 | + |
| 190 | + if not isinstance(payload, dict): |
| 191 | + raise ValueError("payload is not a dict") |
| 192 | + |
| 193 | + status = str(payload.get("status")) |
| 194 | + |
| 195 | + if status == "ok": |
| 196 | + |
| 197 | + answer_type = "reply" |
| 198 | + response = payload.get("response") |
| 199 | + |
| 200 | + if isinstance(response, dict) and "subscriptionId" in response: |
| 201 | + subscription_id = str(response.get("subscriptionId")) |
| 202 | + self.subscription_ids_to_query_ids[subscription_id] = answer_id |
| 203 | + |
| 204 | + elif status == "error": |
| 205 | + response = payload.get("response") |
| 206 | + |
| 207 | + if isinstance(response, dict): |
| 208 | + if "errors" in response: |
| 209 | + raise TransportQueryError( |
| 210 | + str(response.get("errors")), query_id=answer_id |
| 211 | + ) |
| 212 | + elif "reason" in response: |
| 213 | + raise TransportQueryError( |
| 214 | + str(response.get("reason")), query_id=answer_id |
| 215 | + ) |
| 216 | + raise ValueError("reply error") |
| 217 | + |
| 218 | + elif status == "timeout": |
| 219 | + raise TransportQueryError("reply timeout", query_id=answer_id) |
| 220 | + |
| 221 | + elif event == "phx_error": |
| 222 | + raise TransportServerError("Server error") |
| 223 | + elif event == "phx_close": |
| 224 | + answer_type = "close" |
| 225 | + else: |
| 226 | + raise ValueError |
| 227 | + |
| 228 | + except ValueError as e: |
| 229 | + raise TransportProtocolError( |
| 230 | + "Server did not return a GraphQL result" |
| 231 | + ) from e |
| 232 | + |
| 233 | + return answer_type, answer_id, execution_result |
| 234 | + |
| 235 | + async def _handle_answer( |
| 236 | + self, |
| 237 | + answer_type: str, |
| 238 | + answer_id: Optional[int], |
| 239 | + execution_result: Optional[ExecutionResult], |
| 240 | + ) -> None: |
| 241 | + if answer_type == "close": |
| 242 | + await self.close() |
| 243 | + else: |
| 244 | + await super()._handle_answer(answer_type, answer_id, execution_result) |
| 245 | + |
| 246 | + async def _close_coro(self, e: Exception, clean_close: bool = True) -> None: |
| 247 | + if self.heartbeat_task is not None: |
| 248 | + self.heartbeat_task.cancel() |
| 249 | + |
| 250 | + await super()._close_coro(e, clean_close) |
0 commit comments