From d030d22d589662ac9eaa01cd9a165b1a91d8c76f Mon Sep 17 00:00:00 2001 From: Rodrigo Argumedo Date: Fri, 30 Apr 2021 08:52:34 -0500 Subject: [PATCH 1/3] Create send_batch_data method Additionally, Define the data variable as an empty array. --- adafruit_io/adafruit_io.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/adafruit_io/adafruit_io.py b/adafruit_io/adafruit_io.py index 5443727..2d72023 100755 --- a/adafruit_io/adafruit_io.py +++ b/adafruit_io/adafruit_io.py @@ -565,6 +565,26 @@ def send_data(self, feed_key, data, metadata=None, precision=None): payload = self._create_data(data, metadata) self._post(path, payload) + def send_batch_data(self, feed_key, data, metadata=None, precision=None): + """ + Sends a batch array of data to a specified Adafruit IO feed + :param str feed_key: Adafruit IO feed key + :param str data: Data to send + :param dict metadata: Optional metadata associated with the data + :param int precision: Optional amount of precision points to send with floating point data + """ + data = [] + path = self._compose_path("feeds/{0}/data/batch".format(feed_key)) + if precision: + try: + data = round(data, precision) + except NotImplementedError as err: + raise NotImplementedError( + "Precision requires a floating point value" + ) from err + payload = self._create_data(data, metadata) + self._post(path, payload) + def receive_all_data(self, feed_key): """ Get all data values from a specified Adafruit IO feed. Data is From 340d57336cbd0956a60761107fd5bc18c3517873 Mon Sep 17 00:00:00 2001 From: Rodrigo Argumedo Date: Mon, 3 May 2021 20:30:26 -0500 Subject: [PATCH 2/3] Strip unnecessary parameters and reformat code --- adafruit_io/adafruit_io.py | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/adafruit_io/adafruit_io.py b/adafruit_io/adafruit_io.py index 2d72023..4410875 100755 --- a/adafruit_io/adafruit_io.py +++ b/adafruit_io/adafruit_io.py @@ -565,25 +565,16 @@ def send_data(self, feed_key, data, metadata=None, precision=None): payload = self._create_data(data, metadata) self._post(path, payload) - def send_batch_data(self, feed_key, data, metadata=None, precision=None): + def send_batch_data(self, feed, data_list): """ Sends a batch array of data to a specified Adafruit IO feed :param str feed_key: Adafruit IO feed key - :param str data: Data to send - :param dict metadata: Optional metadata associated with the data - :param int precision: Optional amount of precision points to send with floating point data + :param list Data: Data list to send """ - data = [] - path = self._compose_path("feeds/{0}/data/batch".format(feed_key)) - if precision: - try: - data = round(data, precision) - except NotImplementedError as err: - raise NotImplementedError( - "Precision requires a floating point value" - ) from err - payload = self._create_data(data, metadata) - self._post(path, payload) + validate_feed_key(feed) + path = "feeds/{0}/data/batch".format(feed) + data_dict = type(data_list)((data._asdict() for data in data_list)) + self._post(path, {"data": data_dict}) def receive_all_data(self, feed_key): """ From 363a3fd05670dc93d9324dccdd84e031ece556f6 Mon Sep 17 00:00:00 2001 From: Rodrigo Argumedo Date: Wed, 5 May 2021 19:46:28 -0500 Subject: [PATCH 3/3] Match feed_key parameters and variables in send_batch_data --- adafruit_io/adafruit_io.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/adafruit_io/adafruit_io.py b/adafruit_io/adafruit_io.py index 4410875..f8eb476 100755 --- a/adafruit_io/adafruit_io.py +++ b/adafruit_io/adafruit_io.py @@ -565,14 +565,14 @@ def send_data(self, feed_key, data, metadata=None, precision=None): payload = self._create_data(data, metadata) self._post(path, payload) - def send_batch_data(self, feed, data_list): + def send_batch_data(self, feed_key, data_list): """ Sends a batch array of data to a specified Adafruit IO feed :param str feed_key: Adafruit IO feed key :param list Data: Data list to send """ - validate_feed_key(feed) - path = "feeds/{0}/data/batch".format(feed) + validate_feed_key(feed_key) + path = "feeds/{0}/data/batch".format(feed_key) data_dict = type(data_list)((data._asdict() for data in data_list)) self._post(path, {"data": data_dict})