Skip to content

Commit 510bc7b

Browse files
committed
Move bytes and json decoding from server to webhook_push_callback
1 parent 57b01c6 commit 510bc7b

2 files changed

Lines changed: 25 additions & 19 deletions

File tree

reolink_aio/baichuan/baichuan.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
from xml.etree import ElementTree as XML
1313

1414
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
1517

1618
from ..const import (
1719
AI_DETECT_CONVERSION,
@@ -517,15 +519,27 @@ def _push_callback(self, cmd_id: int, data: bytes, len_header: int, payload: byt
517519

518520
self._parse_xml(cmd_id, rec_body, payload, mess_id)
519521

520-
def _webhook_push_callback(self, data: dict[str, Any]) -> None:
522+
def webhook_push_callback(self, data: bytes) -> None:
521523
"""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", "")
526540

527541
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)
529543
return
530544

531545
if uid != self.http_api.uid:
@@ -1218,7 +1232,7 @@ async def _subscribe_webhook(self, url: str = "") -> None:
12181232
if not url:
12191233
# start a internall webhook server and use that
12201234
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)
12221236
await self._webhook_server.start()
12231237
url = self._webhook_server.adress
12241238

reolink_aio/baichuan/webhook_server.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,16 @@
33
import asyncio
44
import logging
55
from collections.abc import Callable
6-
from typing import Any
76

87
from aiohttp import web
9-
from orjson import JSONDecodeError # pylint: disable=no-name-in-module
10-
from orjson import loads as json_loads # pylint: disable=no-name-in-module
118

129
_LOGGER = logging.getLogger(__name__)
1310

1411

1512
class WebhookServer:
1613
"""Reolink webhook server to receive Baichuan push callbacks."""
1714

18-
def __init__(self, host: str, port: int = 0, push_callback: Callable[[dict[str, Any]], None] | None = None) -> None:
15+
def __init__(self, host: str, port: int = 0, push_callback: Callable[[bytes], None] | None = None) -> None:
1916
self.port: int = port
2017
self.adress: str = ""
2118
self.local_ip: str = "127.0.0.1"
@@ -27,15 +24,10 @@ def __init__(self, host: str, port: int = 0, push_callback: Callable[[dict[str,
2724

2825
async def handle_webhook(self, request: web.Request) -> web.Response:
2926
"""Handle a incomming message on the webhook."""
30-
text = ""
3127
try:
32-
text = await request.text()
33-
data = json_loads(text)
34-
except JSONDecodeError as err:
35-
_LOGGER.debug("Baichuan server %s: error during decoding json data:\n%s\n%s", self.port, text, err)
36-
return web.Response(text="JSONDecodeError", status=400)
28+
data = await request.read()
3729
except Exception as err:
38-
_LOGGER.debug("Baichuan server %s: error during receiving data:\n%s\n%s", self.port, text, err)
30+
_LOGGER.debug("Baichuan server %s: error during receiving data: %s", self.port, err)
3931
return web.Response(text="Error", status=400)
4032

4133
if self._push_callback is not None:
@@ -44,7 +36,7 @@ async def handle_webhook(self, request: web.Request) -> web.Response:
4436
except Exception as err:
4537
_LOGGER.debug("Baichuan server %s: Error during push callback with data:\n%s\n%s", self.port, data, err)
4638
else:
47-
_LOGGER.debug("Baichuan server %s: Received:\n%s", self.port, data)
39+
_LOGGER.debug("Baichuan server %s: Received:\n%s", self.port, data.decode("utf-8"))
4840

4941
return web.Response(text="OK", status=200)
5042

0 commit comments

Comments
 (0)