Spaces:
Sleeping
Sleeping
File size: 3,465 Bytes
99bbd9b | 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 | # from fastapi import APIRouter, HTTPException
# from app.models import DatabaseConnectionRequest
# # from app.services.sql_agent import setup_database_connection
# from sqlalchemy.exc import OperationalError, DatabaseError
# from urllib.parse import urlparse
# from app.services.sql_agent_instance import sql_agent
# router = APIRouter()
# @router.post("/setup-connection")
# async def setup_connection(request: DatabaseConnectionRequest):
# try:
# # Basic validation of connection string format
# parsed = urlparse(request.connection_string)
# if not all([parsed.scheme, parsed.netloc]):
# raise HTTPException(
# status_code=400,
# detail="Invalid connection string format. Expected format: dialect+driver://username:password@host:port/database"
# )
# sql_agent.setup_database_connection(request.connection_string)
# return {"message": "Database connection established successfully!"}
# except OperationalError as e:
# raise HTTPException(
# status_code=503,
# detail=f"Failed to connect to database: Connection refused or invalid credentials. Details: {str(e)}"
# )
# except DatabaseError as e:
# raise HTTPException(
# status_code=500,
# detail=f"Database error occurred: {str(e)}"
# )
# except ValueError as e:
# raise HTTPException(
# status_code=400,
# detail=f"Invalid configuration: {str(e)}"
# )
# except Exception as e:
# raise HTTPException(
# status_code=500,
# detail=f"Unexpected error occurred while setting up database connection: {str(e)}"
# )
# app/api/v1/endpoints/database_connection.py
from fastapi import APIRouter, HTTPException
from pydantic import BaseModel
from app.services.sql_agent_instance import sql_agent
from sqlalchemy.exc import OperationalError, DatabaseError
from urllib.parse import urlparse
router = APIRouter()
class DatabaseConnectionRequest(BaseModel):
connection_string: str
@router.post("/setup-connection")
async def setup_connection(request: DatabaseConnectionRequest):
try:
# Basic validation of connection string format
parsed = urlparse(request.connection_string)
if not all([parsed.scheme, parsed.netloc]):
raise HTTPException(
status_code=400,
detail="Invalid connection string format. Expected format: dialect+driver://username:password@host:port/database"
)
sql_agent.setup_database_connection(request.connection_string)
return {"message": "Database connection established successfully!"}
except OperationalError as e:
raise HTTPException(
status_code=503,
detail=f"Failed to connect to database: Connection refused or invalid credentials. Details: {str(e)}"
)
except DatabaseError as e:
raise HTTPException(
status_code=500,
detail=f"Database error occurred: {str(e)}"
)
except ValueError as e:
raise HTTPException(
status_code=400,
detail=f"Invalid configuration: {str(e)}"
)
except Exception as e:
raise HTTPException(
status_code=500,
detail=f"Unexpected error occurred while setting up database connection: {str(e)}"
) |