Skip to content

Commit

Permalink
Merge pull request #112 from kavinaidoo/main
Browse files Browse the repository at this point in the history
Added receive_n_data function
  • Loading branch information
FoamyGuy authored Dec 18, 2023
2 parents b84e97b + af3c50e commit 6ba69d7
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions adafruit_io/adafruit_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 6ba69d7

Please sign in to comment.