Skip to content

Commit

Permalink
Update nvd_nist_api.py
Browse files Browse the repository at this point in the history
Debug Information Logging
  • Loading branch information
derIckeBrln authored May 21, 2024
1 parent 9bad5ce commit 7403062
Showing 1 changed file with 48 additions and 13 deletions.
61 changes: 48 additions & 13 deletions CveXplore/core/nvd_nist/nvd_nist_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from json import JSONDecodeError
from urllib.parse import urlencode

from requests.exceptions import RequestException, HTTPError

import aiohttp as aiohttp
import requests
from aiohttp import ContentTypeError
Expand Down Expand Up @@ -81,19 +83,22 @@ def _build_url(self, resource: dict = None, data: int = 1) -> str:
resource = urlencode(resource)
if data == self.datasource.CVE:
if self.filter_rejected:
return f"{self.baseurl}/rest/json/{self.datasource_mapping[data]}/{self.api_path}/?noRejected&{resource}"
url = f"{self.baseurl}/rest/json/{self.datasource_mapping[data]}/{self.api_path}/?noRejected&{resource}"
else:
return f"{self.baseurl}/rest/json/{self.datasource_mapping[data]}/{self.api_path}/?{resource}"
url = f"{self.baseurl}/rest/json/{self.datasource_mapping[data]}/{self.api_path}/?{resource}"
else:
return f"{self.baseurl}/rest/json/{self.datasource_mapping[data]}/{self.api_path}/?{resource}"
url = f"{self.baseurl}/rest/json/{self.datasource_mapping[data]}/{self.api_path}/?{resource}"
else:
if data == self.datasource.CVE:
if self.filter_rejected:
return f"{self.baseurl}/rest/json/{self.datasource_mapping[data]}/{self.api_path}/?noRejected"
url = f"{self.baseurl}/rest/json/{self.datasource_mapping[data]}/{self.api_path}/?noRejected"
else:
return f"{self.baseurl}/rest/json/{self.datasource_mapping[data]}/{self.api_path}/"
url = f"{self.baseurl}/rest/json/{self.datasource_mapping[data]}/{self.api_path}/"
else:
return f"{self.baseurl}/rest/json/{self.datasource_mapping[data]}/{self.api_path}/"
url = f"{self.baseurl}/rest/json/{self.datasource_mapping[data]}/{self.api_path}/"

self.logger.info(f"Built URL: {url}")
return url

def _connect(
self,
Expand All @@ -113,11 +118,27 @@ def _connect(
"proxies": self.proxies,
}

full_url = self._build_url(resource, data=data)
self.logger.info(f"Sending {method} request to URL: {full_url}")
self.logger.info(f"Request headers: {request_api_resource['headers']}")
self.logger.info(f"Request data: {data}")

try:
self.logger.debug(f"Sending request: resource={resource}, data={data}")
r = session.get(
self._build_url(resource, data=data), **request_api_resource
)
if method == "GET":
r = session.get(full_url, **request_api_resource)
elif method == "POST":
r = session.post(full_url, data=data, **request_api_resource)
elif method == "PUT":
r = session.put(full_url, data=data, **request_api_resource)
elif method == "PATCH":
r = session.patch(full_url, data=data, **request_api_resource)
elif method == "DELETE":
r = session.delete(full_url, **request_api_resource)
else:
raise ValueError(f"Unsupported method: {method}")

self.logger.info(f"Received response: {r.status_code}")
self.logger.info(f"Response content: {r.text}")

try:
if isinstance(r, Response):
Expand Down Expand Up @@ -146,8 +167,10 @@ def _connect(
return the_response

except requests.exceptions.ConnectionError as err:
self.logger.error(f"Connection error occurred: {err}")
raise requests.exceptions.ConnectionError(err)
except Exception as err:
self.logger.error(f"An error occurred: {err}")
raise Exception(err)

def __repr__(self):
Expand Down Expand Up @@ -244,17 +267,29 @@ def get_count(
last_mod_end_date=last_mod_end_date,
)

self.logger.debug(f"Getting count for datasource: {datasource}")
self.logger.info(f"Getting count for datasource: {datasource}")

try:
ret_data = self.call(self.methods.GET, resource=resource, data=datasource)

if not isinstance(ret_data, Response):
self.logger.info(f"API response data: {ret_data}")
return ret_data["totalResults"]
else:
self.logger.error(f"Unexpected response type: {ret_data}")
raise ApiDataRetrievalFailed(resource)
except Exception:
raise ApiMaxRetryError
except HTTPError as http_err:
self.logger.error(f"HTTP error occurred: {http_err}")
raise ApiMaxRetryError from http_err
except RequestException as req_err:
self.logger.error(f"Request exception occurred: {req_err}")
raise ApiMaxRetryError from req_err
except ApiDataRetrievalFailed as data_err:
self.logger.error(f"Data retrieval error: {data_err}")
raise
except Exception as err:
self.logger.error(f"An unexpected error occurred: {err}")
raise ApiMaxRetryError from err

def get_all_data(
self,
Expand Down

0 comments on commit 7403062

Please sign in to comment.