Skip to content

Commit eee6119

Browse files
committed
fix: address review — fix stale docstrings, restore Accept header, add extra_headers to open_url
- Fix _open_url() docstrings in extensions.py and presets.py that incorrectly claimed redirect stripping behavior - Add extra_headers parameter to open_url() so callers can pass additional headers (e.g. Accept) that persist across retries - Restore Accept: application/vnd.github+json header in _fetch_latest_release_tag() via extra_headers
1 parent 9c292d9 commit eee6119

4 files changed

Lines changed: 17 additions & 7 deletions

File tree

src/specify_cli/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1724,7 +1724,11 @@ def _fetch_latest_release_tag() -> tuple[str | None, str | None]:
17241724
from .authentication.http import open_url
17251725

17261726
try:
1727-
with open_url(GITHUB_API_LATEST, timeout=5) as resp:
1727+
with open_url(
1728+
GITHUB_API_LATEST,
1729+
timeout=5,
1730+
extra_headers={"Accept": "application/vnd.github+json"},
1731+
) as resp:
17281732
payload = json.loads(resp.read().decode("utf-8"))
17291733
tag = payload.get("tag_name")
17301734
if not isinstance(tag, str) or not tag:

src/specify_cli/authentication/http.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,25 @@ def build_request(url: str, extra_headers: dict[str, str] | None = None) -> urll
3030
return urllib.request.Request(url, headers=headers)
3131

3232

33-
def open_url(url: str, timeout: int = 10):
33+
def open_url(url: str, timeout: int = 10, extra_headers: dict[str, str] | None = None):
3434
"""Open *url*, trying each configured provider's auth on failure.
3535
3636
1. Try each configured provider's auth headers in registry order.
3737
2. If the request gets a 401 or 403, move to the next provider.
3838
3. If all providers fail (or none are configured), try unauthenticated.
3939
4. Any non-auth error is raised immediately.
40+
41+
*extra_headers* (e.g. ``Accept``) are merged into every attempt.
4042
"""
4143
providers = configured_providers()
4244

45+
def _make_req(headers: dict[str, str]) -> urllib.request.Request:
46+
if extra_headers:
47+
headers = {**headers, **extra_headers}
48+
return urllib.request.Request(url, headers=headers)
49+
4350
for provider in providers:
44-
headers = provider.auth_headers()
45-
req = urllib.request.Request(url, headers=headers)
51+
req = _make_req(provider.auth_headers())
4652
try:
4753
return urllib.request.urlopen(req, timeout=timeout) # noqa: S310
4854
except urllib.error.HTTPError as exc:
@@ -53,5 +59,5 @@ def open_url(url: str, timeout: int = 10):
5359
raise
5460

5561
# No configured provider worked (or none existed) — try unauthenticated.
56-
req = urllib.request.Request(url)
62+
req = _make_req({})
5763
return urllib.request.urlopen(req, timeout=timeout) # noqa: S310

src/specify_cli/extensions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1554,7 +1554,7 @@ def _make_request(self, url: str):
15541554
return build_request(url)
15551555

15561556
def _open_url(self, url: str, timeout: int = 10):
1557-
"""Open a URL with provider-based auth, stripping the header on foreign redirects.
1557+
"""Open a URL with provider-based auth, trying each configured provider.
15581558
15591559
Delegates to :func:`specify_cli.authentication.http.open_url`.
15601560
"""

src/specify_cli/presets.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1853,7 +1853,7 @@ def _make_request(self, url: str):
18531853
return build_request(url)
18541854

18551855
def _open_url(self, url: str, timeout: int = 10):
1856-
"""Open a URL with provider-based auth, stripping the header on foreign redirects.
1856+
"""Open a URL with provider-based auth, trying each configured provider.
18571857
18581858
Delegates to :func:`specify_cli.authentication.http.open_url`.
18591859
"""

0 commit comments

Comments
 (0)