Spaces:
Sleeping
Sleeping
File size: 25,981 Bytes
3f9f85b | 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 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 | """
API Testing Module for AI Travel Planner
This module demonstrates:
- Async API testing patterns
- Comprehensive error handling
- Response validation
- Retry logic implementation
- Step-by-step debugging techniques
"""
import asyncio
import json
import time
from contextlib import asynccontextmanager
from datetime import datetime, timedelta
from typing import Any, Dict, List, Optional, Union, Tuple
from dataclasses import dataclass
from enum import Enum
import httpx
from pydantic import BaseModel, Field, ValidationError
from ..utils.logging import get_logger
from ..utils.security import ErrorResponse, ErrorType
class APITestResult(BaseModel):
"""Standardized API test result."""
api_name: str = Field(..., description="Name of the API being tested")
endpoint: str = Field(..., description="API endpoint tested")
success: bool = Field(..., description="Whether the test passed")
response_time_ms: float = Field(..., description="Response time in milliseconds")
status_code: Optional[int] = Field(None, description="HTTP status code")
error_type: Optional[str] = Field(None, description="Type of error if any")
error_message: Optional[str] = Field(None, description="Error message if any")
response_data: Optional[Dict[str, Any]] = Field(None, description="Response data if successful")
timestamp: datetime = Field(default_factory=datetime.now, description="Test timestamp")
retry_count: int = Field(0, description="Number of retries attempted")
class APIErrorType(Enum):
"""Types of API errors for proper handling."""
NETWORK_ERROR = "network_error"
AUTHENTICATION_ERROR = "authentication_error"
AUTHORIZATION_ERROR = "authorization_error"
RATE_LIMIT_ERROR = "rate_limit_error"
TIMEOUT_ERROR = "timeout_error"
VALIDATION_ERROR = "validation_error"
SERVER_ERROR = "server_error"
UNKNOWN_ERROR = "unknown_error"
@dataclass
class APIConfig:
"""Configuration for API testing."""
name: str
base_url: str
api_key: str
timeout: int = 30
max_retries: int = 3
retry_delay: float = 1.0
rate_limit_per_minute: int = 60
class APITester:
"""
Comprehensive API testing class with debugging capabilities.
This class demonstrates:
- Async API testing patterns
- Proper error handling and categorization
- Response validation
- Retry logic with exponential backoff
- Step-by-step debugging techniques
"""
def __init__(self, config: APIConfig):
"""Initialize the API tester."""
self.config = config
self.logger = get_logger(f"api_tester.{config.name}")
self._client: Optional[httpx.AsyncClient] = None
self._request_count = 0
self._rate_limit_tokens = config.rate_limit_per_minute
self._rate_limit_reset = datetime.now() + timedelta(minutes=1)
self.logger.info(f"Initialized API tester for {config.name}")
async def __aenter__(self):
"""Async context manager entry."""
await self._setup_client()
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
"""Async context manager exit with cleanup."""
await self._cleanup_client()
if exc_type:
self.logger.error(f"API tester exited with error: {exc_val}")
else:
self.logger.info(f"API tester completed successfully")
async def _setup_client(self):
"""Setup HTTP client with proper configuration."""
if self._client:
return
self._client = httpx.AsyncClient(
base_url=self.config.base_url,
timeout=httpx.Timeout(self.config.timeout),
headers={
"Authorization": f"Bearer {self.config.api_key}",
"User-Agent": f"WanderlustAI-APITester/1.0",
"Content-Type": "application/json"
}
)
self.logger.info(f"HTTP client setup for {self.config.name}")
async def _cleanup_client(self):
"""Cleanup HTTP client resources."""
if self._client:
await self._client.aclose()
self._client = None
self.logger.info(f"HTTP client cleaned up for {self.config.name}")
async def _check_rate_limit(self):
"""Check and enforce rate limiting."""
now = datetime.now()
# Reset tokens if minute has passed
if now >= self._rate_limit_reset:
self._rate_limit_tokens = self.config.rate_limit_per_minute
self._rate_limit_reset = now + timedelta(minutes=1)
self.logger.debug(f"Rate limit reset for {self.config.name}")
# Check if we have tokens available
if self._rate_limit_tokens <= 0:
wait_time = (self._rate_limit_reset - now).total_seconds()
self.logger.warning(f"Rate limit exceeded for {self.config.name}, waiting {wait_time:.1f}s")
await asyncio.sleep(wait_time)
await self._check_rate_limit()
# Consume a token
self._rate_limit_tokens -= 1
self.logger.debug(f"Rate limit: {self._rate_limit_tokens} tokens remaining for {self.config.name}")
def _categorize_error(self, error: Exception, status_code: Optional[int] = None) -> APIErrorType:
"""
Categorize API errors for proper handling.
This is crucial for implementing appropriate retry strategies.
"""
if isinstance(error, httpx.TimeoutException):
return APIErrorType.TIMEOUT_ERROR
elif isinstance(error, httpx.ConnectError):
return APIErrorType.NETWORK_ERROR
elif isinstance(error, httpx.HTTPStatusError):
if status_code == 401:
return APIErrorType.AUTHENTICATION_ERROR
elif status_code == 403:
return APIErrorType.AUTHORIZATION_ERROR
elif status_code == 429:
return APIErrorType.RATE_LIMIT_ERROR
elif 500 <= status_code < 600:
return APIErrorType.SERVER_ERROR
else:
return APIErrorType.UNKNOWN_ERROR
elif isinstance(error, httpx.RequestError):
return APIErrorType.NETWORK_ERROR
else:
return APIErrorType.UNKNOWN_ERROR
def _should_retry(self, error_type: APIErrorType, retry_count: int) -> bool:
"""
Determine if a request should be retried based on error type.
This implements intelligent retry logic:
- Network errors: Retry (temporary)
- Timeout errors: Retry (temporary)
- Server errors: Retry (temporary)
- Auth errors: Don't retry (permanent)
- Rate limit errors: Retry with longer delay
"""
if retry_count >= self.config.max_retries:
return False
retryable_errors = {
APIErrorType.NETWORK_ERROR,
APIErrorType.TIMEOUT_ERROR,
APIErrorType.SERVER_ERROR,
APIErrorType.RATE_LIMIT_ERROR
}
return error_type in retryable_errors
def _calculate_retry_delay(self, error_type: APIErrorType, retry_count: int) -> float:
"""
Calculate retry delay with exponential backoff.
Different error types get different retry strategies:
- Rate limit errors: Longer delay
- Network errors: Standard exponential backoff
- Server errors: Standard exponential backoff
"""
base_delay = self.config.retry_delay
if error_type == APIErrorType.RATE_LIMIT_ERROR:
# Rate limit errors need longer delays
return base_delay * (2 ** retry_count) * 2
else:
# Standard exponential backoff
return base_delay * (2 ** retry_count)
async def test_api_call(
self,
method: str,
endpoint: str,
data: Optional[Dict[str, Any]] = None,
params: Optional[Dict[str, Any]] = None,
headers: Optional[Dict[str, str]] = None
) -> APITestResult:
"""
Test an API call with comprehensive error handling and retry logic.
This is the main testing function that demonstrates:
- Proper error handling
- Retry logic with exponential backoff
- Response validation
- Performance monitoring
"""
start_time = time.time()
retry_count = 0
# Rate limiting check
await self._check_rate_limit()
# Prepare request
request_headers = {}
if headers:
request_headers.update(headers)
self.logger.info(f"Testing {method} {endpoint} for {self.config.name}")
while retry_count <= self.config.max_retries:
try:
async with self.get_client() as client:
self.logger.debug(f"{self.config.name}: {method} {endpoint} (attempt {retry_count + 1})")
# Make the request
response = await client.request(
method=method,
url=endpoint,
json=data,
params=params,
headers=request_headers
)
# Calculate response time
response_time = (time.time() - start_time) * 1000
# Update request tracking
self._request_count += 1
# Create test result
result = APITestResult(
api_name=self.config.name,
endpoint=endpoint,
success=response.status_code < 400,
response_time_ms=response_time,
status_code=response.status_code,
retry_count=retry_count
)
if result.success:
try:
result.response_data = response.json()
self.logger.info(f"β
{self.config.name}: {method} {endpoint} - {response.status_code} ({response_time:.1f}ms)")
except json.JSONDecodeError as e:
result.success = False
result.error_type = APIErrorType.VALIDATION_ERROR.value
result.error_message = f"Invalid JSON response: {e}"
self.logger.warning(f"β {self.config.name}: Invalid JSON response")
else:
result.error_type = APIErrorType.SERVER_ERROR.value
result.error_message = response.text
self.logger.warning(f"β {self.config.name}: {method} {endpoint} - {response.status_code}")
return result
except Exception as e:
error_type = self._categorize_error(e, getattr(e, 'response', {}).get('status_code'))
response_time = (time.time() - start_time) * 1000
self.logger.warning(f"β {self.config.name}: {method} {endpoint} - {error_type.value} (attempt {retry_count + 1})")
# Check if we should retry
if self._should_retry(error_type, retry_count):
retry_count += 1
delay = self._calculate_retry_delay(error_type, retry_count - 1)
self.logger.info(f"π Retrying {self.config.name} in {delay:.1f}s (attempt {retry_count + 1})")
await asyncio.sleep(delay)
continue
else:
# Don't retry, return error result
return APITestResult(
api_name=self.config.name,
endpoint=endpoint,
success=False,
response_time_ms=response_time,
error_type=error_type.value,
error_message=str(e),
retry_count=retry_count
)
# This should never be reached, but just in case
return APITestResult(
api_name=self.config.name,
endpoint=endpoint,
success=False,
response_time_ms=(time.time() - start_time) * 1000,
error_type=APIErrorType.UNKNOWN_ERROR.value,
error_message="Max retries exceeded",
retry_count=retry_count
)
@asynccontextmanager
async def get_client(self):
"""Context manager for HTTP client."""
if not self._client:
await self._setup_client()
try:
yield self._client
finally:
pass
# =========================================================================
# SPECIFIC API TESTING METHODS
# =========================================================================
async def test_anthropic_api(self, test_message: str = "Hello, how are you?") -> APITestResult:
"""
Test Anthropic Claude API with a simple message.
This demonstrates testing a specific API with proper request structure.
"""
self.logger.info(f"Testing Anthropic API with message: '{test_message}'")
# Anthropic API request structure
request_data = {
"model": "claude-3-sonnet-20240229",
"max_tokens": 100,
"messages": [
{
"role": "user",
"content": test_message
}
]
}
result = await self.test_api_call(
method="POST",
endpoint="/v1/messages",
data=request_data
)
# Validate Anthropic-specific response structure
if result.success and result.response_data:
result = self._validate_anthropic_response(result)
return result
async def test_tavily_api(self, query: str = "travel destinations") -> APITestResult:
"""
Test Tavily search API with a simple query.
This demonstrates testing a different API with different request structure.
"""
self.logger.info(f"Testing Tavily API with query: '{query}'")
# Tavily API request structure
request_data = {
"query": query,
"search_depth": "basic",
"include_answer": True,
"include_raw_content": False,
"max_results": 5
}
result = await self.test_api_call(
method="POST",
endpoint="/search",
data=request_data
)
# Validate Tavily-specific response structure
if result.success and result.response_data:
result = self._validate_tavily_response(result)
return result
def _validate_anthropic_response(self, result: APITestResult) -> APITestResult:
"""
Validate Anthropic API response structure.
This demonstrates response validation for specific APIs.
"""
try:
data = result.response_data
# Check required fields
required_fields = ["id", "type", "role", "content"]
for field in required_fields:
if field not in data:
result.success = False
result.error_type = APIErrorType.VALIDATION_ERROR.value
result.error_message = f"Missing required field: {field}"
return result
# Check content structure
if not isinstance(data.get("content"), list):
result.success = False
result.error_type = APIErrorType.VALIDATION_ERROR.value
result.error_message = "Content field must be a list"
return result
# Check usage information
if "usage" in data:
usage = data["usage"]
if not all(key in usage for key in ["input_tokens", "output_tokens"]):
result.success = False
result.error_type = APIErrorType.VALIDATION_ERROR.value
result.error_message = "Usage field missing required token information"
return result
self.logger.info(f"β
Anthropic response validation passed")
except Exception as e:
result.success = False
result.error_type = APIErrorType.VALIDATION_ERROR.value
result.error_message = f"Response validation error: {e}"
self.logger.error(f"β Anthropic response validation failed: {e}")
return result
def _validate_tavily_response(self, result: APITestResult) -> APITestResult:
"""
Validate Tavily API response structure.
This demonstrates response validation for a different API.
"""
try:
data = result.response_data
# Check required fields
required_fields = ["query", "follow_up_questions", "images", "results"]
for field in required_fields:
if field not in data:
result.success = False
result.error_type = APIErrorType.VALIDATION_ERROR.value
result.error_message = f"Missing required field: {field}"
return result
# Check results structure
results = data.get("results", [])
if not isinstance(results, list):
result.success = False
result.error_type = APIErrorType.VALIDATION_ERROR.value
result.error_message = "Results field must be a list"
return result
# Check individual result structure
for i, result_item in enumerate(results):
if not isinstance(result_item, dict):
result.success = False
result.error_type = APIErrorType.VALIDATION_ERROR.value
result.error_message = f"Result {i} must be a dictionary"
return result
if not all(key in result_item for key in ["title", "url", "content"]):
result.success = False
result.error_type = APIErrorType.VALIDATION_ERROR.value
result.error_message = f"Result {i} missing required fields"
return result
self.logger.info(f"β
Tavily response validation passed")
except Exception as e:
result.success = False
result.error_type = APIErrorType.VALIDATION_ERROR.value
result.error_message = f"Response validation error: {e}"
self.logger.error(f"β Tavily response validation failed: {e}")
return result
# =========================================================================
# DEBUGGING UTILITIES
# =========================================================================
def debug_api_issue(self, result: APITestResult) -> Dict[str, Any]:
"""
Provide step-by-step debugging information for API issues.
This demonstrates comprehensive debugging techniques.
"""
debug_info = {
"api_name": result.api_name,
"endpoint": result.endpoint,
"success": result.success,
"response_time_ms": result.response_time_ms,
"timestamp": result.timestamp,
"retry_count": result.retry_count
}
if not result.success:
debug_info["error_analysis"] = self._analyze_error(result)
debug_info["troubleshooting_steps"] = self._get_troubleshooting_steps(result)
debug_info["recommendations"] = self._get_recommendations(result)
return debug_info
def _analyze_error(self, result: APITestResult) -> Dict[str, Any]:
"""Analyze the error and provide detailed information."""
analysis = {
"error_type": result.error_type,
"error_message": result.error_message,
"status_code": result.status_code
}
# Add specific analysis based on error type
if result.error_type == APIErrorType.AUTHENTICATION_ERROR.value:
analysis["likely_causes"] = [
"Invalid API key",
"Expired API key",
"Incorrect authentication header format",
"API key not activated"
]
elif result.error_type == APIErrorType.RATE_LIMIT_ERROR.value:
analysis["likely_causes"] = [
"Too many requests per minute",
"Exceeded daily quota",
"Rate limit not properly handled"
]
elif result.error_type == APIErrorType.NETWORK_ERROR.value:
analysis["likely_causes"] = [
"Internet connection issues",
"DNS resolution problems",
"Firewall blocking requests",
"API server down"
]
elif result.error_type == APIErrorType.TIMEOUT_ERROR.value:
analysis["likely_causes"] = [
"Request timeout too short",
"API server overloaded",
"Network latency issues"
]
return analysis
def _get_troubleshooting_steps(self, result: APITestResult) -> List[str]:
"""Get step-by-step troubleshooting steps."""
steps = []
if result.error_type == APIErrorType.AUTHENTICATION_ERROR.value:
steps = [
"1. Verify your API key is correct",
"2. Check if the API key is activated",
"3. Ensure the authentication header format is correct",
"4. Test with a simple curl command",
"5. Check API documentation for authentication requirements"
]
elif result.error_type == APIErrorType.RATE_LIMIT_ERROR.value:
steps = [
"1. Check your current rate limit usage",
"2. Implement proper rate limiting in your code",
"3. Consider upgrading your API plan",
"4. Add delays between requests",
"5. Use exponential backoff for retries"
]
elif result.error_type == APIErrorType.NETWORK_ERROR.value:
steps = [
"1. Check your internet connection",
"2. Test with a different network",
"3. Verify the API endpoint URL",
"4. Check if there are firewall restrictions",
"5. Test with a simple ping to the API server"
]
elif result.error_type == APIErrorType.TIMEOUT_ERROR.value:
steps = [
"1. Increase the timeout value",
"2. Check if the API server is responding",
"3. Test with a smaller request",
"4. Check network latency",
"5. Consider using a different API endpoint"
]
return steps
def _get_recommendations(self, result: APITestResult) -> List[str]:
"""Get recommendations for fixing the issue."""
recommendations = []
if result.error_type == APIErrorType.AUTHENTICATION_ERROR.value:
recommendations = [
"Double-check your API key configuration",
"Review the API documentation for authentication examples",
"Test with a minimal request first"
]
elif result.error_type == APIErrorType.RATE_LIMIT_ERROR.value:
recommendations = [
"Implement proper rate limiting",
"Consider using a queue system for requests",
"Monitor your API usage regularly"
]
elif result.error_type == APIErrorType.NETWORK_ERROR.value:
recommendations = [
"Check your network configuration",
"Consider using a VPN if there are regional restrictions",
"Test from a different location"
]
elif result.error_type == APIErrorType.TIMEOUT_ERROR.value:
recommendations = [
"Optimize your request size",
"Implement proper timeout handling",
"Consider using async requests for better performance"
]
return recommendations
# =========================================================================
# UTILITY METHODS
# =========================================================================
def get_stats(self) -> Dict[str, Any]:
"""Get API tester statistics."""
return {
"api_name": self.config.name,
"request_count": self._request_count,
"rate_limit_tokens": self._rate_limit_tokens,
"rate_limit_reset": self._rate_limit_reset
}
async def health_check(self) -> bool:
"""Check if the API is healthy."""
try:
# Try a simple request to check connectivity
result = await self.test_api_call("GET", "/health")
return result.success
except Exception as e:
self.logger.error(f"Health check failed for {self.config.name}: {e}")
return False
|