Skip to content

Commit 6ba69d7

Browse files
authored
Merge pull request #112 from kavinaidoo/main
Added receive_n_data function
2 parents b84e97b + af3c50e commit 6ba69d7

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

adafruit_io/adafruit_io.py

+26
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,20 @@ def validate_feed_key(feed_key: str):
5454
)
5555

5656

57+
def validate_n_values(n_values: int):
58+
"""Validates a provided number of values to retrieve data from Adafruit IO.
59+
60+
Although Adafruit IO will accept values < 1 and > 1000, this avoids two types of issues:
61+
<1 - Coding errors
62+
>1000 - Pagination-related expectation management
63+
64+
"""
65+
if n_values < 1 or n_values > 1000: # validate 0 < n_values <= 1000
66+
raise ValueError(
67+
"Number of values must be greater than zero and less than or equal to 1000"
68+
)
69+
70+
5771
class IO_MQTT:
5872
"""
5973
Client for interacting with Adafruit IO MQTT API.
@@ -633,6 +647,18 @@ def receive_all_data(self, feed_key: str):
633647
path = self._compose_path("feeds/{0}/data".format(feed_key))
634648
return self._get(path)
635649

650+
def receive_n_data(self, feed_key: str, n_values: int):
651+
"""
652+
Get n data values from a specified Adafruit IO feed. Data is
653+
returned in reverse order.
654+
655+
:param str feed_key: Adafruit IO feed key
656+
"""
657+
validate_n_values(n_values)
658+
validate_feed_key(feed_key)
659+
path = self._compose_path("feeds/{0}/data?limit={1}".format(feed_key, n_values))
660+
return self._get(path)
661+
636662
def receive_data(self, feed_key: str):
637663
"""
638664
Return the most recent value for the specified feed.

0 commit comments

Comments
 (0)