From 7a4752498d319c68924f52826b88e8be1661c0f0 Mon Sep 17 00:00:00 2001 From: jasquat Date: Tue, 24 Jan 2023 16:18:10 -0500 Subject: [PATCH] updated waku send message to take an array of recipients w/ burnettk --- .../connector_waku/commands/sendMessage.py | 57 +++++++++++-------- 1 file changed, 34 insertions(+), 23 deletions(-) diff --git a/connectors/connector-waku/connector_waku/commands/sendMessage.py b/connectors/connector-waku/connector_waku/commands/sendMessage.py index d1183c6..0a32f86 100644 --- a/connectors/connector-waku/connector_waku/commands/sendMessage.py +++ b/connectors/connector-waku/connector_waku/commands/sendMessage.py @@ -29,32 +29,43 @@ class SendMessage: message: str message_type: str - recipient: str + recipient: list[str] def execute(self, config, task_data): """Execute.""" - url = f'{current_app.config["WAKU_BASE_URL"]}' - headers = {"Accept": "application/json", "Content-type": "application/json"} - request_body = { - "jsonrpc": "2.0", - "method": self.message_type, - "params": [{"id": self.recipient, "message": self.message}], - "id": 1, - } + responses = [] + all_calls_returned_200 = True + for rec in self.recipient: + url = f'{current_app.config["WAKU_BASE_URL"]}' + headers = {"Accept": "application/json", "Content-type": "application/json"} + request_body = { + "jsonrpc": "2.0", + "method": self.message_type, + "params": [{"id": rec, "message": self.message}], + "id": 1, + } - status_code = 0 - try: - raw_response = requests.post(url, json.dumps(request_body), headers=headers) - status_code = raw_response.status_code - parsed_response = json.loads(raw_response.text) - response = json.dumps(parsed_response) - except Exception as ex: - response = json.dumps({"error": str(ex)}) - status_code = 500 + status_code = 0 + try: + raw_response = requests.post(url, json.dumps(request_body), headers=headers) + status_code = raw_response.status_code + if status_code != 200: + all_calls_returned_200 = False + parsed_response = json.loads(raw_response.text) + response = parsed_response + except Exception as ex: + response = {"error": str(ex)} + status_code = 500 + all_calls_returned_200 = False - return { - "response": response, - "node_returned_200": True, - "status": status_code, + responses.append({ + "response": response, + "node_returned_200": True, + "status": status_code, + }) + return ({ + "response": json.dumps(responses), + "node_returned_200": all_calls_returned_200, + "status": 200, "mimetype": "application/json", - } + })