Skip to content

Commit 3d267e9

Browse files
committed
Eliminate while True: by querying the feed count if max_results is None (caller is requesting all data).
1 parent e9afd5b commit 3d267e9

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

Adafruit_IO/client.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,14 +251,17 @@ def data(self, feed, data_id=None, max_results=API_PAGE_LIMIT):
251251
:param int max_results: The maximum number of results to return. To
252252
return all data, set to None.
253253
"""
254+
if max_results is None:
255+
res = self._get(f'feeds/{feed}/details')
256+
max_results = res['details']['data']['count']
254257
if data_id:
255258
path = "feeds/{0}/data/{1}".format(feed, data_id)
256259
return Data.from_dict(self._get(path))
257260

258261
params = {'limit': max_results} if max_results else None
259262
data = []
260263
path = "feeds/{0}/data".format(feed)
261-
while True:
264+
while len(data) < max_results:
262265
data.extend(list(map(Data.from_dict, self._get(path,
263266
params=params))))
264267
nlink = self.get_next_link()
@@ -267,8 +270,6 @@ def data(self, feed, data_id=None, max_results=API_PAGE_LIMIT):
267270
# Parse the link for the query parameters
268271
params = parse_qs(urlparse(nlink).query)
269272
if max_results:
270-
if len(data) >= max_results:
271-
break
272273
params['limit'] = max_results - len(data)
273274
return data
274275

0 commit comments

Comments
 (0)