Skip to content

Commit

Permalink
Add list of dicts support to send_batch_data
Browse files Browse the repository at this point in the history
  • Loading branch information
tyeth committed Jul 16, 2024
1 parent e0779b3 commit 6e7df48
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions adafruit_io/adafruit_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -636,12 +636,17 @@ def send_batch_data(self, feed_key: str, data_list: 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
:param list Data: Data list to send (namedtuples or dicts with 'value' key)
"""
validate_feed_key(feed_key)
if not isinstance(data_list, list) or data_list == []:
raise ValueError("Data must be a list of dicts or namedtuples")
if not isinstance(data_list[0], dict): # assume namedtuple
data_list = type(data_list)((data._asdict() for data in data_list))
if not all("value" in data for data in data_list):
raise ValueError("Data list items must at least contain a 'value' 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})
self._post(path, {"data": data_list})

def receive_all_data(self, feed_key: str):
"""
Expand Down

0 comments on commit 6e7df48

Please sign in to comment.