Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Jul 30, 2024
1 parent a775007 commit c9d6d8d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 22 deletions.
24 changes: 19 additions & 5 deletions tap_github/authenticator.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ class TokenManager:
# - not consume all available calls when we rare using an org or user token.
DEFAULT_RATE_LIMIT_BUFFER = 1000

def __init__(self, token: str, rate_limit_buffer: Optional[int] = None, logger: Optional[Any] = None):
def __init__(
self,
token: str,
rate_limit_buffer: Optional[int] = None,
logger: Optional[Any] = None,
):
"""Init TokenManager info."""
self.token = token
self.logger = logger
Expand Down Expand Up @@ -143,7 +148,9 @@ def prepare_tokens(self) -> List[TokenManager]:
if "auth_token" in self._config:
personal_tokens.add(self._config["auth_token"])
if "additional_auth_tokens" in self._config:
personal_tokens = personal_tokens.union(self._config["additional_auth_tokens"])
personal_tokens = personal_tokens.union(
self._config["additional_auth_tokens"]
)
else:
# Accept multiple tokens using environment variables GITHUB_TOKEN*
env_tokens = {
Expand All @@ -159,7 +166,9 @@ def prepare_tokens(self) -> List[TokenManager]:

token_managers: List[TokenManager] = []
for token in personal_tokens:
token_manager = TokenManager(token, rate_limit_buffer=rate_limit_buffer, logger=self.logger)
token_manager = TokenManager(
token, rate_limit_buffer=rate_limit_buffer, logger=self.logger
)
if token_manager.is_valid_token():
token_managers.append(token_manager)

Expand All @@ -182,7 +191,9 @@ def prepare_tokens(self) -> List[TokenManager]:
app_token = generate_app_access_token(
github_app_id, github_private_key, github_installation_id or None
)
token_manager = TokenManager(app_token, rate_limit_buffer=rate_limit_buffer, logger=self.logger)
token_manager = TokenManager(
app_token, rate_limit_buffer=rate_limit_buffer, logger=self.logger
)
if token_manager.is_valid_token():
token_managers.append(token_manager)

Expand Down Expand Up @@ -212,7 +223,10 @@ def get_next_auth_token(self) -> None:
current_token = self.active_token.token if self.active_token else ""
shuffle(token_managers)
for token_manager in token_managers:
if token_manager.has_calls_remaining() and current_token != token_manager.token:
if (
token_manager.has_calls_remaining()
and current_token != token_manager.token
):
self.active_token = token_manager
self.logger.info(f"Switching to fresh auth token")
return
Expand Down
36 changes: 19 additions & 17 deletions tap_github/tests/test_authenticator.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from datetime import datetime, timedelta
from unittest.mock import MagicMock, patch

import pytest
import requests
from unittest.mock import patch, MagicMock

from tap_github.authenticator import TokenManager


class TestTokenManager():
class TestTokenManager:

def test_default_rate_limits(self):
token_manager = TokenManager("mytoken", rate_limit_buffer=700)
Expand All @@ -24,7 +26,7 @@ def test_update_rate_limit(self):
"X-RateLimit-Limit": "5000",
"X-RateLimit-Remaining": "4999",
"X-RateLimit-Reset": "1372700873",
"X-RateLimit-Used": "1"
"X-RateLimit-Used": "1",
}

token_manager = TokenManager("mytoken")
Expand All @@ -36,7 +38,7 @@ def test_update_rate_limit(self):
assert token_manager.rate_limit_used == 1

def test_is_valid_token_successful(self):
with patch('requests.get') as mock_get:
with patch("requests.get") as mock_get:
mock_response = mock_get.return_value
mock_response.raise_for_status.return_value = None

Expand All @@ -45,17 +47,17 @@ def test_is_valid_token_successful(self):
assert token_manager.is_valid_token()
mock_get.assert_called_once_with(
url="https://api.github.com/rate_limit",
headers={"Authorization": "token validtoken"}
headers={"Authorization": "token validtoken"},
)

def test_is_valid_token_failure(self):
with patch('requests.get') as mock_get:
with patch("requests.get") as mock_get:
# Setup for a failed request
mock_response = mock_get.return_value
mock_response.raise_for_status.side_effect = requests.exceptions.HTTPError()
mock_response.status_code = 401
mock_response.content = b'Unauthorized Access'
mock_response.reason = 'Unauthorized'
mock_response.content = b"Unauthorized Access"
mock_response.reason = "Unauthorized"

token_manager = TokenManager("invalidtoken")
token_manager.logger = MagicMock()
Expand All @@ -73,7 +75,7 @@ def test_has_calls_remaining_succeeds_if_lots_remaining(self):
"X-RateLimit-Limit": "5000",
"X-RateLimit-Remaining": "4999",
"X-RateLimit-Reset": "1372700873",
"X-RateLimit-Used": "1"
"X-RateLimit-Used": "1",
}

token_manager = TokenManager("mytoken")
Expand All @@ -86,27 +88,27 @@ def test_has_calls_remaining_succeeds_if_reset_time_reached(self):
"X-RateLimit-Limit": "5000",
"X-RateLimit-Remaining": "1",
"X-RateLimit-Reset": "1372700873",
"X-RateLimit-Used": "4999"
"X-RateLimit-Used": "4999",
}

token_manager = TokenManager("mytoken", rate_limit_buffer=1000)
token_manager.update_rate_limit(mock_response_headers)

assert token_manager.has_calls_remaining()

def test_has_calls_remaining_fails_if_few_calls_remaining_and_reset_time_not_reached(self):
def test_has_calls_remaining_fails_if_few_calls_remaining_and_reset_time_not_reached(
self,
):
mock_response_headers = {
"X-RateLimit-Limit": "5000",
"X-RateLimit-Remaining": "1",
"X-RateLimit-Reset": str(int((datetime.now() + timedelta(days=100)).timestamp())),
"X-RateLimit-Used": "4999"
"X-RateLimit-Reset": str(
int((datetime.now() + timedelta(days=100)).timestamp())
),
"X-RateLimit-Used": "4999",
}

token_manager = TokenManager("mytoken", rate_limit_buffer=1000)
token_manager.update_rate_limit(mock_response_headers)

assert not token_manager.has_calls_remaining()




0 comments on commit c9d6d8d

Please sign in to comment.