From afc3ff4b4f4e81c0da38ee4f3e71da930a930223 Mon Sep 17 00:00:00 2001 From: Kumi Date: Tue, 19 Jul 2022 16:43:37 +0200 Subject: [PATCH] Making it actually work --- pyproject.toml | 2 +- src/reportmonster_client/client.py | 18 ++++++++++++------ src/reportmonster_client/test.py | 7 +++++-- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 6a23d61..3048cc6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "hatchling.build" [project] name = "reportmonster_client" -version = "0.0.2" +version = "0.0.3" authors = [ { name="Kumi Systems e.U.", email="office@kumi.systems" }, ] diff --git a/src/reportmonster_client/client.py b/src/reportmonster_client/client.py index ac015c7..1652d85 100644 --- a/src/reportmonster_client/client.py +++ b/src/reportmonster_client/client.py @@ -19,14 +19,14 @@ class ReportMonsterResponse: def content(self) -> str | dict | list: text = ":".join(self._raw.split(":")[1:]).strip() - if self.status == 20: + if self.status[0] == 20: return loads(text) return text @property def error(self) -> bool: - return self.status >= 90 + return self.status[0] >= 90 class ReportMonsterClient: @@ -49,13 +49,19 @@ class ReportMonsterClient: return response async def read(self, wrap=True): - content = await self._socket_reader.readuntil(b"\n> ") - stripped = content.decode().rstrip().rstrip(">").rstrip() + response = "" + while not response.endswith("\n> "): + data = await self._socket_reader.read(1) + if not (plain := data.decode()): + break + response += plain + stripped = response.strip().strip(">").strip() return ReportMonsterResponse(stripped) if wrap else stripped async def write(self, message, read=True, wrap=True): - await self._socket_writer.write(message.encode()) - return self.read(wrap=wrap) if read else None + self._socket_writer.write(message.encode() + b"\n") + await self._socket_writer.drain() + return await self.read(wrap=wrap) if read else None async def disconnect(self): if self._socket_writer: diff --git a/src/reportmonster_client/test.py b/src/reportmonster_client/test.py index 2943594..5669c7e 100644 --- a/src/reportmonster_client/test.py +++ b/src/reportmonster_client/test.py @@ -4,6 +4,9 @@ import asyncio async def test(): client = ReportMonsterClient("test", "test") - return await client.connect() + await client.connect() + await client.auth() + return await client.write("users Testcademy") -print(asyncio.run(test())) \ No newline at end of file +output = asyncio.run(test()) +print(output.content) \ No newline at end of file