Skip to content

Commit

Permalink
fix linting
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Goodman <[email protected]>
  • Loading branch information
wagoodman committed Nov 26, 2024
1 parent 67547bb commit f3930db
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
18 changes: 16 additions & 2 deletions src/vunnel/providers/nvd/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ class NvdAPI:
_max_results_per_page_: int = 2000
max_date_range_days: int = 120

def __init__(self, api_key: str | None = None, logger: logging.Logger | None = None, timeout: int = 30, retries: int = 10) -> None:
def __init__(
self,
api_key: str | None = None,
logger: logging.Logger | None = None,
timeout: int = 30,
retries: int = 10,
) -> None:
self.api_key = api_key
self.timeout = timeout
self.retries = retries
Expand Down Expand Up @@ -155,7 +161,15 @@ def _request(self, url: str, parameters: dict[str, str], headers: dict[str, str]

# NVD rate-limiting is detailed at https://nvd.nist.gov/developers/start-here and currently resets on a 30 second
# rolling window, so setting retry to start trying again after 30 seconds.
response = http.get(url, self.logger, backoff_in_seconds=30, params=payload_str, headers=headers, timeout=self.timeout, retries=self.retries)
response = http.get(
url,
self.logger,
backoff_in_seconds=30,
params=payload_str,
headers=headers,
timeout=self.timeout,
retries=self.retries,
)
response.encoding = "utf-8"

return response
Expand Down
2 changes: 1 addition & 1 deletion src/vunnel/providers/nvd/overrides.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class NVDOverrides:
__file_name__ = "nvd-overrides.tar.gz"
__extract_name__ = "nvd-overrides"

def __init__( # noqa: PLR0913
def __init__( # noqa: PLR0913
self,
enabled: bool,
url: str,
Expand Down
2 changes: 1 addition & 1 deletion src/vunnel/utils/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def get( # noqa: PLR0913
last_exception: Exception | None = None
for attempt in range(retries + 1):
if last_exception:
sleep_interval = backoff_in_seconds * 2**(attempt-1) + random.uniform(0, 1) # noqa: S311
sleep_interval = backoff_in_seconds * 2 ** (attempt - 1) + random.uniform(0, 1) # noqa: S311
logger.warning(f"will retry in {int(sleep_interval)} seconds...")
time.sleep(sleep_interval)

Expand Down
8 changes: 6 additions & 2 deletions tests/unit/utils/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ def test_correct_number_of_retria(self, mock_requests, mock_sleep, mock_logger,
@patch("time.sleep")
@patch("requests.get")
@patch("random.uniform")
def test_succeeds_if_retries_succeed(self, mock_uniform_random, mock_requests, mock_sleep, mock_logger, error_response, success_response):
def test_succeeds_if_retries_succeed(
self, mock_uniform_random, mock_requests, mock_sleep, mock_logger, error_response, success_response
):
mock_uniform_random.side_effect = [0.1]
mock_requests.side_effect = [error_response, success_response]
http.get("http://example.com/some-path", mock_logger, retries=1, backoff_in_seconds=22)
Expand Down Expand Up @@ -97,7 +99,9 @@ def test_it_log_warns_errors(self, mock_requests, mock_sleep, mock_logger, error
logged_warnings = [call.args[0] for call in mock_logger.warning.call_args_list]

assert any("HTTP ERROR" in message for message in logged_warnings), "Expected 'HTTP ERROR' in logged warnings."
assert any("will retry in 33 seconds" in message for message in logged_warnings), "Expected retry message in logged warnings."
assert any(
"will retry in 33 seconds" in message for message in logged_warnings
), "Expected retry message in logged warnings."

@patch("time.sleep")
@patch("requests.get")
Expand Down

0 comments on commit f3930db

Please sign in to comment.