Spaces:
Running
Running
File size: 8,947 Bytes
79899c0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
"""
HTTP utility functions for making synchronous and asynchronous HTTP requests.
This module provides a unified interface for HTTP operations using httpx,
with proper error handling, timeout configuration, and retry logic.
"""
import asyncio
import os
import time
import uuid
from typing import Any, Dict, Optional
import httpx
from utils.bio_logger import bio_logger as logger
class HTTPError(Exception):
"""Custom exception for HTTP-related errors."""
def __init__(self, status_code: int, message: str, url: str):
self.status_code = status_code
self.message = message
self.url = url
super().__init__(f"HTTP {status_code}: {message} for {url}")
def _create_timeout(timeout: float = 10.0) -> httpx.Timeout:
"""Create a timeout configuration for HTTP requests."""
return httpx.Timeout(timeout, connect=5.0)
def _handle_response(response: httpx.Response, url: str) -> Any:
"""Handle HTTP response and raise appropriate exceptions."""
if response.status_code == 200:
return response.json()
logger.error(f"HTTP request failed: {response.status_code} for {url}")
raise HTTPError(
status_code=response.status_code,
message=f"Request failed with status {response.status_code}",
url=url,
)
async def async_http_get(
url: str,
params: Optional[Dict[str, Any]] = None,
timeout: float = 10.0,
headers: Optional[Dict[str, str]] = None,
) -> Any:
"""
Make an asynchronous HTTP GET request.
Args:
url: The URL to make the request to
params: Query parameters to include in the request
timeout: Request timeout in seconds
headers: Optional headers to include in the request
Returns:
The JSON response from the server
Raises:
HTTPError: If the request fails
httpx.RequestError: If there's a network error
"""
timeout_config = _create_timeout(timeout)
start_time = time.time()
async with httpx.AsyncClient(timeout=timeout_config) as client:
response = await client.get(url=url, params=params, headers=headers)
duration = time.time() - start_time
# Log the API call
logger.log_api_call("GET", url, response.status_code, duration)
return _handle_response(response, url)
def http_get(
url: str,
params: Optional[Dict[str, Any]] = None,
timeout: float = 10.0,
headers: Optional[Dict[str, str]] = None,
) -> Any:
"""
Make a synchronous HTTP GET request.
Args:
url: The URL to make the request to
params: Query parameters to include in the request
timeout: Request timeout in seconds
headers: Optional headers to include in the request
Returns:
The JSON response from the server
Raises:
HTTPError: If the request fails
httpx.RequestError: If there's a network error
"""
timeout_config = _create_timeout(timeout)
start_time = time.time()
with httpx.Client(timeout=timeout_config) as client:
response = client.get(url=url, params=params, headers=headers)
duration = time.time() - start_time
# Log the API call
logger.log_api_call("GET", url, response.status_code, duration)
return _handle_response(response, url)
def http_post(
url: str, data: Any, headers: Optional[Dict[str, Any]] = None, timeout: float = 10.0
) -> Any:
"""
Make a synchronous HTTP POST request.
Args:
url: The URL to make the request to
data: The data to send in the request body
headers: Optional headers to include in the request
timeout: Request timeout in seconds
Returns:
The JSON response from the server
Raises:
HTTPError: If the request fails
httpx.RequestError: If there's a network error
"""
timeout_config = _create_timeout(timeout)
start_time = time.time()
with httpx.Client(timeout=timeout_config) as client:
response = client.post(url=url, json=data, headers=headers)
duration = time.time() - start_time
# Log the API call
logger.log_api_call("POST", url, response.status_code, duration)
return _handle_response(response, url)
async def async_http_post(
url: str,
data: Any,
headers: Optional[Dict[str, Any]] = None,
timeout: float = 10.0,
max_retries: int = 3,
retry_delay: float = 0.5,
) -> Any:
"""
Make an asynchronous HTTP POST request with retry logic.
Args:
url: The URL to make the request to
data: The data to send in the request body
headers: Optional headers to include in the request
timeout: Request timeout in seconds
max_retries: Maximum number of retry attempts
retry_delay: Delay between retries in seconds
Returns:
The JSON response from the server
Raises:
HTTPError: If the request fails after all retries
httpx.RequestError: If there's a network error
"""
timeout_config = _create_timeout(timeout)
async with httpx.AsyncClient(timeout=timeout_config) as client:
for attempt in range(1, max_retries + 1):
try:
start_time = time.time()
response = await client.post(url=url, json=data, headers=headers)
duration = time.time() - start_time
# Log the API call
logger.log_api_call("POST", url, response.status_code, duration)
if response.status_code == 200:
return response.json()
else:
logger.error(
f"HTTP POST failed (attempt {attempt}/{max_retries}): "
f"{response.status_code} for {url}"
)
if attempt < max_retries:
await asyncio.sleep(retry_delay)
else:
raise HTTPError(
status_code=response.status_code,
message=f"Request failed after {max_retries} attempts",
url=url,
)
except httpx.RequestError as e:
logger.error(f"Network error on attempt {attempt}: {e}")
if attempt < max_retries:
await asyncio.sleep(retry_delay)
else:
raise HTTPError(
status_code=0,
message=f"Network error after {max_retries} attempts: {str(e)}",
url=url,
) from e
raise HTTPError(
status_code=0,
message=f"Failed to fetch data from {url} after {max_retries} attempts",
url=url,
)
def download_file(
file_url: str, directory_path: str, timeout: int = 60, verify_ssl: bool = True
) -> Optional[str]:
"""
Download a file from a URL to a local directory.
Args:
file_url: The URL of the file to download
directory_path: The directory to save the file in
timeout: Request timeout in seconds
verify_ssl: Whether to verify SSL certificates
Returns:
The path to the downloaded file, or None if download failed
"""
# Extract file extension from URL
file_extension = file_url.split(".")[-1].split("?")[0] # Remove query params
random_filename = f"{uuid.uuid4()}.{file_extension}"
# Create directory if it doesn't exist
os.makedirs(directory_path, exist_ok=True)
file_path = os.path.join(directory_path, random_filename)
try:
with httpx.Client(timeout=timeout, verify=verify_ssl) as client:
with client.stream("GET", file_url) as response:
if response.status_code == 200:
with open(file_path, "wb") as file:
for chunk in response.iter_bytes(chunk_size=8192):
file.write(chunk)
logger.info(f"Successfully downloaded file to {file_path}")
return file_path
else:
logger.error(
f"Download failed with status code: {response.status_code}"
)
return None
except httpx.TimeoutException:
logger.error("Download request timed out")
return None
except httpx.RequestError as e:
logger.error(f"Download request failed: {e}")
return None
except Exception as e:
logger.error(f"Unexpected error during download: {e}")
return None
# Backward compatibility functions
async def async_http_post_legacy(url: str, params: dict) -> Any:
"""
Legacy async HTTP POST function for backward compatibility.
This function maintains the old interface but uses the new implementation.
"""
return await async_http_post(url=url, data=params)
|