|
12 | 12 | from xml.etree import ElementTree as XML |
13 | 13 |
|
14 | 14 | from Cryptodome.Cipher import AES |
| 15 | +from orjson import JSONDecodeError # pylint: disable=no-name-in-module |
| 16 | +from orjson import loads as json_loads # pylint: disable=no-name-in-module |
15 | 17 |
|
16 | 18 | from ..const import ( |
17 | 19 | AI_DETECT_CONVERSION, |
@@ -517,15 +519,27 @@ def _push_callback(self, cmd_id: int, data: bytes, len_header: int, payload: byt |
517 | 519 |
|
518 | 520 | self._parse_xml(cmd_id, rec_body, payload, mess_id) |
519 | 521 |
|
520 | | - def _webhook_push_callback(self, data: dict[str, Any]) -> None: |
| 522 | + def webhook_push_callback(self, data: bytes) -> None: |
521 | 523 | """Callback to parse a received message that was pushed through the webhook""" |
522 | | - uid: str = data.get("uid", "") |
523 | | - cmd_id: int | None = data.get("cmd") |
524 | | - xml: str | None = data.get("xml") |
525 | | - ext_xml: str = data.get("ext_xml", "") |
| 524 | + try: |
| 525 | + text = data.decode("utf-8") |
| 526 | + except Exception as err: |
| 527 | + _LOGGER.debug("Baichuan host %s: error during decoding webhook data: %s", self._host, err) |
| 528 | + return |
| 529 | + |
| 530 | + try: |
| 531 | + mess: dict[str, Any] = json_loads(text) |
| 532 | + except JSONDecodeError as err: |
| 533 | + _LOGGER.debug("Baichuan host %s: error during decoding webhook json data:\n%s\n%s", self._host, text, err) |
| 534 | + return |
| 535 | + |
| 536 | + uid: str = mess.get("uid", "") |
| 537 | + cmd_id: int | None = mess.get("cmd") |
| 538 | + xml: str | None = mess.get("xml") |
| 539 | + ext_xml: str = mess.get("ext_xml", "") |
526 | 540 |
|
527 | 541 | if cmd_id is None or xml is None: |
528 | | - _LOGGER.debug("Baichuan host %s: received webhook push with malformed data:\n%s", self._host, data) |
| 542 | + _LOGGER.debug("Baichuan host %s: received webhook push with malformed data:\n%s", self._host, mess) |
529 | 543 | return |
530 | 544 |
|
531 | 545 | if uid != self.http_api.uid: |
@@ -1218,7 +1232,7 @@ async def _subscribe_webhook(self, url: str = "") -> None: |
1218 | 1232 | if not url: |
1219 | 1233 | # start a internall webhook server and use that |
1220 | 1234 | if self._webhook_server is None: |
1221 | | - self._webhook_server = WebhookServer(host=self._host, push_callback=self._webhook_push_callback) |
| 1235 | + self._webhook_server = WebhookServer(host=self._host, push_callback=self.webhook_push_callback) |
1222 | 1236 | await self._webhook_server.start() |
1223 | 1237 | url = self._webhook_server.adress |
1224 | 1238 |
|
|
0 commit comments