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 18, 2024
1 parent 2787332 commit e0998f0
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 = self._compose_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 send_group_data(
self, group_key: str, feeds_and_data: list, metadata: Optional[dict] = None
Expand Down

0 comments on commit e0998f0

Please sign in to comment.