pykirdy: Update handling of json objects received

- Accept new line delimited json in read_response()
This commit is contained in:
linuswck 2024-03-25 12:40:46 +08:00
parent b1a1173075
commit a13c6fa86c

View File

@ -622,20 +622,26 @@ class Kirdy:
self._reader = None
self._writer = None
async def _read_response(self):
async def _read_response(self, buffer_size=16384):
"""
Decode newline delimited Json objects and return the latest json received inside the buffer.
- buffer_size: Integer
"""
try:
response = await asyncio.wait_for(self._reader.read(1024), self.timeout)
raw_response = await asyncio.wait_for(self._reader.read(buffer_size), self.timeout)
except asyncio.exceptions.TimeoutError:
return {
"msg_type": "Internal Timeout"
}
response = response.decode('utf-8', errors='ignore')
try:
return json.loads(response)
except json.decoder.JSONDecodeError:
return {
"msg_type": "Internal Invalid"
}
response = raw_response.decode('utf-8', errors='ignore').split("\n")
for item in reversed(response):
try:
return json.loads(item)
except json.decoder.JSONDecodeError:
pass
return { "msg_type": "Internal No json object found in response" }
# If the cmd involves a cmd specific data type,
# checking is done separately within the functions being called