From 9fe13851c0f0fc55592acceedfcf6eca2b99c299 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Thu, 7 Dec 2023 08:54:04 +0100 Subject: [PATCH] Add: Allow to set the number of results to request per page for NVD API Allow a user of the NVD API to specify the number of results within a response. This allows to adjust the size of each "page". --- pontos/nvd/cpe/api.py | 10 ++++++---- pontos/nvd/cve/api.py | 10 ++++++---- pontos/nvd/cve_changes/api.py | 10 ++++++---- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/pontos/nvd/cpe/api.py b/pontos/nvd/cpe/api.py index 7ee66eb4..347914f4 100644 --- a/pontos/nvd/cpe/api.py +++ b/pontos/nvd/cpe/api.py @@ -141,6 +141,7 @@ def cpes( match_criteria_id: Optional[str] = None, request_results: Optional[int] = None, start_index: int = 0, + results_per_page: Optional[int] = None, ) -> NVDResults[CPE]: """ Get all CPEs for the provided arguments @@ -162,6 +163,8 @@ def cpes( to download all available CPEs. start_index: Index of the first CPE to be returned. Useful only for paginated requests that should not start at the first page. + results_per_page: Number of results in a single requests. Mostly + useful for paginated requests. Returns: A NVDResponse for CPEs @@ -205,10 +208,9 @@ def cpes( if match_criteria_id: params["matchCriteriaId"] = match_criteria_id - results_per_page = ( - request_results - if request_results and request_results < MAX_CPES_PER_PAGE - else MAX_CPES_PER_PAGE + results_per_page = min( + results_per_page or MAX_CPES_PER_PAGE, + request_results or MAX_CPES_PER_PAGE, ) return NVDResults( diff --git a/pontos/nvd/cve/api.py b/pontos/nvd/cve/api.py index af58cd9e..6dcf5e49 100644 --- a/pontos/nvd/cve/api.py +++ b/pontos/nvd/cve/api.py @@ -122,6 +122,7 @@ def cves( has_oval: Optional[bool] = None, request_results: Optional[int] = None, start_index: int = 0, + results_per_page: Optional[int] = None, ) -> NVDResults[CVE]: """ Get all CVEs for the provided arguments @@ -174,6 +175,8 @@ def cves( to download all available CVEs. start_index: Index of the first CVE to be returned. Useful only for paginated requests that should not start at the first page. + results_per_page: Number of results in a single requests. Mostly + useful for paginated requests. Returns: A NVDResponse for CVEs @@ -252,10 +255,9 @@ def cves( if has_oval: params["hasOval"] = "" - results_per_page = ( - request_results - if request_results and request_results < MAX_CVES_PER_PAGE - else MAX_CVES_PER_PAGE + results_per_page = min( + results_per_page or MAX_CVES_PER_PAGE, + request_results or MAX_CVES_PER_PAGE, ) return NVDResults( self, diff --git a/pontos/nvd/cve_changes/api.py b/pontos/nvd/cve_changes/api.py index 17aec63a..faa8456b 100644 --- a/pontos/nvd/cve_changes/api.py +++ b/pontos/nvd/cve_changes/api.py @@ -86,6 +86,7 @@ def changes( event_name: Optional[Union[EventName, str]] = None, request_results: Optional[int] = None, start_index: int = 0, + results_per_page: Optional[int] = None, ) -> NVDResults[CVEChange]: """ Get all CVEs for the provided arguments @@ -102,6 +103,8 @@ def changes( start_index: Index of the first CVE change to be returned. Useful only for paginated requests that should not start at the first page. + results_per_page: Number of results in a single requests. Mostly + useful for paginated requests. Returns: A NVDResponse for CVE changes @@ -146,10 +149,9 @@ def changes( if event_name: params["eventName"] = event_name - results_per_page = ( - request_results - if request_results and request_results < MAX_CVE_CHANGES_PER_PAGE - else MAX_CVE_CHANGES_PER_PAGE + results_per_page = min( + results_per_page or MAX_CVE_CHANGES_PER_PAGE, + request_results or MAX_CVE_CHANGES_PER_PAGE, ) return NVDResults( self,