changing environment variables

This commit is contained in:
simonwt 2026-04-07 22:32:13 +01:00
parent 0072e40c5a
commit 590fae1407
2 changed files with 49 additions and 2 deletions

View file

@ -11,7 +11,17 @@ endpoints = {
"auth": "https://oo0wks9pbi.execute-api.us-east-1.amazonaws.com/alpha/", "auth": "https://oo0wks9pbi.execute-api.us-east-1.amazonaws.com/alpha/",
"content": "https://w1yygdhayc.execute-api.us-east-1.amazonaws.com/alpha/", "content": "https://w1yygdhayc.execute-api.us-east-1.amazonaws.com/alpha/",
"megaphone": "https://opdhjaktnl.execute-api.us-east-1.amazonaws.com/alpha/", "megaphone": "https://opdhjaktnl.execute-api.us-east-1.amazonaws.com/alpha/",
} "moderation": "https://sbb1xrqsf1.execute-api.us-east-1.amazonaws.com/alpha/",
"spider": "https://xuvz1oj1sk.execute-api.us-east-1.amazonaws.com/dev/"
},
"production": {
"audrey": "https://iiouau5d2k.execute-api.us-east-1.amazonaws.com/production/",
"auth": "https://e2we3nktl4.execute-api.us-east-1.amazonaws.com/production/",
"content": "https://32hho6rvg1.execute-api.us-east-1.amazonaws.com/production/",
"megaphone": "https://yfnamdpnc8.execute-api.us-east-1.amazonaws.com/production/",
"moderation": "https://egfpoo3mw3.execute-api.us-east-1.amazonaws.com/production/",
"spider": "https://coi5kvgypc.execute-api.us-east-1.amazonaws.com/production/"
},
} }
class APIClient(BaseModel): class APIClient(BaseModel):
# Internal State (Not passed in __init__) # Internal State (Not passed in __init__)
@ -26,7 +36,14 @@ class APIClient(BaseModel):
_access_token: str = PrivateAttr(default="") _access_token: str = PrivateAttr(default="")
_access_token_timeout: int = PrivateAttr(default=0) _access_token_timeout: int = PrivateAttr(default=0)
def __init__(self, **data):
super().__init__(**data)
self.validate_environment()
def validate_environment(self):
if self.environment not in endpoints:
raise ValueError(f"Environment '{self.environment}' is not valid. Must be one of: {list(endpoints.keys())}")
def make_request(self, method: str, endpoint: str, path: str, data: dict = None, authenticate: bool = True, query_params: dict = None) -> dict: def make_request(self, method: str, endpoint: str, path: str, data: dict = None, authenticate: bool = True, query_params: dict = None) -> dict:
# Make sure the endpoint is defined in the endpoints dictionary # Make sure the endpoint is defined in the endpoints dictionary
@ -112,6 +129,17 @@ class APIClient(BaseModel):
self._access_token = token_data.get("access_token", "") self._access_token = token_data.get("access_token", "")
self._access_token_timeout = token_data.get("access_token_timeout", 0) self._access_token_timeout = token_data.get("access_token_timeout", 0)
def set_environment(self, environment: str) -> None:
"""
Set the environment for the API client.
Args:
environment (str): The environment to set (e.g., "alpha", "production").
"""
self.validate_environment()
self.environment = environment
def is_token_valid(self) -> bool: def is_token_valid(self) -> bool:
""" """
Checks if the current access token is still valid based on the current time and the token's expiration time. Checks if the current access token is still valid based on the current time and the token's expiration time.

View file

@ -46,6 +46,25 @@ class TestAPIClient(unittest.TestCase):
result = self.api_client.run_job(mock_job, "value1") result = self.api_client.run_job(mock_job, "value1")
self.assertEqual(result, "Job executed with value1") self.assertEqual(result, "Job executed with value1")
def test_set_production_env(self):
Prod_API = APIClient(
client_id="prod_client_id",
client_secret="prod_client_secret",
debug=False,
environment="production"
)
self.assertEqual(Prod_API.environment, "production")
def test_set_bad_env(self):
with self.assertRaises(ValueError):
APIClient(
client_id="test_client_id",
client_secret="test_client_secret",
debug=False,
environment="invalid_env"
)
@patch('trustcafeapiwrapper.apiclient.requests.request') @patch('trustcafeapiwrapper.apiclient.requests.request')
def test_make_request(self, mock_request): def test_make_request(self, mock_request):