- Replace black, isort, and flake8 with Ruff (all-in-one linter and formatter) - Add comprehensive pyproject.toml configuration - Simplify Makefile code quality targets - Configure exclusions for venv/.venv in pyproject.toml - Auto-fix 1,359 linting issues across codebase Benefits: - Much faster builds (Ruff is written in Rust) - Single tool replaces multiple tools - More comprehensive rule set (UP, B, C4, SIM, PIE, RET, Q) - All configuration centralized in pyproject.toml - Better import sorting and formatting consistency 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
160 lines
5.5 KiB
Python
160 lines
5.5 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
FastAPI Route Diagnostics Script
|
|
|
|
Run this script to check if your vendor routes are properly configured.
|
|
Usage: python route_diagnostics.py
|
|
"""
|
|
|
|
|
|
|
|
def check_route_order():
|
|
"""Check if routes are registered in the correct order."""
|
|
print("🔍 Checking FastAPI Route Configuration...\n")
|
|
|
|
try:
|
|
# Try to import the vendor router
|
|
from app.api.v1.vendor import router as vendor_router
|
|
|
|
print("✅ Successfully imported vendor router\n")
|
|
|
|
# Check if routes are registered
|
|
routes = vendor_router.routes
|
|
print(f"📊 Total routes registered: {len(routes)}\n")
|
|
|
|
# Analyze route order
|
|
json_routes = []
|
|
html_routes = []
|
|
|
|
for route in routes:
|
|
if hasattr(route, "path"):
|
|
path = route.path
|
|
methods = getattr(route, "methods", set())
|
|
|
|
# Determine if JSON or HTML based on common patterns
|
|
if "login" in path or "dashboard" in path or "products" in path:
|
|
# Check response class if available
|
|
if hasattr(route, "response_class"):
|
|
response_class = str(route.response_class)
|
|
if "HTML" in response_class:
|
|
html_routes.append((path, methods))
|
|
else:
|
|
json_routes.append((path, methods))
|
|
else:
|
|
# Check endpoint name/tags
|
|
endpoint = getattr(route, "endpoint", None)
|
|
if endpoint and "page" in endpoint.__name__.lower():
|
|
html_routes.append((path, methods))
|
|
else:
|
|
json_routes.append((path, methods))
|
|
|
|
print("📋 Route Analysis:")
|
|
print(f" JSON API routes: {len(json_routes)}")
|
|
print(f" HTML page routes: {len(html_routes)}\n")
|
|
|
|
# Check for specific vendor info route
|
|
vendor_info_found = False
|
|
for route in routes:
|
|
if hasattr(route, "path"):
|
|
if route.path == "/{vendor_code}" and "GET" in getattr(
|
|
route, "methods", set()
|
|
):
|
|
vendor_info_found = True
|
|
print("✅ Found vendor info endpoint: GET /{vendor_code}")
|
|
break
|
|
|
|
if not vendor_info_found:
|
|
print("❌ WARNING: Vendor info endpoint (GET /{vendor_code}) not found!")
|
|
print(" This is likely causing your JSON parsing error.")
|
|
print(" Action required: Add the vendor info endpoint\n")
|
|
return False
|
|
|
|
print("\n✅ Route configuration looks good!")
|
|
return True
|
|
|
|
except ImportError as e:
|
|
print(f"❌ Error importing vendor router: {e}")
|
|
print(" Make sure you're running this from your project root")
|
|
return False
|
|
except Exception as e:
|
|
print(f"❌ Unexpected error: {e}")
|
|
return False
|
|
|
|
|
|
def test_vendor_endpoint():
|
|
"""Test if the vendor endpoint returns JSON."""
|
|
print("\n🧪 Testing Vendor Endpoint...\n")
|
|
|
|
try:
|
|
import requests
|
|
|
|
# Test with a sample vendor code
|
|
vendor_code = "WIZAMART"
|
|
url = f"http://localhost:8000/api/v1/vendor/{vendor_code}"
|
|
|
|
print(f"📡 Making request to: {url}")
|
|
|
|
response = requests.get(url, timeout=5)
|
|
|
|
print(f" Status Code: {response.status_code}")
|
|
print(f" Content-Type: {response.headers.get('content-type', 'N/A')}")
|
|
|
|
# Check if response is JSON
|
|
content_type = response.headers.get("content-type", "")
|
|
if "application/json" in content_type:
|
|
print("✅ Response is JSON")
|
|
data = response.json()
|
|
print(f" Vendor: {data.get('name', 'N/A')}")
|
|
print(f" Code: {data.get('vendor_code', 'N/A')}")
|
|
return True
|
|
if "text/html" in content_type:
|
|
print("❌ ERROR: Response is HTML, not JSON!")
|
|
print(" This confirms the route ordering issue")
|
|
print(" The HTML page route is catching the API request")
|
|
return False
|
|
print(f"⚠️ Unknown content type: {content_type}")
|
|
return False
|
|
|
|
except requests.exceptions.ConnectionError:
|
|
print("⚠️ Cannot connect to server. Is FastAPI running on localhost:8000?")
|
|
return None
|
|
except Exception as e:
|
|
print(f"❌ Error testing endpoint: {e}")
|
|
return False
|
|
|
|
|
|
def main():
|
|
"""Run all diagnostics."""
|
|
print("=" * 60)
|
|
print("FastAPI Vendor Route Diagnostics")
|
|
print("=" * 60 + "\n")
|
|
|
|
# Check route configuration
|
|
config_ok = check_route_order()
|
|
|
|
# Test actual endpoint
|
|
endpoint_ok = test_vendor_endpoint()
|
|
|
|
# Summary
|
|
print("\n" + "=" * 60)
|
|
print("SUMMARY")
|
|
print("=" * 60)
|
|
|
|
if config_ok and endpoint_ok:
|
|
print("✅ All checks passed! Your vendor routes are configured correctly.")
|
|
elif config_ok is False:
|
|
print("❌ Route configuration issues detected.")
|
|
print(" Action: Add vendor info endpoint and check route order")
|
|
elif endpoint_ok is False:
|
|
print("❌ Endpoint is returning HTML instead of JSON.")
|
|
print(" Action: Check route registration order in vendor/__init__.py")
|
|
elif endpoint_ok is None:
|
|
print("⚠️ Could not test endpoint (server not running)")
|
|
print(" Action: Start your FastAPI server and run this script again")
|
|
|
|
print("\n📚 See TROUBLESHOOTING_GUIDE.md for detailed solutions")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|