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: rewrite get_rest_pages so it knows the API provides fully formed… #193

Merged
merged 3 commits into from
Aug 30, 2023
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 .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ jobs:

- name: Install poetry
uses: snok/install-poetry@v1
with:
version: 1.5.1

- name: Install Dependencies
run: poetry install -v
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ jobs:
virtualenvs-create: true
virtualenvs-in-project: true
virtualenvs-path: .venv

version: 1.5.1

- name: Load cached venv
id: cached-poetry-dependencies
uses: actions/cache@v3
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pysnyk"
version = "0.9.11"
version = "0.9.12"
description = "A Python client for the Snyk API"
authors = [
"Gareth Rushgrove <[email protected]>",
Expand Down
42 changes: 13 additions & 29 deletions snyk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def get(
params = {}

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

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

# this is a raw primative but a higher level module might want something that does an
# arbitrary path + origin=foo + limit=100 url construction instead before being sent here

limit = params["limit"]

data = list()

page = self.get(path, params).json()

data.extend(page["data"])

while "next" in page["links"].keys():
while page_data.get("links", {}).get("next"):
logger.debug(
f"GET_REST_PAGES: Another link exists: {page['links']['next']}"
f"GET_REST_PAGES: Another link exists: {page_data['links']['next']}"
)
next_url = page_data.get("links", {}).get("next")

next_url = urllib.parse.urlsplit(page["links"]["next"])
query = urllib.parse.parse_qs(next_url.query)

for k, v in query.items():
params[k] = v

params["limit"] = limit

page = self.get(next_url.path, params).json()

data.extend(page["data"])

# The next url comes back fully formed (i.e. with all the params already set, so no need to do it here)
next_page_response = self.get(next_url, {}, exclude_version=True)
page_data = next_page_response.json()
return_data.extend(page_data["data"])
logger.debug(
f"GET_REST_PAGES: Added another {len(page['data'])} items to the response"
f"GET_REST_PAGES: Added another {len(page_data['data'])} items to the response"
)

return data
return return_data

# alias for backwards compatibility where V3 was the old name
get_v3_pages = get_rest_pages
Expand Down