def process_response(self, response, log_extra=None, log_format=None): """ Process the response and return the 'result' portion if present. :param response: The JSON-RPC response string to process. :return: The response string, or None """ if response: # Log the response before processing it self.log_response(response, log_extra, log_format) # If it's a json string, parse to object if isinstance(response, basestring): try: response = json.loads(response) except ValueError: raise exceptions.ParseResponseError() # Validate the response against the Response schema (raises # jsonschema.ValidationError if invalid) if config.validate: self.validator.validate(response) if isinstance(response, list): # Batch request - just return the whole response return response else: # If the response was "error", raise to ensure it's handled if 'error' in response and response['error'] is not None: raise GenericJsonRpcServerError( code=JsonError.INVALID_REQUEST, message=response['error'].get('message'), http_status=status.HTTP_BAD_REQUEST) # All was successful, return just the result part return response.get('result') # No response was given return None
def get_channel_stub_by_channel_name(channel_name): try: channel_stub = StubCollection().channel_stubs[channel_name] except KeyError: raise GenericJsonRpcServerError(code=JsonError.INVALID_REQUEST, message="Invalid channel name", http_status=status.HTTP_BAD_REQUEST) else: return channel_stub
async def get_block_by_params(channel_name=None, block_height=None, block_hash="", with_commit_state=False): channel_name = StubCollection().conf[ ConfigKey.CHANNEL] if channel_name is None else channel_name block_data_filter = "prev_block_hash, height, block_hash, merkle_tree_root_hash," \ " time_stamp, peer_id, signature" tx_data_filter = "icx_origin_data" try: channel_stub = get_channel_stub_by_channel_name(channel_name) except KeyError: raise GenericJsonRpcServerError(code=JsonError.INVALID_REQUEST, message="Invalid channel name", http_status=status.HTTP_BAD_REQUEST) response_code, block_hash, block_data_json, tx_data_json_list = \ await channel_stub.async_task().get_block( block_height=block_height, block_hash=block_hash, block_data_filter=block_data_filter, tx_data_filter=tx_data_filter ) try: block = json.loads( block_data_json ) if response_code == message_code.Response.success else {} except Exception as e: logging.error(f"get_block_by_params error caused by : {e}") block = {} result = {'response_code': response_code, 'block': block} if 'commit_state' in result['block'] and not with_commit_state: del result['block']['commit_state'] return block_hash, result