Send bytes; if expected_id is set, skip push notifications until that response arrives.
(
self,
data: bytes,
expected_id: Optional[str] = None,
timeout: float = SOCKET_TIMEOUT,
)
| 309 | return None |
| 310 | |
| 311 | def send_raw( |
| 312 | self, |
| 313 | data: bytes, |
| 314 | expected_id: Optional[str] = None, |
| 315 | timeout: float = SOCKET_TIMEOUT, |
| 316 | ) -> Optional[dict]: |
| 317 | """Send bytes; if expected_id is set, skip push notifications until that response arrives.""" |
| 318 | if not self.socket: |
| 319 | return None |
| 320 | |
| 321 | try: |
| 322 | if self.verbose: |
| 323 | print(f"[SEND] {data.decode('utf-8', errors='replace').strip()}") |
| 324 | |
| 325 | self.socket.sendall(data) |
| 326 | |
| 327 | if expected_id: |
| 328 | end_time = time.time() + timeout |
| 329 | while True: |
| 330 | remaining = end_time - time.time() |
| 331 | if remaining <= 0: |
| 332 | if self.verbose: |
| 333 | print( |
| 334 | f"[ERROR] Timeout waiting for response ID {expected_id}" |
| 335 | ) |
| 336 | return None |
| 337 | |
| 338 | msg = self.recv_message(timeout=remaining) |
| 339 | if not msg: |
| 340 | return None |
| 341 | |
| 342 | if ( |
| 343 | msg.get("type") == MessageType.RESPONSE |
| 344 | and msg.get("id") == expected_id |
| 345 | ): |
| 346 | return msg |
| 347 | |
| 348 | if self.verbose: |
| 349 | print( |
| 350 | f"[DEBUG] Discarding push notification while waiting for {expected_id}" |
| 351 | ) |
| 352 | else: |
| 353 | return self.recv_message(timeout=timeout) |
| 354 | |
| 355 | except Exception as e: |
| 356 | if self.verbose: |
| 357 | print(f"[ERROR] {e}") |
| 358 | return None |
| 359 | |
| 360 | def send_json(self, obj: dict) -> Optional[dict]: |
| 361 | data = json.dumps(obj, separators=(",", ":")) + "\n" |
no test coverage detected