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

Fix handling of req_params for pagination #59

Merged
merged 4 commits into from
Nov 24, 2022
Merged
Show file tree
Hide file tree
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
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