def call(self, method, *args, **kwargs): """ Creates the JSON-RPC request string, calls the HTTP server, converts JSON-RPC response string to python and returns the result. :param method: Name of the method which will be called on the HTTP server. Or a list with RPC-Request-Dictionaries. Syntax:: "<MethodName>" or [<JsonRpcRequestDict>, ...] RPC-Request-Dictionaries will be made with the function *rpcrequest.create_request_dict()*. """ # Create JSON-RPC-request if isinstance(method, string_types): request_json = rpcrequest.create_request_json(method, *args, **kwargs) else: assert not args and not kwargs request_json = rpcjson.dumps(method) # Call the HTTP-JSON-RPC server response_json = http_request( url = self.url, json_string = request_json, username = self.username, password = self.password, timeout = self.timeout, additional_headers = self.additional_headers, content_type = self.content_type, cookies = self.cookies, gzipped = self.gzipped, ssl_context = self.ssl_context, debug = self.debug ) if not response_json: return # Convert JSON-RPC-response to python-object response = rpcresponse.parse_response_json(response_json) if isinstance(response, rpcresponse.Response): if response.error: # Raise error if response.error.code in rpcerror.jsonrpcerrors: raise rpcerror.jsonrpcerrors[response.error.code]( message = response.error.message, data = response.error.data ) else: raise rpcerror.JsonRpcError( message = response.error.message, data = response.error.data, code = response.error.code ) else: # Return result return response.result elif isinstance(response, list): # Bei Listen wird keine Fehlerauswerung gemacht return response
def call(self, json_request): """ Parses the *json_request*, calls the function(s) and returns the *json_response*. :param json_request: JSON-RPC-string with one or more JSON-RPC-requests :return: JSON-RPC-string with one or more responses. """ # List for the responses responses = [] # List with requests requests = rpcrequest.parse_request_json(json_request) if not isinstance(requests, list): requests = [requests] # Every JSON-RPC request in a batch of requests for request in requests: # Request-Data jsonrpc = request.jsonrpc id = request.id method = request.get("method", "") if not method in self.methods: # Method not found error responses.append( rpcresponse.Response( jsonrpc = jsonrpc, id = id, error = rpcerror.MethodNotFound( data = u"Method name: '%s'" % method ) ) ) continue # split positional and named params positional_params, named_params = request.get_splitted_params() # Call the method with parameters try: rpc_function = self.methods[method] (result, centec_error_msg) = rpc_function(*positional_params, **named_params) # No return value is OK if we don´t have an ID (=notification) if result is None: if id: responses.append( rpcresponse.Response( jsonrpc = jsonrpc, id = id, error = rpcerror.InternalError( data = u"No result from JSON-RPC method." ) ) ) else: #Centec centec_error = None if centec_error_msg != None: centec_error = rpcerror.JsonRpcError(message = centec_error_msg['message'], data = result) centec_error.code = centec_error_msg['code'] # Successful response responses.append( rpcresponse.Response(jsonrpc = jsonrpc, id = id, result = result, error=centec_error) ) except TypeError, err: traceback_info = "".join(traceback.format_exception(*sys.exc_info())) if "takes exactly" in unicode(err) and "arguments" in unicode(err): responses.append( rpcresponse.Response( jsonrpc = jsonrpc, id = id, error = rpcerror.InvalidParams(data = traceback_info) ) ) else: responses.append( rpcresponse.Response( jsonrpc = jsonrpc, id = id, error = rpcerror.InternalError(data = traceback_info) ) ) except BaseException, err: traceback_info = "".join(traceback.format_exception(*sys.exc_info())) if hasattr(err, "data"): error_data = err.data else: error_data = None responses.append( rpcresponse.Response( jsonrpc = jsonrpc, id = id, error = rpcerror.InternalError( data = error_data or traceback_info ) ) )