#!/usr/bin/env python3
"""Test API with correct header"""
import os
import sys
from pathlib import Path
import httpx
sys.path.insert(0, str(Path(__file__).parent / "src"))
from mcp_opinion.config import OpinionConfig
async def test_api_correct():
"""Test API with correct apikey header"""
config = OpinionConfig.from_env()
print(f"\nTesting Opinion.trade API")
print(f"="*60)
print(f"API Host: {config.api_host}")
print(f"API Key: {config.api_key[:20]}...")
try:
async with httpx.AsyncClient(timeout=30) as client:
# Correct header format
headers = {
"apikey": config.api_key,
"Content-Type": "application/json"
}
url = f"{config.api_host}/openapi/market"
print(f"\nMaking request to: {url}")
print(f"Header: apikey = {config.api_key[:20]}...")
response = await client.get(url, headers=headers)
print(f"\nResponse Status: {response.status_code}")
if response.status_code == 401:
print(f"✗ 401 Unauthorized - API key may be invalid or expired")
print(f" Response: {response.text}")
elif response.status_code == 200:
try:
data = response.json()
print(f"✓ 200 OK - API is working!")
print(f" Response structure: {list(data.keys())}")
if 'result' in data:
result = data['result']
if isinstance(result, list):
print(f" Markets found: {len(result)}")
if result:
print(f" First market keys: {list(result[0].keys())}")
except Exception as e:
print(f"✗ Could not parse response: {e}")
else:
print(f"✗ Unexpected status {response.status_code}")
print(f" Response: {response.text[:200]}")
except Exception as e:
print(f"✗ Error: {e}")
import traceback
traceback.print_exc()
if __name__ == "__main__":
import asyncio
asyncio.run(test_api_correct())