|
| 1 | +import time |
| 2 | +import websocket |
| 3 | +import json |
| 4 | +import base64 |
| 5 | +import threading |
| 6 | +import uuid |
| 7 | +import logging |
| 8 | +from typing import Callable, Dict, Any |
| 9 | +from .exceptions import MaxRetriesExceeded |
| 10 | + |
| 11 | +logger = logging.getLogger(__name__) |
| 12 | + |
| 13 | + |
| 14 | +class GraphQLWebSocketClient: |
| 15 | + def __init__(self, url: str, auth_function: Callable[[], Dict[str, str]], max_retries: int = 5): |
| 16 | + self.url = url |
| 17 | + self.auth_function = auth_function |
| 18 | + self.ws = None |
| 19 | + self.subscriptions = {} |
| 20 | + self._is_open = False |
| 21 | + self._acknowledged_event = threading.Event() |
| 22 | + self.max_retries = max_retries |
| 23 | + self.retry_count = 0 |
| 24 | + self.lock = threading.Lock() |
| 25 | + |
| 26 | + def _build_ws_url(self) -> str: |
| 27 | + """ |
| 28 | + Build the WebSocket URL with authentication headers. |
| 29 | + """ |
| 30 | + auth_info = self.auth_function() |
| 31 | + headers_encoded = base64.b64encode(json.dumps(auth_info).encode()).decode() |
| 32 | + all_url = f"{self.url}?header={headers_encoded}&payload={base64.b64encode(json.dumps({}).encode()).decode()}" |
| 33 | + logger.debug(f"WebSocket URL: {all_url}") |
| 34 | + return all_url |
| 35 | + |
| 36 | + def connect(self): |
| 37 | + """ |
| 38 | + Establish the WebSocket connection. |
| 39 | + """ |
| 40 | + try: |
| 41 | + ws_url = self._build_ws_url() |
| 42 | + self.ws = websocket.WebSocketApp( |
| 43 | + ws_url, |
| 44 | + subprotocols=["graphql-ws"], |
| 45 | + on_open=self._on_open, |
| 46 | + on_message=self._on_message, |
| 47 | + on_error=self._on_error, |
| 48 | + on_close=self._on_close, |
| 49 | + ) |
| 50 | + thread = threading.Thread(target=self.ws.run_forever) |
| 51 | + thread.daemon = True |
| 52 | + thread.start() |
| 53 | + except Exception as e: |
| 54 | + logger.error(f"Failed to connect: {e}") |
| 55 | + self._attempt_reconnect() |
| 56 | + |
| 57 | + def _on_open(self, ws): |
| 58 | + """ |
| 59 | + WebSocket open event handler. Sends connection initialization message. |
| 60 | + """ |
| 61 | + self._is_open = True |
| 62 | + self._acknowledged_event.clear() |
| 63 | + self._send_message({"type": "connection_init"}) |
| 64 | + logger.info("WebSocket connection opened.") |
| 65 | + self.retry_count = 0 |
| 66 | + |
| 67 | + def _on_message(self, ws, message): |
| 68 | + """ |
| 69 | + WebSocket message event handler. Process incoming messages. |
| 70 | + """ |
| 71 | + msg = json.loads(message) |
| 72 | + message_type = msg.get('type') |
| 73 | + |
| 74 | + if message_type == "connection_ack": |
| 75 | + self._acknowledged_event.set() |
| 76 | + logger.info("Connection acknowledged.") |
| 77 | + elif message_type == "ka": |
| 78 | + logger.debug("Keep-alive received.") |
| 79 | + elif message_type == "data": |
| 80 | + subscription_id = msg.get('id') |
| 81 | + if subscription_id and subscription_id in self.subscriptions: |
| 82 | + callback = self.subscriptions[subscription_id]['callback'] |
| 83 | + callback(msg['payload']) |
| 84 | + elif message_type == "error": |
| 85 | + logger.error(f"Error received: {msg.get('payload')}") |
| 86 | + |
| 87 | + def _on_error(self, ws, error): |
| 88 | + """ |
| 89 | + WebSocket error event handler. |
| 90 | + """ |
| 91 | + logger.error(f"WebSocket error occurred: {error}") |
| 92 | + if isinstance(error, Exception): |
| 93 | + logger.exception("Exception details:", exc_info=error) |
| 94 | + self._attempt_reconnect() |
| 95 | + |
| 96 | + def _on_close(self, ws, close_status_code, close_msg): |
| 97 | + """ |
| 98 | + WebSocket close event handler. |
| 99 | + """ |
| 100 | + self._is_open = False |
| 101 | + self._acknowledged_event.clear() |
| 102 | + logger.warning(f"WebSocket closed: {close_status_code}, message: {close_msg}") |
| 103 | + self._attempt_reconnect() |
| 104 | + |
| 105 | + def _attempt_reconnect(self): |
| 106 | + """ |
| 107 | + Attempt to reconnect to the WebSocket. |
| 108 | + """ |
| 109 | + if self.retry_count < self.max_retries: |
| 110 | + self.retry_count += 1 |
| 111 | + wait_time = 2**self.retry_count + (0.5 * self.retry_count) |
| 112 | + logger.info(f"Reconnecting in {wait_time} seconds...") |
| 113 | + time.sleep(wait_time) |
| 114 | + self.connect() |
| 115 | + else: |
| 116 | + raise MaxRetriesExceeded("Max retries reached.") |
| 117 | + |
| 118 | + def subscribe( |
| 119 | + self, query: str, variables: Dict[str, Any], callback: Callable[[Dict[str, Any]], None], acknowledgment_timeout: int = 10 |
| 120 | + ) -> str: |
| 121 | + """ |
| 122 | + Subscribe to a GraphQL query via WebSocket. |
| 123 | +
|
| 124 | + :param query: GraphQL query as a string. |
| 125 | + :param variables: Variables for the GraphQL query. |
| 126 | + :param callback: A callback function that handles the incoming data. |
| 127 | + :return: The subscription ID (used for unsubscribing later). |
| 128 | + """ |
| 129 | + # Wait for connection to be acknowledged before subscribing |
| 130 | + if not self._acknowledged_event.wait(timeout=acknowledgment_timeout): |
| 131 | + logger.error("Connection acknowledgment timeout.") |
| 132 | + raise TimeoutError("Connection acknowledgment timeout.") |
| 133 | + |
| 134 | + subscription_id = str(uuid.uuid4()) |
| 135 | + self.subscriptions[subscription_id] = {'query': query, 'variables': variables, 'callback': callback} |
| 136 | + |
| 137 | + message = { |
| 138 | + "id": subscription_id, |
| 139 | + "type": "start", |
| 140 | + "payload": { |
| 141 | + "data": json.dumps({"query": query, "variables": variables}), |
| 142 | + "extensions": {"authorization": self.auth_function()}, |
| 143 | + }, |
| 144 | + } |
| 145 | + self._send_message(message) |
| 146 | + logger.info(f"Subscribed with ID: {subscription_id}") |
| 147 | + return subscription_id |
| 148 | + |
| 149 | + def unsubscribe(self, subscription_id: str): |
| 150 | + """ |
| 151 | + Unsubscribe from a subscription. |
| 152 | + :param subscription_id: The subscription ID. |
| 153 | + """ |
| 154 | + with self.lock: |
| 155 | + if subscription_id in self.subscriptions: |
| 156 | + self._send_message({"id": subscription_id, "type": "stop"}) |
| 157 | + del self.subscriptions[subscription_id] |
| 158 | + logger.info(f"Unsubscribed from: {subscription_id}") |
| 159 | + |
| 160 | + def _send_message(self, message: Dict[str, Any]): |
| 161 | + """ |
| 162 | + Send a message over the WebSocket. |
| 163 | + """ |
| 164 | + with self.lock: |
| 165 | + if self.ws and self._is_open: |
| 166 | + self.ws.send(json.dumps(message)) |
| 167 | + else: |
| 168 | + logger.warning("WebSocket is not open. Cannot send message.") |
| 169 | + |
| 170 | + def isConnectionOpen(self): |
| 171 | + """ |
| 172 | + Check if the WebSocket connection is open. |
| 173 | + """ |
| 174 | + return self._is_open |
| 175 | + |
| 176 | + def isAcknowledged(self): |
| 177 | + """ |
| 178 | + Check if the connection has been acknowledged. |
| 179 | + """ |
| 180 | + return self._acknowledged_event.is_set() |
| 181 | + |
| 182 | + def close(self): |
| 183 | + """ |
| 184 | + Close the WebSocket connection. |
| 185 | + """ |
| 186 | + with self.lock: |
| 187 | + if self.ws: |
| 188 | + self.ws.close() |
| 189 | + self._is_open = False |
| 190 | + logger.info("WebSocket connection closed.") |
| 191 | + |
| 192 | + def __del__(self): |
| 193 | + self.close() |
0 commit comments