def send_request(self, method, params=None, callback=None): """Sends a JSON RPC request to the client. Args: method(str): The method name of the message to send params(any): The payload of the message Returns: Future that will be resolved once a response has been received """ msg_id = str(uuid.uuid4()) logger.debug('Sending request with id "%s": %s %s', msg_id, method, params) request = JsonRPCRequestMessage( id=msg_id, jsonrpc=JsonRPCProtocol.VERSION, method=method, params=params ) future = Future() # If callback function is given, call it when result is received if callback: def wrapper(future: Future): result = future.result() logger.info('Client response for %s received: %s', params, result) callback(result) future.add_done_callback(wrapper) self._server_request_futures[msg_id] = future self._send_data(request) return future
def deserialize_message(data, get_params_type=get_method_params_type): """Function used to deserialize data received from client.""" if 'jsonrpc' in data: try: deserialize_params(data, get_params_type) except ValueError: raise JsonRpcInvalidParams() if 'id' in data: if 'method' in data: return JsonRPCRequestMessage(**data) else: return JsonRPCResponseMessage(**data) else: return JsonRPCNotification(**data) return data