Wrap responses

This commit is contained in:
Kumi 2022-07-19 12:20:17 +02:00
parent 3a98541524
commit 4aabaa9d9c
Signed by: kumi
GPG key ID: 6C2B851B15DF1681

View file

@ -41,15 +41,21 @@ class ReportMonsterClient:
async def connect(self): async def connect(self):
self._socket_reader, self._socket_writer = await asyncio.open_connection(self.host, self.port) self._socket_reader, self._socket_writer = await asyncio.open_connection(self.host, self.port)
identification = await self.read() identification = await self.read(wrap=False)
return identification return identification
async def read(self): async def auth(self):
content = await self._socket_reader.readuntil(b"\n> ") response = await self.write(f"login {self.username} {self.password}")
return content.decode().rstrip().rstrip(">").rstrip() return response
async def write(self, message): async def read(self, wrap=True):
content = await self._socket_reader.readuntil(b"\n> ")
stripped = content.decode().rstrip().rstrip(">").rstrip()
return ReportMonsterResponse(stripped) if wrap else stripped
async def write(self, message, read=True, wrap=True):
await self._socket_writer.write(message.encode()) await self._socket_writer.write(message.encode())
return self.read(wrap=wrap) if read else None
async def disconnect(self): async def disconnect(self):
if self._socket_writer: if self._socket_writer: