Skip to content

Commit

Permalink
Move reques forming back to RPC class
Browse files Browse the repository at this point in the history
  • Loading branch information
bitphage committed Dec 4, 2019
1 parent ecbbb7f commit 8973b87
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 18 deletions.
11 changes: 3 additions & 8 deletions grapheneapi/aio/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,13 @@ async def connect(self):
async def disconnect(self):
await self.session.close()

async def rpcexec(self, *args):
async def rpcexec(self, payload):
""" Execute a RPC call
:param args: args are passed as "params" in json-rpc request:
:param dict payload: json-rpc request in format:
{"jsonrpc": "2.0", "method": "call", "params": "[x, y, z]", "id": 1}
"""

request_id = self.get_request_id()
request = {"jsonrpc": "2.0", "method": "call", "params": args, "id": request_id}

async with self.session.post(self.url, json=request) as response:
async with self.session.post(self.url, json=payload) as response:
response_text = await response.text()

# Return raw response (jsonrpcclient does own parsing)
return response_text
13 changes: 8 additions & 5 deletions grapheneapi/aio/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,21 @@ async def method(*args, **kwargs):
# let's be able to define the num_retries per query
self.num_retries = kwargs.get("num_retries", self.num_retries)

# We're passing only "params" instead of forming full json-rpc query to allow jsonrpcclient handle
# everything
query = [api_id, name, list(args)]
query = {
"jsonrpc": "2.0",
"method": "call",
"params": [api_id, name, list(args)],
"id": self.get_request_id(),
}

# Need to await here!
r = await self.rpcexec(*query)
r = await self.rpcexec(query)
message = self.parse_response(r)

return message

return method

def parse_response(self, *args, **kwargs):
# Disable logging in parent method because we're logging via jsonrpcclient
# Disable logging in parent method because we're logging via client libraries
return super().parse_response(*args, log_on_debug=False, **kwargs)
9 changes: 4 additions & 5 deletions grapheneapi/aio/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,18 +100,17 @@ async def get_response_by_id(self, request_id):
response = self._messages.pop(request_id, None)
return response

async def rpcexec(self, *args):
async def rpcexec(self, payload):
""" Execute a RPC call
:param args: args are passed as "params" in json-rpc request:
:param dict payload: json-rpc request in format:
{"jsonrpc": "2.0", "method": "call", "params": "[x, y, z]", "id": 1}
"""
if not self.ws:
await self.connect()

request_id = self.get_request_id()
request = {"jsonrpc": "2.0", "method": "call", "params": args, "id": request_id}
request_json = json.dumps(request)
request_id = payload["id"]
request_json = json.dumps(payload)

await self.ws.send(request_json)
response = await self.get_response_by_id(request_id)
Expand Down

0 comments on commit 8973b87

Please sign in to comment.