Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added receive_n_data function #112

Merged
merged 2 commits into from
Dec 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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