From ad11e538ac0183fa5e41b8be7ac72fa60438828e Mon Sep 17 00:00:00 2001 From: IanYoung-BO Date: Wed, 27 Dec 2023 11:26:35 -0700 Subject: [PATCH 1/2] Initial fix for #212 --- snyk/client.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/snyk/client.py b/snyk/client.py index e3fa329..3743513 100644 --- a/snyk/client.py +++ b/snyk/client.py @@ -226,13 +226,34 @@ def get_rest_pages(self, path: str, params: dict = {}) -> List: logger.debug( f"GET_REST_PAGES: Another link exists: {page_data['links']['next']}" ) - next_url = page_data.get("links", {}).get("next") + + # Process links to get the next url + if "next" in page_data.json()["links"]: + # If the next url is the same as the current url, break out of the loop + if page_data.json()["links"]["next"] == page_data.json()["links"]["self"]: + break + else: + next_url = page_data.get("links", {}).get("next") + else: + # If there is no next url, break out of the loop + break # 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, exclude_params=True ) page_data = next_page_response.json() + + # Verify that response contains data + if "data" in page_data: + # If the data is empty, break out of the loop + if page_data["data"].len() == 0: + break + # If response does not contain data, break out of the loop + else: + break + + # Append the data from the next page to the return data return_data.extend(page_data["data"]) logger.debug( f"GET_REST_PAGES: Added another {len(page_data['data'])} items to the response" From c871f3ac5a587302d809f0515617e76200d96b5b Mon Sep 17 00:00:00 2001 From: IanYoung-BO Date: Wed, 27 Dec 2023 13:01:00 -0700 Subject: [PATCH 2/2] Fix issues discovered in testing. --- snyk/client.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/snyk/client.py b/snyk/client.py index 3743513..b7087dc 100644 --- a/snyk/client.py +++ b/snyk/client.py @@ -228,9 +228,9 @@ def get_rest_pages(self, path: str, params: dict = {}) -> List: ) # Process links to get the next url - if "next" in page_data.json()["links"]: + if "next" in page_data["links"]: # If the next url is the same as the current url, break out of the loop - if page_data.json()["links"]["next"] == page_data.json()["links"]["self"]: + if "self" in page_data["links"] and page_data["links"]["next"] == page_data["links"]["self"]: break else: next_url = page_data.get("links", {}).get("next") @@ -247,7 +247,7 @@ def get_rest_pages(self, path: str, params: dict = {}) -> List: # Verify that response contains data if "data" in page_data: # If the data is empty, break out of the loop - if page_data["data"].len() == 0: + if len(page_data["data"]) == 0: break # If response does not contain data, break out of the loop else: