def jsonrpc_server_call(target, jsonrpc_request, json_decoder=None): """Execute the given JSON-RPC request (as JSON-encoded string) on the given target object and return the JSON-RPC response, as a dict """ if json_decoder is None: json_decoder = ScrapyJSONDecoder() try: req = json_decoder.decode(jsonrpc_request.decode('utf-8')) except Exception as e: return jsonrpc_error(None, jsonrpc_errors.PARSE_ERROR, 'Parse error', traceback.format_exc()) try: id, methname = req['id'], req['method'] except KeyError: return jsonrpc_error(None, jsonrpc_errors.INVALID_REQUEST, 'Invalid Request') try: method = getattr(target, methname) except AttributeError: return jsonrpc_error(id, jsonrpc_errors.METHOD_NOT_FOUND, 'Method not found') params = req.get('params', []) a, kw = ([], params) if isinstance(params, dict) else (params, {}) kw = dict([(str(k), v) for k, v in kw.items()]) # convert kw keys to str try: return jsonrpc_result(id, method(*a, **kw)) except Exception as e: return jsonrpc_error(id, jsonrpc_errors.INTERNAL_ERROR, str(e), \ traceback.format_exc())
def jsonrpc_server_call(target, jsonrpc_request, json_decoder=None): """Execute the given JSON-RPC request (as JSON-encoded string) on the given target object and return the JSON-RPC response, as a dict """ if json_decoder is None: json_decoder = ScrapyJSONDecoder() try: req = json_decoder.decode(jsonrpc_request) except Exception as e: return jsonrpc_error(None, jsonrpc_errors.PARSE_ERROR, 'Parse error', traceback.format_exc()) try: id, methname = req['id'], req['method'] except KeyError: return jsonrpc_error(None, jsonrpc_errors.INVALID_REQUEST, 'Invalid Request') try: method = getattr(target, methname) except AttributeError: return jsonrpc_error(id, jsonrpc_errors.METHOD_NOT_FOUND, 'Method not found') params = req.get('params', []) a, kw = ([], params) if isinstance(params, dict) else (params, {}) kw = dict([(str(k), v) for k, v in kw.items()]) # convert kw keys to str try: return jsonrpc_result(id, method(*a, **kw)) except Exception as e: return jsonrpc_error(id, jsonrpc_errors.INTERNAL_ERROR, str(e), \ traceback.format_exc())
def setUp(self): self.spider1 = Spider('name1') self.spider2 = Spider('name2') open_spiders = set([self.spider1, self.spider2]) crawler = CrawlerMock(open_spiders) self.spref = SpiderReferencer(crawler) self.encoder = ScrapyJSONEncoder(spref=self.spref) self.decoder = ScrapyJSONDecoder(spref=self.spref)
def setUp(self): crawler = CrawlerMock([]) self.json_decoder = ScrapyJSONDecoder(crawler=crawler)
def __init__(self, crawler, target=None): JsonResource.__init__(self, crawler, target) self.json_decoder = ScrapyJSONDecoder(crawler=crawler) self.crawler = crawler self._target = target
def __init__(self, crawler, target=None): super(JsonRpcResource, self).__init__(crawler, target) self.json_decoder = ScrapyJSONDecoder(crawler=crawler) self.crawler = crawler self._target = target