diff --git a/adafruit_io/adafruit_io.py b/adafruit_io/adafruit_io.py index dfafc14..cdb6d56 100755 --- a/adafruit_io/adafruit_io.py +++ b/adafruit_io/adafruit_io.py @@ -54,6 +54,20 @@ def validate_feed_key(feed_key: str): ) +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: """ Client for interacting with Adafruit IO MQTT API. @@ -633,6 +647,18 @@ def receive_all_data(self, feed_key: str): 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): """ Return the most recent value for the specified feed.