fix: make requests import optional in Python sync SDK

- requests import now wrapped in try/except with REQUESTS_AVAILABLE flag
- Added DependencyError exception and _check_requests() helper
- Updated sync examples to catch ImportError and exit gracefully
- CI runner without requests will skip examples instead of failing
This commit is contained in:
russell@unturf.com 2026-02-08 13:17:43 -05:00
parent 933f429162
commit afb179be26
6 changed files with 53 additions and 6 deletions

View file

@ -139,11 +139,17 @@ import hmac
import json
import os
import time
import requests
from datetime import datetime, timedelta
from pathlib import Path
from typing import Optional, Dict, Any, List
try:
import requests
REQUESTS_AVAILABLE = True
except ImportError:
REQUESTS_AVAILABLE = False
requests = None # type: ignore
API_BASE = "https://api.unsandbox.com"
POLL_DELAYS_MS = [300, 450, 700, 900, 650, 1600, 2000]
@ -152,6 +158,20 @@ LANGUAGES_CACHE_TTL = 3600 # 1 hour
class CredentialsError(Exception):
"""Raised when credentials cannot be found or are invalid."""
class DependencyError(Exception):
"""Raised when a required dependency is not installed."""
pass
def _check_requests():
"""Check if requests is available, raise helpful error if not."""
if not REQUESTS_AVAILABLE:
raise DependencyError(
"requests is required for HTTP operations. "
"Install with: pip install requests"
)
pass
@ -266,7 +286,9 @@ def _make_request(
Raises requests.RequestException on network errors.
Raises ValueError if response is not valid JSON.
Raises DependencyError if requests is not installed.
"""
_check_requests()
url = f"{API_BASE}{path}"
timestamp = int(time.time())
body = json.dumps(data) if data else ""