Created
January 7, 2018 21:57
-
-
Save athoune/f425aed8645c0a1e485c12f1ad87c339 to your computer and use it in GitHub Desktop.
jsonrpc2 with aiohttp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
from jsonrpc.jsonrpc2 import JSONRPC20Request | |
def jsonrpcrequest(data): | |
logging.debug(data) | |
if isinstance(data, list): | |
for d in data: | |
yield JSONRPC20Request.from_data(d) | |
else: | |
if data is not None: | |
yield JSONRPC20Request.from_data(data) | |
class Session: | |
def __init__(self, ws): | |
self.ids = set() | |
self.ws = ws | |
self.queue = list() | |
def __aiter__(self): | |
return self | |
async def __anext__(self): | |
while len(self.queue) == 0: | |
msg = None | |
try: | |
msg = await self.ws.receive_json() | |
except TypeError: | |
continue | |
except RuntimeError as e: | |
if e.args[0] == 'WebSocket connection is closed.': | |
raise StopAsyncIteration | |
raise e | |
finally: | |
logging.debug(msg) | |
for req in jsonrpcrequest(msg): | |
self.queue.append(req) | |
req = self.queue.pop() | |
_id = req._id | |
if _id in self.ids: | |
raise Exception("Replayed id") | |
self.ids.add(_id) | |
return req |
Author
athoune
commented
Jan 7, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment