def sendNotify(self, method, params): # jsonrpc: A String specifying the version of the JSON-RPC protocol. # MUST be exactly "2.0" # If jsonrpc is missing, the server MAY handle the Request as # JSON-RPC V1.0-Request. # version: String specifying the version of the JSON-RPC protocol. # MUST be exactly "1.1" # NOTE: We send both, to indicate that we can handle both. # # id: If omitted, the Request is a Notification # NOTE: JSON-RPC 1.0 uses an id of Null for Notifications. # method: A String containing the name of the procedure to be invoked. # params: An Array or Object, that holds the actual parameter values # for the invocation of the procedure. Can be omitted if # empty. # NOTE: JSON-RPC 1.0 only a non-empty Array is used # From the spec of 1.1: # The Content-Type MUST be specified and # SHOULD read # application/json. # The Accept MUST be specified and SHOULD read application/json. # msg = {"jsonrpc": "2.0", "version": "1.1", "method": method, "params": params } msg_data = dumps(msg) if not HTTPRequest().asyncPost(self.url, msg_data, self, False, "text/json", self.headers): return -1 return 1
def sendNotify(self, method, params): from jsonrpc.json import dumps msg = {"id": None, "method": method, "params": params} msg_data = dumps(msg) if not HTTPRequest().asyncPost(self.url, msg_data, self, content_type="text/x-json"): return -1 return 1
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 _sendRequest(self, method, params, handler): id = pygwt.getNextHashId() msg = {"id":id, "method":method, "params":params} msg_data = dumps(msg) request_info = JSONRequestInfo(id, method, handler) if not HTTPRequest().asyncPost(self.url, msg_data, JSONResponseTextHandler(request_info)): return -1 return id
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 sendRequest(self, method, params, handler): from jsonrpc.json import dumps id = pygwt.getNextHashId() msg = {"id":id, "method":method, "params":params} msg_data = dumps(msg) request_info = JSONRequestInfo(id, method, handler) if not HTTPRequest().asyncPost(self.url, msg_data, JSONResponseTextHandler(request_info), content_type="text/x-json"): return -1 return id
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 __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 sendRequest(self, method, params, handler): id = nextRequestID() msg = {"jsonrpc": "2.0", "id": id, "method": method, "params": params } msg_data = dumps(msg) request_info = JSONRequestInfo(id, method, handler) if not HTTPRequest().asyncPost(self.url, msg_data, JSONResponseTextHandler(request_info), False, "text/json", self.headers): return -1 return id
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() 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']
# 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")
def _sendNotify(self, method, params): msg = {"id":None, "method":method, "params":params} msg_data = dumps(msg) if not HTTPRequest().asyncPost(self.url, msg_data, self): return -1 return 1