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]
name = "reportmonster_client"
version = "0.0.2"
version = "0.0.3"
authors = [
{ name="Kumi Systems e.U.", email="office@kumi.systems" },
]

View file

@ -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:

View file

@ -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()))
output = asyncio.run(test())
print(output.content)