Skip to content

Commit

Permalink
Merge pull request #122 from nott/jsonrpc
Browse files Browse the repository at this point in the history
JsonRPC improvements
  • Loading branch information
fiorix committed Dec 20, 2013
2 parents 40108e2 + 378392f commit 4bd61d0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
35 changes: 23 additions & 12 deletions cyclone/httpclient.py
Expand Up @@ -18,7 +18,7 @@
"""Non-blocking HTTP client"""

import functools
import types
from types import ListType, DictType

from cyclone import escape
from cyclone.web import HTTPError
Expand Down Expand Up @@ -118,7 +118,7 @@ def fetch(self):
headers = dict(response.headers.getAllRawHeaders())
location = headers.get("Location")
if location:
if isinstance(location, types.ListType):
if isinstance(location, ListType):
location = location[0]

#print("redirecting to:", location)
Expand Down Expand Up @@ -210,10 +210,14 @@ class JsonRPC:
Note that in the example above, ``echo`` and ``sort`` are remote methods
provided by the server.
Optional parameters of ``httpclient.fetch`` may also be used.
"""
def __init__(self, url):
def __init__(self, url, *args, **kwargs):
self.__rpcId = 0
self.__rpcUrl = url
self.__fetch_args = args
self.__fetch_kwargs = kwargs

def __getattr__(self, attr):
return functools.partial(self.__rpcRequest, attr)
Expand All @@ -223,21 +227,28 @@ def __rpcRequest(self, method, *args):
"id": self.__rpcId})
self.__rpcId += 1
r = defer.Deferred()
d = fetch(
self.__rpcUrl,
method="POST",
postdata=q,
headers={
"Content-Type": ["application/json-rpc"]
}
)

fetch_kwargs = {
'method': "POST",
'postdata': q,
'headers': {"Content-Type": ["application/json-rpc"]},
}
fetch_kwargs.update(self.__fetch_kwargs)
d = fetch(self.__rpcUrl, *self.__fetch_args, **fetch_kwargs)

def _success(response, deferred):
if response.code == 200:
data = escape.json_decode(response.body)
error = data.get("error")
if error:
deferred.errback(Exception(error))
if isinstance(error, DictType) and 'message' in error:
# JSON-RPC spec is not very verbose about error schema,
# but it should look like {'code': 0, 'message': 'msg'}
deferred.errback(Exception(error['message']))
else:
# For backward compatibility with previous versions of
# cyclone.jsonrpc.JsonrpcRequestHandler
deferred.errback(Exception(error))
else:
deferred.callback(data.get("result"))
else:
Expand Down
4 changes: 1 addition & 3 deletions cyclone/jsonrpc.py
Expand Up @@ -56,8 +56,6 @@ def post(self, *args):
try:
req = cyclone.escape.json_decode(self.request.body)
jsonid = req["id"]
assert isinstance(jsonid, types.IntType), \
"Invalid id type: %s" % type(jsonid)
method = req["method"]
assert isinstance(method, types.StringTypes), \
"Invalid method type: %s" % type(method)
Expand All @@ -79,7 +77,7 @@ def post(self, *args):

def _cbResult(self, result, jsonid):
if isinstance(result, failure.Failure):
error = str(result.value)
error = {'code': 0, 'message': str(result.value)}
result = None
else:
error = None
Expand Down

0 comments on commit 4bd61d0

Please sign in to comment.