def __call__(self, *args): postdata = dumps({"method": self.__serviceName, 'params': args, 'id':'jsonrpc'}) respdata = urllib.urlopen(self.__serviceURL, postdata).read() resp = loads(respdata) if resp['error'] != None: raise JSONRPCException(resp['error']) else: return resp['result']
def getNonce(self): ServiceProxy.__request_id += 1 postdata = dumps({"method": "system.getNonce", 'params': [], 'id': ServiceProxy.__request_id }) respdata = self.__handler.make_request( self.__serviceURL, postdata ) resp = loads(respdata) if 'error' in resp and resp['error'] != None: print("A little error: ", resp['error']) raise JSONRPCException(resp['error']) self.nonce = resp['result']
def __call__(self, *args, **kwargs): """Issue a remote procedure call using JSON-RPC 2.0""" self._idcnt += 1 if args and kwargs: raise JSONRPCException({ 'code': -32600, 'message': 'Cannot use both positional ' 'and keyword arguments ' '(according to JSON-RPC spec.)'}) postdata = dumps({ 'id': self._idcnt, 'jsonrpc': self._jsonrpc, 'method': self._name, 'params': args or kwargs, }) self._conn.request('POST', self._url.path, postdata, self._headers) httpresp = self._conn.getresponse() if httpresp is None: raise JSONRPCException({ 'code': -342, 'message': 'missing HTTP response from the server', }) resp = httpresp.read().decode(self._encoding) if self._use_decimal: resp = loads(resp, parse_float=decimal.Decimal) else: resp = loads(resp) error = resp.get('error', None) if error is not None: raise JSONRPCException(error) try: return resp['result'] except KeyError: raise JSONRPCException({ 'code': -32600, 'message': 'missing result in JSON response', })
def __call__(self, *args): postdata = dumps({"method": self.__serviceName, 'params': args[:-1], 'id':'jsonrpc'}) # TODO: make this asynchronous - because we can. respdata = urllib.urlopen(self.__serviceURL, postdata).read() resp = loads(respdata) resp_info = JSONRequestInfo(resp['id'], self.__serviceName, args[-1]) print resp if resp['error'] == None: args[-1].onRemoteResponse(resp['result'], resp_info) return resp['id'] else: args[-1].onRemoteError(resp.get('code'), resp['error'], resp_info) return -1
def __call__(self, *args): ServiceProxy.__request_id += 1 postdata = dumps({"method": self.__serviceName, 'params': args, 'nonce': self.nonce, 'id': ServiceProxy.__request_id }) respdata = self.__handler.make_request( self.__serviceURL, postdata ) resp = loads(respdata) if 'error' in resp and resp['error'] != None: print("A little error: ", resp['error']) raise JSONRPCException(resp['error']) self.__fix_callable_refs( resp ) result = resp['result'] if 'fixups' in resp and type( resp['fixups'] ) is list: self.__handle_fixups__( result, resp['fixups'] ) return result
def __call__(self, *args): postdata = dumps({'method': self.__serviceName, 'params': args, 'id':'jsonrpc'}) request = urllib2.Request(self.__serviceURL, postdata, {'Content-type': 'application/json'}) respdata = urllib2.urlopen(request).read() try: resp = loads(respdata) if resp.get('error'): raise JSONRPCException(resp['error']) else: return resp['result'] except JSONDecodeException: raise JSONDecodeException(respdata)
def onCompletion(self, json_str): try: response = loads(json_str) except JSONDecodeException: # err.... help?!! error = dict( code=-32700, message="Parse error while decoding response", data=None, ) self.request.handler.onRemoteError(0, error, self.request) return if not response: error = dict( code=-32603, message="Empty Response", data=None, ) self.request.handler.onRemoteError(0, error, self.request) elif response.get("error"): error = response["error"] jsonrpc = response.get("jsonrpc") code = error.get("code", 0) message = error.get("message", error) data = error.get("data") if not jsonrpc: jsonrpc = response.get("version", "1.0") if jsonrpc == "1.0": message = error else: data = error.get("error") error = dict( code=code, message=message, data=data, ) self.request.handler.onRemoteError(0, error, self.request) elif "result" in response: self.request.handler.onRemoteResponse(response["result"], self.request) else: error = dict( code=-32603, message="No result or error in response", data=response, ) self.request.handler.onRemoteError(0, error, self.request)
def onCompletion(self, json_str): from jsonrpc.json import loads, JSONDecodeException try: response = loads(json_str) except JSONDecodeException: # err.... help?!! self.request.handler.onRemoteError(0, "decode failure", None) return if not response: self.request.handler.onRemoteError(0, "Server Error or Invalid Response", self.request) elif response.get("error"): error = response["error"] self.request.handler.onRemoteError(error["code"], error["message"], self.request) else: self.request.handler.onRemoteResponse(response["result"], self.request)
def __call__(self, *args): postdata = dumps({"method": self.__serviceName, 'params': args[:-1], 'id':'jsonrpc'}) # TODO: make this asynchronous - because we can. respdata = urllib.urlopen(self.__serviceURL, postdata).read() try: resp = loads(respdata) except JSONDecodeException: # err.... help?!! args[-1].onRemoteError(0, "decode failure", None) return -1 resp_info = JSONRequestInfo(resp['id'], self.__serviceName, args[-1]) print "resp", resp if not resp: args[-1].onRemoteError(0, "decode failure", None) return -1 if not resp.has_key('error') or resp['error'] == None: args[-1].onRemoteResponse(resp['result'], resp_info) return resp['id'] else: args[-1].onRemoteError(resp.get('code'), resp['error'], resp_info) return -1
def __call__(self, *args, **kwargs): headers = kwargs.pop('headers', {}) timeout = kwargs.pop('timeout', DEFAULT_URLLIB2_TIMEOUT) params = kwargs if len(kwargs) else args postdata = dumps({ 'jsonrpc': self.__serviceVersion, "method": self.__serviceName, 'params': params, 'id':'jsonrpc'}) postdata = postdata.encode('utf-8') req = urllib2.Request(self.__serviceURL, postdata, headers) try: respdata = urllib2.urlopen(req, timeout=timeout).read() except urllib2.HTTPError as err: # try read content respdata = err.read() except urllib2.URLError as err: # return the reason of error raise Exception(err.reason[-1]) resp = loads(respdata) if resp['error'] != None: raise JSONRPCException(resp['error']) else: return resp['result']
def check_json_precision(): """Make sure json library being used does not lose precision converting BTC values""" n = Decimal("20000000.00000003") satoshis = int(json.loads(json.dumps(float(n)))*1.0e8) if satoshis != 2000000000000003: raise RuntimeError("JSON encode/decode loses precision")
# This file is part of Uriel. # Uriel is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 3 of the License, or # (at your option) any later version. # Uriel is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this library. If not, see <http://www.gnu.org/licenses/> from jsonrpc.json import dumps, loads data = dumps({"method": "runTest", 'params': (1, "yes", True), 'id':'jsonrpc'}) print repr(data) objs = loads(data) print repr(objs) #class ServiceProxy(object): # def __init__(self, addr, name): # pass # def __call__(self, *args):
def check_json_precision(): """Make sure json library being used does not lose precision converting BTC values""" n = Decimal("20000000.00000003") satoshis = int(json.loads(json.dumps(float(n))) * 1.0e8) if satoshis != 2000000000000003: raise RuntimeError("JSON encode/decode loses precision")