def get_raw_message( self, api: LedgerApi, message: ContractApiMessage, dialogue: ContractApiDialogue, ) -> ContractApiMessage: """ Send the request 'get_raw_message'. :param api: the API object. :param message: the Ledger API message :param dialogue: the contract API dialogue :return: None """ contract = self.contract_registry.make(message.contract_id) method_to_call = getattr(contract, message.callable) try: rm = method_to_call(api, message.contract_address, **message.kwargs.body) response = ContractApiMessage( performative=ContractApiMessage.Performative.RAW_MESSAGE, message_id=message.message_id + 1, target=message.message_id, dialogue_reference=dialogue.dialogue_label.dialogue_reference, raw_message=RawMessage(message.ledger_id, rm), ) response.counterparty = message.counterparty dialogue.update(response) except Exception as e: # pylint: disable=broad-except # pragma: nocover response = self.get_error_message(e, api, message, dialogue) return response
def dispatch_request( self, ledger_api: LedgerApi, message: ContractApiMessage, dialogue: ContractApiDialogue, response_builder: Callable[[bytes], ContractApiMessage], ) -> ContractApiMessage: """ Dispatch a request to a user-defined contract method. :param ledger_api: the ledger apis. :param message: the contract API request message. :param dialogue: the contract API dialogue. :param response_builder: callable that from bytes builds a contract API message. :return: the response message. """ contract = self.contract_registry.make(message.contract_id) try: data = self._get_data(ledger_api, message, contract) response = response_builder(data) response.counterparty = message.counterparty dialogue.update(response) except AEAException as e: self.logger.error(str(e)) response = self.get_error_message(e, ledger_api, message, dialogue) except Exception as e: # pylint: disable=broad-except # pragma: nocover self.logger.error( f"An error occurred while processing the contract api request: '{str(e)}'." ) response = self.get_error_message(e, ledger_api, message, dialogue) return response