Skip to content

Commit

Permalink
Change: Fix endless loop in NVDApi (#1073)
Browse files Browse the repository at this point in the history
* Change: Invert if-clause to reduce indentation

* Change: Stop requesting more results than available
  • Loading branch information
n-thumann authored Dec 12, 2024
1 parent 54d814c commit 7631819
Showing 1 changed file with 33 additions and 28 deletions.
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

0 comments on commit 7631819

Please sign in to comment.