Refactored messages.
This commit is contained in:
parent
51cf3fb500
commit
b029eb8d17
@ -21,23 +21,19 @@ logger = logging.getLogger("snekbot.rpc")
|
|||||||
class RPC:
|
class RPC:
|
||||||
class Response:
|
class Response:
|
||||||
def __init__(self, msg):
|
def __init__(self, msg):
|
||||||
logger.debug("Initializing response.")
|
|
||||||
if isinstance(msg, list):
|
if isinstance(msg, list):
|
||||||
self.list = msg
|
self.list = msg
|
||||||
self.__dict__.update(msg)
|
self.__dict__.update(msg)
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
logger.debug("Iterating sync trough result data.")
|
|
||||||
for k in self.__dict__.get("data", []):
|
for k in self.__dict__.get("data", []):
|
||||||
yield k
|
yield k
|
||||||
|
|
||||||
async def __aiter__(self):
|
async def __aiter__(self):
|
||||||
logger.debug("Iterating async trough result data.")
|
|
||||||
for k in self.__dict__.get("data", []):
|
for k in self.__dict__.get("data", []):
|
||||||
yield k
|
yield k
|
||||||
|
|
||||||
def __getitem__(self, name):
|
def __getitem__(self, name):
|
||||||
logger.debug("Getting result data: " + name + ".")
|
|
||||||
try:
|
try:
|
||||||
return self.__dict__[name]
|
return self.__dict__[name]
|
||||||
except:
|
except:
|
||||||
@ -45,7 +41,6 @@ class RPC:
|
|||||||
return self.__dict__.get('data',{})[name]
|
return self.__dict__.get('data',{})[name]
|
||||||
|
|
||||||
def __setitem__(self, name, value):
|
def __setitem__(self, name, value):
|
||||||
logger.debug("Setting result data: " + name + ".")
|
|
||||||
if not name in self.__dict__.get('data',{}):
|
if not name in self.__dict__.get('data',{}):
|
||||||
self.__dict__[name] = value
|
self.__dict__[name] = value
|
||||||
else:
|
else:
|
||||||
@ -55,12 +50,11 @@ class RPC:
|
|||||||
return json.dumps(self.__dict__, default=str, indent=2)
|
return json.dumps(self.__dict__, default=str, indent=2)
|
||||||
|
|
||||||
def __init__(self, ws):
|
def __init__(self, ws):
|
||||||
logger.debug("Initializing RPC.")
|
|
||||||
self.ws = ws
|
self.ws = ws
|
||||||
self.current_call_id = None
|
self.current_call_id = None
|
||||||
|
|
||||||
async def echo(self, data):
|
async def echo(self, data):
|
||||||
logger.debug("Sending echo to server: " + str(data))
|
logger.debug("Schedule for retry: " + str(data))
|
||||||
await self.ws.send_json(dict(method="echo",args=[data]))
|
await self.ws.send_json(dict(method="echo",args=[data]))
|
||||||
|
|
||||||
def __getattr__(self, name):
|
def __getattr__(self, name):
|
||||||
@ -70,7 +64,7 @@ class RPC:
|
|||||||
try:
|
try:
|
||||||
await self.ws.send_json(payload)
|
await self.ws.send_json(payload)
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
print(ex)
|
logger.exception(ex)
|
||||||
|
|
||||||
async def returner():
|
async def returner():
|
||||||
while True:
|
while True:
|
||||||
@ -104,7 +98,7 @@ class RPC:
|
|||||||
try:
|
try:
|
||||||
path.unlink()
|
path.unlink()
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
print("Error deleting temporary file:", ex)
|
logger.error(ex)
|
||||||
|
|
||||||
return response
|
return response
|
||||||
|
|
||||||
@ -113,7 +107,7 @@ class RPC:
|
|||||||
try:
|
try:
|
||||||
msg = await self.ws.receive()
|
msg = await self.ws.receive()
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
print("Error while receiving:", ex)
|
logger.exception("Error while receiving:", ex)
|
||||||
break
|
break
|
||||||
if msg.type == aiohttp.WSMsgType.CLOSED:
|
if msg.type == aiohttp.WSMsgType.CLOSED:
|
||||||
raise Exception("WebSocket closed.")
|
raise Exception("WebSocket closed.")
|
||||||
@ -129,7 +123,7 @@ class RPC:
|
|||||||
self.current_call_id = None
|
self.current_call_id = None
|
||||||
return response
|
return response
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
print("Error while parsing message:", msg, ex)
|
logger.exception(ex)
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
raise Exception("Unexpected message type.")
|
raise Exception("Unexpected message type.")
|
||||||
|
Loading…
Reference in New Issue
Block a user