Skip to content

Commit f8c61e3

Browse files
ecanlarAtenea Agent
andcommitted
fix(adk_issue_monitoring_agent): defer GITHUB_TOKEN validation to first HTTP call
Move the ValueError from module import time to the lazy _get_session() factory so that importing the package (e.g. pytest collection) does not fail in environments where GITHUB_TOKEN is not set. Co-authored-by: Atenea Agent <srv_atenea_gitlab@ofidona.net>
1 parent 8bfe2d0 commit f8c61e3

2 files changed

Lines changed: 19 additions & 10 deletions

File tree

contributing/samples/adk_team/adk_issue_monitoring_agent/settings.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323

2424
GITHUB_BASE_URL = "https://api.github.com"
2525
GITHUB_TOKEN = os.getenv("GITHUB_TOKEN")
26-
if not GITHUB_TOKEN:
27-
raise ValueError("GITHUB_TOKEN environment variable not set")
2826

2927
OWNER = os.getenv("OWNER", "google")
3028
REPO = os.getenv("REPO", "adk-python")

contributing/samples/adk_team/adk_issue_monitoring_agent/utils.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,24 +48,35 @@ def _increment_api_call_count() -> None:
4848
allowed_methods=["GET", "DELETE"],
4949
)
5050
adapter = HTTPAdapter(max_retries=retry_strategy)
51-
_session = requests.Session()
52-
_session.mount("https://", adapter)
53-
_session.headers.update({
54-
"Authorization": f"token {GITHUB_TOKEN}",
55-
"Accept": "application/vnd.github.v3+json",
56-
})
51+
_session = None
52+
53+
54+
def _get_session() -> requests.Session:
55+
global _session
56+
if _session is not None:
57+
return _session
58+
if not GITHUB_TOKEN:
59+
raise ValueError("GITHUB_TOKEN environment variable not set")
60+
session = requests.Session()
61+
session.mount("https://", adapter)
62+
session.headers.update({
63+
"Authorization": f"token {GITHUB_TOKEN}",
64+
"Accept": "application/vnd.github.v3+json",
65+
})
66+
_session = session
67+
return _session
5768

5869

5970
def get_request(url: str, params: dict[str, Any] | None = None) -> Any:
6071
_increment_api_call_count()
61-
response = _session.get(url, params=params or {}, timeout=60)
72+
response = _get_session().get(url, params=params or {}, timeout=60)
6273
response.raise_for_status()
6374
return response.json()
6475

6576

6677
def post_request(url: str, payload: Any) -> Any:
6778
_increment_api_call_count()
68-
response = _session.post(url, json=payload, timeout=60)
79+
response = _get_session().post(url, json=payload, timeout=60)
6980
response.raise_for_status()
7081
return response.json()
7182

0 commit comments

Comments
 (0)