refactor(src/window/desktop): replace urllib with httpx in http_get_json
- Replace urllib.request with httpx.get to simplify HTTP handling and improve error resilience - Update docstring to reflect httpx usage- Remove unused json and urllib.request imports - Streamline header and timeout configuration
This commit is contained in:
1 file changed
+7
-8
@@ -1,8 +1,7 @@
|
|||||||
import calendar
|
import calendar
|
||||||
import datetime
|
import datetime
|
||||||
import json
|
|
||||||
import urllib.parse
|
import urllib.parse
|
||||||
import urllib.request
|
import httpx
|
||||||
from concurrent.futures import ThreadPoolExecutor
|
from concurrent.futures import ThreadPoolExecutor
|
||||||
from typing import Any, Dict, List, Optional, Tuple
|
from typing import Any, Dict, List, Optional, Tuple
|
||||||
|
|
||||||
@@ -41,17 +40,17 @@ _location_cache: Dict[str, Tuple[float, float, float]] = {}
|
|||||||
def http_get_json(
|
def http_get_json(
|
||||||
url: str, timeout: int = 3, headers: Optional[Dict] = None
|
url: str, timeout: int = 3, headers: Optional[Dict] = None
|
||||||
) -> Optional[Dict]:
|
) -> Optional[Dict]:
|
||||||
"""Helper to perform GET requests and return JSON using built-in urllib."""
|
"""Helper to perform GET requests and return JSON using httpx."""
|
||||||
try:
|
try:
|
||||||
# Some APIs require a User-Agent or they will return 403 Forbidden
|
# Some APIs require a User-Agent or they will return 403 Forbidden
|
||||||
default_headers = {"User-Agent": "Modus-Desktop/1.0"}
|
default_headers = {"User-Agent": "Modus-Desktop/1.0"}
|
||||||
if headers:
|
if headers:
|
||||||
default_headers.update(headers)
|
default_headers.update(headers)
|
||||||
|
response = httpx.get(
|
||||||
req = urllib.request.Request(url, headers=default_headers)
|
url, headers=default_headers, timeout=timeout, follow_redirects=True
|
||||||
with urllib.request.urlopen(req, timeout=timeout) as response:
|
)
|
||||||
if response.status == 200:
|
if response.status_code == 200:
|
||||||
return json.loads(response.read().decode("utf-8"))
|
return response.json()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"HTTP Request to {url} failed: {e}")
|
print(f"HTTP Request to {url} failed: {e}")
|
||||||
return None
|
return None
|
||||||
|
|||||||
Reference in new issue
Block a user