Making it actually work

This commit is contained in:
Kumi 2022-07-19 16:43:37 +02:00
parent 36ddbcadb1
commit afc3ff4b4f
Signed by: kumi
GPG key ID: 6C2B851B15DF1681
3 changed files with 18 additions and 9 deletions

View file

@ -4,7 +4,7 @@ build-backend = "hatchling.build"
[project] [project]
name = "reportmonster_client" name = "reportmonster_client"
version = "0.0.2" version = "0.0.3"
authors = [ authors = [
{ name="Kumi Systems e.U.", email="office@kumi.systems" }, { name="Kumi Systems e.U.", email="office@kumi.systems" },
] ]

View file

@ -19,14 +19,14 @@ class ReportMonsterResponse:
def content(self) -> str | dict | list: def content(self) -> str | dict | list:
text = ":".join(self._raw.split(":")[1:]).strip() text = ":".join(self._raw.split(":")[1:]).strip()
if self.status == 20: if self.status[0] == 20:
return loads(text) return loads(text)
return text return text
@property @property
def error(self) -> bool: def error(self) -> bool:
return self.status >= 90 return self.status[0] >= 90
class ReportMonsterClient: class ReportMonsterClient:
@ -49,13 +49,19 @@ class ReportMonsterClient:
return response return response
async def read(self, wrap=True): async def read(self, wrap=True):
content = await self._socket_reader.readuntil(b"\n> ") response = ""
stripped = content.decode().rstrip().rstrip(">").rstrip() 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 return ReportMonsterResponse(stripped) if wrap else stripped
async def write(self, message, read=True, wrap=True): async def write(self, message, read=True, wrap=True):
await self._socket_writer.write(message.encode()) self._socket_writer.write(message.encode() + b"\n")
return self.read(wrap=wrap) if read else None await self._socket_writer.drain()
return await self.read(wrap=wrap) if read else None
async def disconnect(self): async def disconnect(self):
if self._socket_writer: if self._socket_writer:

View file

@ -4,6 +4,9 @@ import asyncio
async def test(): async def test():
client = ReportMonsterClient("test", "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())) output = asyncio.run(test())
print(output.content)