Compare commits
No commits in common. "b029eb8d1711c3f0e30d51c699b08de9229d6c32" and "1ecb595beeb75a3fb510a0272c75a2feb2d1bd81" have entirely different histories.
b029eb8d17
...
1ecb595bee
@ -21,19 +21,23 @@ 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:
|
||||||
@ -41,6 +45,7 @@ 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:
|
||||||
@ -50,11 +55,12 @@ 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("Schedule for retry: " + str(data))
|
logger.debug("Sending echo to server: " + 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):
|
||||||
@ -64,7 +70,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:
|
||||||
logger.exception(ex)
|
print(ex)
|
||||||
|
|
||||||
async def returner():
|
async def returner():
|
||||||
while True:
|
while True:
|
||||||
@ -98,7 +104,7 @@ class RPC:
|
|||||||
try:
|
try:
|
||||||
path.unlink()
|
path.unlink()
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
logger.error(ex)
|
print("Error deleting temporary file:", ex)
|
||||||
|
|
||||||
return response
|
return response
|
||||||
|
|
||||||
@ -107,12 +113,13 @@ class RPC:
|
|||||||
try:
|
try:
|
||||||
msg = await self.ws.receive()
|
msg = await self.ws.receive()
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
logger.exception("Error while receiving:", ex)
|
print("Error while receiving:", ex)
|
||||||
break
|
break
|
||||||
if msg.type == aiohttp.WSMsgType.CLOSED:
|
if msg.type == aiohttp.WSMsgType.CLOSED:
|
||||||
raise Exception("WebSocket closed.")
|
break
|
||||||
elif msg.type == aiohttp.WSMsgType.ERROR:
|
elif msg.type == aiohttp.WSMsgType.ERROR:
|
||||||
raise Exception("WebSocket error:", msg)
|
print("WebSocket error:", msg)
|
||||||
|
break
|
||||||
elif msg.type == aiohttp.WSMsgType.TEXT:
|
elif msg.type == aiohttp.WSMsgType.TEXT:
|
||||||
|
|
||||||
if self.current_call_id and not msg.json().get('callId') != self.current_call_id:
|
if self.current_call_id and not msg.json().get('callId') != self.current_call_id:
|
||||||
@ -123,8 +130,9 @@ class RPC:
|
|||||||
self.current_call_id = None
|
self.current_call_id = None
|
||||||
return response
|
return response
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
logger.exception(ex)
|
print("Error while parsing message:", msg, ex)
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
raise Exception("Unexpected message type.")
|
raise Exception("Unexpected message type.")
|
||||||
|
print("huh")
|
||||||
return None
|
return None
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user