fleet-memory/generate_openapi.py
2025-11-07 15:12:37 +01:00

54 lines
1.8 KiB
Python

#!/usr/bin/env python3
"""
Generate OpenAPI specification from FastAPI app.
This script imports the FastAPI app and exports its OpenAPI schema to a JSON file.
"""
import json
import sys
import os
from pathlib import Path
# Add parent directory to path to import memory module
sys.path.insert(0, str(Path(__file__).parent))
from memora.api import create_app
from memora import TemporalSemanticMemory
def generate_openapi_spec(output_path: str = "openapi.json"):
"""Generate OpenAPI spec and save to file."""
# Create a temporary memory instance for OpenAPI generation
_memory = TemporalSemanticMemory(
db_url="mock",
memory_llm_provider="ollama",
memory_llm_api_key="mock",
memory_llm_model="mock",
)
app = create_app(_memory)
# Get the OpenAPI schema from the app
openapi_schema = app.openapi()
# Write to file
output_file = Path(output_path)
with open(output_file, 'w') as f:
json.dump(openapi_schema, f, indent=2)
print(f"✓ OpenAPI specification generated: {output_file.absolute()}")
print(f" - Title: {openapi_schema['info']['title']}")
print(f" - Version: {openapi_schema['info']['version']}")
print(f" - Endpoints: {len(openapi_schema['paths'])}")
# List endpoints
print("\n Endpoints:")
for path, methods in openapi_schema['paths'].items():
for method in methods.keys():
if method.upper() in ['GET', 'POST', 'PUT', 'DELETE', 'PATCH']:
endpoint_info = methods[method]
summary = endpoint_info.get('summary', 'No summary')
tags = ', '.join(endpoint_info.get('tags', ['untagged']))
print(f" {method.upper():6} {path:30} [{tags}] - {summary}")
if __name__ == "__main__":
output = sys.argv[1] if len(sys.argv) > 1 else "openapi.json"
generate_openapi_spec(output)