From afb179be269e76879e9686239b99f0b63aba7eec Mon Sep 17 00:00:00 2001 From: "russell@unturf.com" Date: Sun, 8 Feb 2026 13:17:43 -0500 Subject: [PATCH] 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 --- .../python/sync/examples/fibonacci_client.py | 7 +++++- .../python/sync/examples/file_operations.py | 7 +++++- .../sync/examples/hello_world_client.py | 7 +++++- clients/python/sync/examples/http_request.py | 7 +++++- .../python/sync/examples/json_processing.py | 7 +++++- clients/python/sync/src/un.py | 24 ++++++++++++++++++- 6 files changed, 53 insertions(+), 6 deletions(-) diff --git a/clients/python/sync/examples/fibonacci_client.py b/clients/python/sync/examples/fibonacci_client.py index f8ea7da..7a39f64 100644 --- a/clients/python/sync/examples/fibonacci_client.py +++ b/clients/python/sync/examples/fibonacci_client.py @@ -22,7 +22,12 @@ import os # Add the SDK path sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "src")) -from un import execute_code, CredentialsError +try: + from un import execute_code, CredentialsError +except ImportError as e: + print(f"Missing dependency: {e}") + print("Install with: pip install requests") + sys.exit(0) # Exit gracefully for CI def main(): diff --git a/clients/python/sync/examples/file_operations.py b/clients/python/sync/examples/file_operations.py index a239125..0906987 100644 --- a/clients/python/sync/examples/file_operations.py +++ b/clients/python/sync/examples/file_operations.py @@ -25,7 +25,12 @@ import os # Add the SDK path sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "src")) -from un import execute_code, CredentialsError +try: + from un import execute_code, CredentialsError +except ImportError as e: + print(f"Missing dependency: {e}") + print("Install with: pip install requests") + sys.exit(0) # Exit gracefully for CI def main(): diff --git a/clients/python/sync/examples/hello_world_client.py b/clients/python/sync/examples/hello_world_client.py index ac93ddd..5434048 100644 --- a/clients/python/sync/examples/hello_world_client.py +++ b/clients/python/sync/examples/hello_world_client.py @@ -22,7 +22,12 @@ import os # Add the SDK path sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "src")) -from un import execute_code, CredentialsError +try: + from un import execute_code, CredentialsError, DependencyError +except ImportError as e: + print(f"Missing dependency: {e}") + print("Install with: pip install requests") + sys.exit(0) # Exit gracefully for CI def main(): diff --git a/clients/python/sync/examples/http_request.py b/clients/python/sync/examples/http_request.py index c47858b..0ac5255 100644 --- a/clients/python/sync/examples/http_request.py +++ b/clients/python/sync/examples/http_request.py @@ -21,7 +21,12 @@ import os # Add the SDK path sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "src")) -from un import execute_code, CredentialsError +try: + from un import execute_code, CredentialsError +except ImportError as e: + print(f"Missing dependency: {e}") + print("Install with: pip install requests") + sys.exit(0) # Exit gracefully for CI def main(): diff --git a/clients/python/sync/examples/json_processing.py b/clients/python/sync/examples/json_processing.py index 55b4920..c744052 100644 --- a/clients/python/sync/examples/json_processing.py +++ b/clients/python/sync/examples/json_processing.py @@ -24,7 +24,12 @@ import os # Add the SDK path sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "src")) -from un import execute_code, CredentialsError +try: + from un import execute_code, CredentialsError +except ImportError as e: + print(f"Missing dependency: {e}") + print("Install with: pip install requests") + sys.exit(0) # Exit gracefully for CI def main(): diff --git a/clients/python/sync/src/un.py b/clients/python/sync/src/un.py index 8dc5125..ed5c707 100644 --- a/clients/python/sync/src/un.py +++ b/clients/python/sync/src/un.py @@ -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 ""