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

Change: Fix endless loop in NVDApi #1073

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
61 changes: 33 additions & 28 deletions pontos/nvd/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,42 +267,47 @@ def __await__(self) -> Generator[Any, None, "NVDResults"]:

async def _load_next_data(self) -> None:
if (
self._current_request_results is None
or self._downloaded_results < self._current_request_results
self._current_request_results is not None
and self._downloaded_results >= self._current_request_results
):
params = self._params
params["startIndex"] = self._current_index
raise NoMoreResults()

if self._current_results_per_page is not None:
params["resultsPerPage"] = self._current_results_per_page
if (
self._total_results is not None
and self._current_index >= self._total_results
):
raise NoMoreResults()

response = await self._api._get(params=params)
response.raise_for_status()
params = self._params
params["startIndex"] = self._current_index

self._url = response.url
data: JSON = response.json(object_hook=convert_camel_case)
if self._current_results_per_page is not None:
params["resultsPerPage"] = self._current_results_per_page

self._data = data
self._current_results_per_page = int(data["results_per_page"]) # type: ignore
self._total_results = int(data["total_results"]) # type: ignore
self._current_index += self._current_results_per_page
self._downloaded_results += self._current_results_per_page
response = await self._api._get(params=params)
response.raise_for_status()

if not self._current_request_results:
self._current_request_results = self._total_results
self._url = response.url
data: JSON = response.json(object_hook=convert_camel_case)

if (
self._request_results
and self._downloaded_results + self._current_results_per_page
> self._request_results
):
# avoid downloading more results then requested
self._current_results_per_page = (
self._request_results - self._downloaded_results
)
self._data = data
self._current_results_per_page = int(data["results_per_page"]) # type: ignore
self._total_results = int(data["total_results"]) # type: ignore
self._current_index += self._current_results_per_page
self._downloaded_results += self._current_results_per_page

else:
raise NoMoreResults()
if not self._current_request_results:
self._current_request_results = self._total_results

if (
self._request_results
and self._downloaded_results + self._current_results_per_page
> self._request_results
):
# avoid downloading more results then requested
self._current_results_per_page = (
self._request_results - self._downloaded_results
)

async def _get_next_iterator(self) -> Iterator[T]:
await self._load_next_data()
Expand Down
Loading