Skip to content

Commit 6e2af26

Browse files
committed
Refactor HTTP client usage to utilize truststore for SSL context
1 parent fc8eb04 commit 6e2af26

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ dependencies = [
99
"httpx",
1010
"platformdirs",
1111
"readchar",
12+
"truststore>=0.10.4",
1213
]
1314

1415
[project.scripts]
@@ -19,4 +20,4 @@ requires = ["hatchling"]
1920
build-backend = "hatchling.build"
2021

2122
[tool.hatch.build.targets.wheel]
22-
packages = ["src/specify_cli"]
23+
packages = ["src/specify_cli"]

src/specify_cli/__init__.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@
4646

4747
# For cross-platform keyboard input
4848
import readchar
49+
import ssl
50+
import truststore
51+
52+
ssl_context = truststore.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
53+
client = httpx.Client(verify=ssl_context)
4954

5055
# Constants
5156
AI_CHOICES = {
@@ -397,10 +402,10 @@ def download_template_from_github(ai_assistant: str, download_dir: Path, *, verb
397402
api_url = f"https://api.github.com/repos/{repo_owner}/{repo_name}/releases/latest"
398403

399404
try:
400-
response = httpx.get(api_url, timeout=30, follow_redirects=True)
405+
response = client.get(api_url, timeout=30, follow_redirects=True)
401406
response.raise_for_status()
402407
release_data = response.json()
403-
except httpx.RequestError as e:
408+
except client.RequestError as e:
404409
if verbose:
405410
console.print(f"[red]Error fetching release information:[/red] {e}")
406411
raise typer.Exit(1)
@@ -437,7 +442,7 @@ def download_template_from_github(ai_assistant: str, download_dir: Path, *, verb
437442
console.print(f"[cyan]Downloading template...[/cyan]")
438443

439444
try:
440-
with httpx.stream("GET", download_url, timeout=30, follow_redirects=True) as response:
445+
with client.stream("GET", download_url, timeout=30, follow_redirects=True) as response:
441446
response.raise_for_status()
442447
total_size = int(response.headers.get('content-length', 0))
443448

@@ -466,7 +471,7 @@ def download_template_from_github(ai_assistant: str, download_dir: Path, *, verb
466471
for chunk in response.iter_bytes(chunk_size=8192):
467472
f.write(chunk)
468473

469-
except httpx.RequestError as e:
474+
except client.RequestError as e:
470475
if verbose:
471476
console.print(f"[red]Error downloading template:[/red] {e}")
472477
if zip_path.exists():
@@ -843,9 +848,9 @@ def check():
843848
# Check if we have internet connectivity by trying to reach GitHub API
844849
console.print("[cyan]Checking internet connectivity...[/cyan]")
845850
try:
846-
response = httpx.get("https://api.github.com", timeout=5, follow_redirects=True)
851+
response = client.get("https://api.github.com", timeout=5, follow_redirects=True)
847852
console.print("[green]✓[/green] Internet connection available")
848-
except httpx.RequestError:
853+
except client.RequestError:
849854
console.print("[red]✗[/red] No internet connection - required for downloading templates")
850855
console.print("[yellow]Please check your internet connection[/yellow]")
851856

0 commit comments

Comments
 (0)