#!/usr/bin/env python3
"""Test raw API response"""
import os
import sys
from pathlib import Path
import httpx
import json
sys.path.insert(0, str(Path(__file__).parent / "src"))
from mcp_opinion.config import OpinionConfig
async def test_raw_api():
"""Test raw API response without parsing"""
config = OpinionConfig.from_env()
print(f"\nRaw API Response Test")
print(f"="*60)
print(f"API Host: {config.api_host}")
print(f"Chain ID: {config.chain_id}")
try:
async with httpx.AsyncClient(timeout=60) as client:
headers = {
"apikey": config.api_key,
"Content-Type": "application/json"
}
url = f"{config.api_host}/openapi/market"
print(f"\nFetching: {url}")
print(f"Waiting for response...")
response = await client.get(url, headers=headers, follow_redirects=True)
print(f"\n✓ Response received!")
print(f"Status Code: {response.status_code}")
print(f"Content-Type: {response.headers.get('content-type')}")
print(f"Content Length: {len(response.text)} bytes")
# Try to parse as JSON
try:
data = response.json()
print(f"\n✓ Valid JSON response")
print(f"Top-level keys: {list(data.keys())}")
# Check response format
if 'code' in data:
print(f" Format: code/msg/result (expected by client)")
print(f" code: {data.get('code')}")
print(f" msg: {data.get('msg')}")
elif 'errno' in data:
print(f" Format: errno/errmsg/result")
print(f" errno: {data.get('errno')}")
print(f" errmsg: {data.get('errmsg')}")
# Check result
result = data.get('result')
if isinstance(result, list):
print(f" Result: list with {len(result)} items")
if result:
print(f" First item keys: {list(result[0].keys())}")
elif isinstance(result, dict):
print(f" Result: dict with {len(result)} keys")
print(f" Keys: {list(result.keys())}")
else:
print(f" Result: {type(result)}")
except Exception as e:
print(f"✗ Not valid JSON: {e}")
print(f"Response preview: {response.text[:300]}")
except Exception as e:
print(f"✗ Error: {e}")
import traceback
traceback.print_exc()
if __name__ == "__main__":
import asyncio
asyncio.run(test_raw_api())