Making it actually work
This commit is contained in:
parent
36ddbcadb1
commit
afc3ff4b4f
3 changed files with 18 additions and 9 deletions
|
@ -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" },
|
||||
]
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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)
|
Loading…
Reference in a new issue