HFAgentsCourse / test_api.py
nicolacaione's picture
Used to pass the exam
3874cd4
#!/usr/bin/env python3
"""
API Testing Script for HF Agents Course Scoring API
Tests connectivity, endpoints, and response formats
"""
import requests
import json
import sys
from datetime import datetime
# API Configuration
API_BASE_URL = "https://agents-course-unit4-scoring.hf.space"
QUESTIONS_URL = f"{API_BASE_URL}/questions"
SUBMIT_URL = f"{API_BASE_URL}/submit"
def test_connectivity():
"""Test basic API connectivity"""
print("πŸ” Testing API Connectivity...")
print(f"πŸ“ Base URL: {API_BASE_URL}")
print("-" * 50)
try:
response = requests.get(API_BASE_URL, timeout=10)
print(f"βœ… Base URL Status: {response.status_code}")
print(f"πŸ“‹ Headers: {dict(response.headers)}")
if response.text:
print(f"πŸ“„ Response (first 200 chars): {response.text[:200]}")
return True
except requests.exceptions.RequestException as e:
print(f"❌ Base URL Error: {e}")
return False
def test_questions_endpoint():
"""Test questions endpoint"""
print("\nπŸ” Testing Questions Endpoint...")
print(f"πŸ“ Questions URL: {QUESTIONS_URL}")
print("-" * 50)
try:
response = requests.get(QUESTIONS_URL, timeout=15)
print(f"βœ… Status Code: {response.status_code}")
print(f"πŸ“‹ Headers: {dict(response.headers)}")
if response.status_code == 200:
try:
data = response.json()
print(f"πŸ“Š Response Type: {type(data)}")
if isinstance(data, list):
print(f"πŸ“ˆ Questions Count: {len(data)}")
if data:
print(f"πŸ“ Sample Question Keys: {list(data[0].keys()) if data else 'Empty'}")
# Show first question safely
if data:
first_q = data[0]
print(f"πŸ“‹ First Question ID: {first_q.get('task_id', 'N/A')}")
question_text = first_q.get('question', '')
print(f"πŸ“‹ First Question (50 chars): {question_text[:50]}...")
else:
print(f"πŸ“„ Response Content: {data}")
return True, data
except json.JSONDecodeError as e:
print(f"❌ JSON Decode Error: {e}")
print(f"πŸ“„ Raw Response (first 500 chars): {response.text[:500]}")
return False, None
else:
print(f"❌ HTTP Error: {response.status_code}")
print(f"πŸ“„ Error Response: {response.text[:500]}")
return False, None
except requests.exceptions.RequestException as e:
print(f"❌ Request Error: {e}")
return False, None
def test_submit_endpoint():
"""Test submit endpoint with minimal payload"""
print("\nπŸ” Testing Submit Endpoint...")
print(f"πŸ“ Submit URL: {SUBMIT_URL}")
print("-" * 50)
# Create minimal test payload
test_payload = {
"username": "test_user",
"agent_code": "https://github.com/test/repo",
"answers": [
{"task_id": "test_task_001", "submitted_answer": "test_answer"}
]
}
try:
# Note: This might fail with authentication or validation errors, but it tests connectivity
response = requests.post(SUBMIT_URL, json=test_payload, timeout=30)
print(f"πŸ“Š Status Code: {response.status_code}")
print(f"πŸ“‹ Headers: {dict(response.headers)}")
try:
data = response.json()
print(f"πŸ“„ Response: {json.dumps(data, indent=2)}")
except:
print(f"πŸ“„ Raw Response: {response.text[:500]}")
return response.status_code, response.text
except requests.exceptions.RequestException as e:
print(f"❌ Submit Error: {e}")
return None, str(e)
def check_huggingface_space():
"""Check if the HuggingFace Space exists and is accessible"""
print("\nπŸ” Checking HuggingFace Space...")
hf_space_url = "https://huggingface.co/spaces/agents-course/unit4-scoring"
print(f"πŸ“ HF Space URL: {hf_space_url}")
print("-" * 50)
try:
response = requests.get(hf_space_url, timeout=10)
print(f"πŸ“Š HF Space Status: {response.status_code}")
if response.status_code == 200:
if "Space" in response.text and "Gradio" in response.text:
print("βœ… HuggingFace Space appears to be active")
else:
print("⚠️ HuggingFace Space exists but might not be a Gradio space")
else:
print(f"❌ HuggingFace Space not accessible: {response.status_code}")
except requests.exceptions.RequestException as e:
print(f"❌ HF Space Check Error: {e}")
def main():
"""Run all API tests"""
print("πŸš€ API Testing Suite")
print(f"⏰ Started at: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
print("=" * 60)
# Test 1: Basic connectivity
connectivity_ok = test_connectivity()
# Test 2: Questions endpoint
questions_ok, questions_data = test_questions_endpoint()
# Test 3: Submit endpoint (will likely fail, but shows connectivity)
submit_status, submit_response = test_submit_endpoint()
# Test 4: Check HuggingFace Space
check_huggingface_space()
# Summary
print("\n" + "=" * 60)
print("πŸ“‹ SUMMARY")
print("=" * 60)
print(f"πŸ”— Base URL: {'βœ… OK' if connectivity_ok else '❌ FAILED'}")
print(f"❓ Questions: {'βœ… OK' if questions_ok else '❌ FAILED'}")
if questions_ok and questions_data:
print(f"πŸ“Š Available Questions: {len(questions_data) if isinstance(questions_data, list) else 'Unknown'}")
print(f"πŸ“€ Submit Endpoint: {'πŸ”— Reachable' if submit_status else '❌ FAILED'}")
if not any([connectivity_ok, questions_ok]):
print("\n❌ API appears to be completely down or unreachable")
print("πŸ’‘ Suggestions:")
print(" - Check your internet connection")
print(" - Try again later (server might be temporarily down)")
print(" - Contact course administrators if issue persists")
elif questions_ok:
print("\nβœ… Questions endpoint is working - you can fetch questions")
print("❌ Submit endpoint issue might be temporary server problem")
print(f"\n⏰ Completed at: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
if __name__ == "__main__":
main()