-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_api_test.py
More file actions
143 lines (116 loc) · 4.84 KB
/
Copy pathsimple_api_test.py
File metadata and controls
143 lines (116 loc) · 4.84 KB
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
#!/usr/bin/env python3
"""
Simple API test script to verify Tripo AI API connection.
"""
import os
import sys
import asyncio
from pathlib import Path
# Add src to Python path
project_root = Path(__file__).parent
src_path = project_root / "src"
sys.path.insert(0, str(src_path))
# Load environment variables
from dotenv import load_dotenv
load_dotenv()
async def test_api_connection():
"""Test the API connection with the configured API key."""
print("🔍 Testing Tripo AI API Connection...")
# Get API key from environment
api_key = os.getenv("TRIPO_API_KEY")
api_url = os.getenv("TRIPO_API_URL", "https://api.tripo3d.ai/v2/openapi")
if not api_key:
print("❌ No API key found in environment variables")
return False
print(f"🔑 API Key: {api_key[:10]}...")
print(f"🌐 API URL: {api_url}")
print("-" * 50)
try:
import aiohttp
# Test basic connection
async with aiohttp.ClientSession() as session:
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
# Test with a simple task creation instead of health check
print("📡 Testing API with simple task creation...")
try:
# Create a simple test payload
test_payload = {
"type": "text_to_model",
"prompt": "A simple red cube",
"model_version": "v2.5-20250123"
}
async with session.post(f"{api_url}/task", headers=headers, json=test_payload) as response:
if response.status == 200:
data = await response.json()
print("✅ API connection successful!")
print(f"Response: {data}")
if data.get("code") == 0 and data.get("data", {}).get("task_id"):
print(f"✅ Task created successfully! Task ID: {data['data']['task_id']}")
return True
else:
print(f"⚠️ API responded but task creation failed: {data}")
return False
else:
error_text = await response.text()
print(f"❌ API request failed with status {response.status}")
print(f"Error: {error_text}")
return False
except Exception as e:
print(f"❌ Connection error: {e}")
return False
except ImportError:
print("❌ aiohttp not installed. Please install it with: pip install aiohttp")
return False
except Exception as e:
print(f"❌ Unexpected error: {e}")
return False
async def test_credits():
"""Test credit balance endpoint."""
print("\n💰 Testing credit balance...")
api_key = os.getenv("TRIPO_API_KEY")
api_url = os.getenv("TRIPO_API_URL", "https://api.tripo3d.ai/v2/openapi")
try:
import aiohttp
async with aiohttp.ClientSession() as session:
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
# Try to get user info or credits
try:
async with session.get(f"{api_url}/user", headers=headers) as response:
if response.status == 200:
data = await response.json()
print("✅ User info retrieved successfully!")
print(f"User data: {data}")
return True
else:
print(f"⚠️ Could not get user info (status {response.status})")
return False
except Exception as e:
print(f"⚠️ Could not check user info: {e}")
return False
except Exception as e:
print(f"❌ Error checking credits: {e}")
return False
async def main():
"""Main test function."""
print("🚀 Tripo AI API Connection Test")
print("=" * 50)
# Test basic connection
connection_ok = await test_api_connection()
if connection_ok:
# Test credits
await test_credits()
print("\n🎉 API connection test completed successfully!")
print("🚀 Your API key is valid and ready to use!")
else:
print("\n❌ API connection failed. Please check:")
print(" - Your API key is correct")
print(" - You have internet connection")
print(" - The API service is available")
if __name__ == "__main__":
asyncio.run(main())