From 29815284e4cea37d800e4fb948b92502bf3f71fb Mon Sep 17 00:00:00 2001 From: kavinaidoo <83715825+kavinaidoo@users.noreply.github.com> Date: Thu, 7 Dec 2023 13:49:00 +0200 Subject: [PATCH 1/2] Added receive_n_data function Allows for an in-between step between receive_data (most recent value only) and receive_all_data (all data values) --- adafruit_io/adafruit_io.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/adafruit_io/adafruit_io.py b/adafruit_io/adafruit_io.py index dfafc14..22d6dba 100755 --- a/adafruit_io/adafruit_io.py +++ b/adafruit_io/adafruit_io.py @@ -53,6 +53,16 @@ def validate_feed_key(feed_key: str): "Feed key must contain English letters, numbers, dash, and a period or a forward slash." ) +def validate_n_values(n_values: int): + """Validates a provided number of values to retrieve data from Adafruit IO. + + Although Adafruit IO will accept values < 1 and > 1000, this avoids two types of issues: + <1 - Coding errors + >1000 - Pagination-related expectation management + + """ + if n_values < 1 or n_values > 1000: # validate 0 < n_values <= 1000 + raise ValueError("Number of values must be greater than zero and less than or equal to 1000") class IO_MQTT: """ @@ -632,6 +642,18 @@ def receive_all_data(self, feed_key: str): validate_feed_key(feed_key) path = self._compose_path("feeds/{0}/data".format(feed_key)) return self._get(path) + + def receive_n_data(self, feed_key: str, n_values: int): + """ + Get n data values from a specified Adafruit IO feed. Data is + returned in reverse order. + + :param str feed_key: Adafruit IO feed key + """ + validate_n_values(n_values) + validate_feed_key(feed_key) + path = self._compose_path("feeds/{0}/data?limit={1}".format(feed_key,n_values)) + return self._get(path) def receive_data(self, feed_key: str): """ From af3c50ef96a4ce58a7bdf31d93762e24f57dd7b9 Mon Sep 17 00:00:00 2001 From: kavinaidoo <83715825+kavinaidoo@users.noreply.github.com> Date: Thu, 7 Dec 2023 15:22:35 +0200 Subject: [PATCH 2/2] formatting changes from black --- adafruit_io/adafruit_io.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/adafruit_io/adafruit_io.py b/adafruit_io/adafruit_io.py index 22d6dba..cdb6d56 100755 --- a/adafruit_io/adafruit_io.py +++ b/adafruit_io/adafruit_io.py @@ -53,6 +53,7 @@ def validate_feed_key(feed_key: str): "Feed key must contain English letters, numbers, dash, and a period or a forward slash." ) + def validate_n_values(n_values: int): """Validates a provided number of values to retrieve data from Adafruit IO. @@ -62,7 +63,10 @@ def validate_n_values(n_values: int): """ if n_values < 1 or n_values > 1000: # validate 0 < n_values <= 1000 - raise ValueError("Number of values must be greater than zero and less than or equal to 1000") + raise ValueError( + "Number of values must be greater than zero and less than or equal to 1000" + ) + class IO_MQTT: """ @@ -642,7 +646,7 @@ def receive_all_data(self, feed_key: str): validate_feed_key(feed_key) path = self._compose_path("feeds/{0}/data".format(feed_key)) return self._get(path) - + def receive_n_data(self, feed_key: str, n_values: int): """ Get n data values from a specified Adafruit IO feed. Data is @@ -652,7 +656,7 @@ def receive_n_data(self, feed_key: str, n_values: int): """ validate_n_values(n_values) validate_feed_key(feed_key) - path = self._compose_path("feeds/{0}/data?limit={1}".format(feed_key,n_values)) + path = self._compose_path("feeds/{0}/data?limit={1}".format(feed_key, n_values)) return self._get(path) def receive_data(self, feed_key: str):