Skip to content

Commit

Permalink
Merge pull request #59 from MagerValp/fixpagination
Browse files Browse the repository at this point in the history
Fix handling of req_params for pagination
  • Loading branch information
MagerValp authored Nov 24, 2022
2 parents 4e1c9fc + d822ce6 commit 45d2c6c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
- Fixes `_get_data` so that it properly preserves all input parameters ([#45](https://github.com/macadmins/simpleMDMpy/issues/45)) - TY [@bryanheinz](https://github.com/bryanheinz)
- Adds help docs to Devices.get_device() - TY [@bryanheinz](https://github.com/bryanheinz)
- Add Scripts and ScriptJobs - TY [@MagerValp](https://github.com/MagerValp)
- Fix pagination - TY [@jcfrt](https://github.com/jcfrt)

### Issues

Expand All @@ -32,6 +33,7 @@
- Closes issue #43
- Closes issue #29
- Closes issue #45
- Closes issue #57

## [v3.0.6]

Expand Down
14 changes: 8 additions & 6 deletions SimpleMDMpy/SimpleMDM.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,27 @@ def _url(self, path): #pylint: disable=no-self-use
def _is_devices_req(self, url):
return url.startswith(self._url("/devices"))

def _get_data(self, url, params={}):
def _get_data(self, url, params=None):
"""GET call to SimpleMDM API"""
has_more = True
list_data = []
# by using the local req_params variable, we can set our own defaults if
# the parameters aren't included with the input params. This is needed
# so that certain other functions, like Logs.get_logs(), can send custom
# starting_after and limit parameters.
req_params = {}
req_params['limit'] = params.get('limit', 100)
req_params['starting_after'] = params.get('starting_after', 0)
if params is None:
req_params = {}
else:
req_params = params.copy()
req_params['limit'] = req_params.get('limit', 100)
while has_more:
# Calls to /devices should be rate limited
if self._is_devices_req(url):
if time.time() - self.last_device_req_timestamp < self.device_req_rate_limit:
time.sleep(time.time() - self.last_device_req_timestamp)
self.last_device_req_timestamp = time.time()
while True:
resp = self.session.get(url, params=params, auth=(self.api_key, ""), proxies=self.proxyDict)
resp = self.session.get(url, params=req_params, auth=(self.api_key, ""), proxies=self.proxyDict)
# A 429 means we've hit the rate limit, so back off and retry
if resp.status_code == 429:
time.sleep(1)
Expand All @@ -86,7 +88,7 @@ def _get_data(self, url, params={}):
list_data.extend(data)
has_more = resp_json.get('has_more', False)
if has_more:
params["starting_after"] = data[-1].get('id')
req_params["starting_after"] = data[-1].get('id')
return list_data

def _get_xml(self, url, params=None):
Expand Down

0 comments on commit 45d2c6c

Please sign in to comment.