Skip to content

Commit 5f58c50

Browse files
authored
fix: rewrite get_rest_pages so it knows the API provides fully formed… (snyk-labs#193)
* fix: rewrite get_rest_pages so it knows the API provides fully formed next urls * chore: black * fix: fix version of Poetry
1 parent 92b8973 commit 5f58c50

File tree

4 files changed

+18
-31
lines changed

4 files changed

+18
-31
lines changed

.github/workflows/release.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ jobs:
2020

2121
- name: Install poetry
2222
uses: snok/install-poetry@v1
23+
with:
24+
version: 1.5.1
2325

2426
- name: Install Dependencies
2527
run: poetry install -v

.github/workflows/test.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ jobs:
2121
virtualenvs-create: true
2222
virtualenvs-in-project: true
2323
virtualenvs-path: .venv
24-
24+
version: 1.5.1
25+
2526
- name: Load cached venv
2627
id: cached-poetry-dependencies
2728
uses: actions/cache@v3

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "pysnyk"
3-
version = "0.9.11"
3+
version = "0.9.12"
44
description = "A Python client for the Snyk API"
55
authors = [
66
"Gareth Rushgrove <[email protected]>",

snyk/client.py

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def get(
152152
params = {}
153153

154154
# we use the presence of version to determine if we are REST or not
155-
if "version" not in params.keys() and self.version:
155+
if "version" not in params.keys() and self.version and not exclude_version:
156156
params["version"] = version or self.version
157157

158158
# Python Bools are True/False, JS Bools are true/false
@@ -212,40 +212,24 @@ def get_rest_pages(self, path: str, params: dict = {}) -> List:
212212
This collects the "data" list from the first reponse and then appends the
213213
any further "data" lists if a next link is found in the links field.
214214
"""
215+
first_page_response = self.get(path, params)
216+
page_data = first_page_response.json()
217+
return_data = page_data["data"]
215218

216-
# this is a raw primative but a higher level module might want something that does an
217-
# arbitrary path + origin=foo + limit=100 url construction instead before being sent here
218-
219-
limit = params["limit"]
220-
221-
data = list()
222-
223-
page = self.get(path, params).json()
224-
225-
data.extend(page["data"])
226-
227-
while "next" in page["links"].keys():
219+
while page_data.get("links", {}).get("next"):
228220
logger.debug(
229-
f"GET_REST_PAGES: Another link exists: {page['links']['next']}"
221+
f"GET_REST_PAGES: Another link exists: {page_data['links']['next']}"
230222
)
223+
next_url = page_data.get("links", {}).get("next")
231224

232-
next_url = urllib.parse.urlsplit(page["links"]["next"])
233-
query = urllib.parse.parse_qs(next_url.query)
234-
235-
for k, v in query.items():
236-
params[k] = v
237-
238-
params["limit"] = limit
239-
240-
page = self.get(next_url.path, params).json()
241-
242-
data.extend(page["data"])
243-
225+
# The next url comes back fully formed (i.e. with all the params already set, so no need to do it here)
226+
next_page_response = self.get(next_url, {}, exclude_version=True)
227+
page_data = next_page_response.json()
228+
return_data.extend(page_data["data"])
244229
logger.debug(
245-
f"GET_REST_PAGES: Added another {len(page['data'])} items to the response"
230+
f"GET_REST_PAGES: Added another {len(page_data['data'])} items to the response"
246231
)
247-
248-
return data
232+
return return_data
249233

250234
# alias for backwards compatibility where V3 was the old name
251235
get_v3_pages = get_rest_pages

0 commit comments

Comments
 (0)