Refactor HTTP client usage to utilize truststore for SSL context
This commit is contained in:
@@ -9,6 +9,7 @@ dependencies = [
|
|||||||
"httpx",
|
"httpx",
|
||||||
"platformdirs",
|
"platformdirs",
|
||||||
"readchar",
|
"readchar",
|
||||||
|
"truststore>=0.10.4",
|
||||||
]
|
]
|
||||||
|
|
||||||
[project.scripts]
|
[project.scripts]
|
||||||
@@ -19,4 +20,4 @@ requires = ["hatchling"]
|
|||||||
build-backend = "hatchling.build"
|
build-backend = "hatchling.build"
|
||||||
|
|
||||||
[tool.hatch.build.targets.wheel]
|
[tool.hatch.build.targets.wheel]
|
||||||
packages = ["src/specify_cli"]
|
packages = ["src/specify_cli"]
|
||||||
|
|||||||
@@ -46,6 +46,11 @@ from typer.core import TyperGroup
|
|||||||
|
|
||||||
# For cross-platform keyboard input
|
# For cross-platform keyboard input
|
||||||
import readchar
|
import readchar
|
||||||
|
import ssl
|
||||||
|
import truststore
|
||||||
|
|
||||||
|
ssl_context = truststore.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
|
||||||
|
client = httpx.Client(verify=ssl_context)
|
||||||
|
|
||||||
# Constants
|
# Constants
|
||||||
AI_CHOICES = {
|
AI_CHOICES = {
|
||||||
@@ -397,10 +402,10 @@ def download_template_from_github(ai_assistant: str, download_dir: Path, *, verb
|
|||||||
api_url = f"https://api.github.com/repos/{repo_owner}/{repo_name}/releases/latest"
|
api_url = f"https://api.github.com/repos/{repo_owner}/{repo_name}/releases/latest"
|
||||||
|
|
||||||
try:
|
try:
|
||||||
response = httpx.get(api_url, timeout=30, follow_redirects=True)
|
response = client.get(api_url, timeout=30, follow_redirects=True)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
release_data = response.json()
|
release_data = response.json()
|
||||||
except httpx.RequestError as e:
|
except client.RequestError as e:
|
||||||
if verbose:
|
if verbose:
|
||||||
console.print(f"[red]Error fetching release information:[/red] {e}")
|
console.print(f"[red]Error fetching release information:[/red] {e}")
|
||||||
raise typer.Exit(1)
|
raise typer.Exit(1)
|
||||||
@@ -437,7 +442,7 @@ def download_template_from_github(ai_assistant: str, download_dir: Path, *, verb
|
|||||||
console.print(f"[cyan]Downloading template...[/cyan]")
|
console.print(f"[cyan]Downloading template...[/cyan]")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with httpx.stream("GET", download_url, timeout=30, follow_redirects=True) as response:
|
with client.stream("GET", download_url, timeout=30, follow_redirects=True) as response:
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
total_size = int(response.headers.get('content-length', 0))
|
total_size = int(response.headers.get('content-length', 0))
|
||||||
|
|
||||||
@@ -466,7 +471,7 @@ def download_template_from_github(ai_assistant: str, download_dir: Path, *, verb
|
|||||||
for chunk in response.iter_bytes(chunk_size=8192):
|
for chunk in response.iter_bytes(chunk_size=8192):
|
||||||
f.write(chunk)
|
f.write(chunk)
|
||||||
|
|
||||||
except httpx.RequestError as e:
|
except client.RequestError as e:
|
||||||
if verbose:
|
if verbose:
|
||||||
console.print(f"[red]Error downloading template:[/red] {e}")
|
console.print(f"[red]Error downloading template:[/red] {e}")
|
||||||
if zip_path.exists():
|
if zip_path.exists():
|
||||||
@@ -843,9 +848,9 @@ def check():
|
|||||||
# Check if we have internet connectivity by trying to reach GitHub API
|
# Check if we have internet connectivity by trying to reach GitHub API
|
||||||
console.print("[cyan]Checking internet connectivity...[/cyan]")
|
console.print("[cyan]Checking internet connectivity...[/cyan]")
|
||||||
try:
|
try:
|
||||||
response = httpx.get("https://api.github.com", timeout=5, follow_redirects=True)
|
response = client.get("https://api.github.com", timeout=5, follow_redirects=True)
|
||||||
console.print("[green]✓[/green] Internet connection available")
|
console.print("[green]✓[/green] Internet connection available")
|
||||||
except httpx.RequestError:
|
except client.RequestError:
|
||||||
console.print("[red]✗[/red] No internet connection - required for downloading templates")
|
console.print("[red]✗[/red] No internet connection - required for downloading templates")
|
||||||
console.print("[yellow]Please check your internet connection[/yellow]")
|
console.print("[yellow]Please check your internet connection[/yellow]")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user