Coverage for src/semware/api/tables.py: 77%

53 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2025-09-09 02:16 -0700

1"""API endpoints for table management.""" 

2 

3from fastapi import APIRouter, Depends, HTTPException, status 

4from loguru import logger 

5 

6from ..models.requests import ( 

7 CreateTableRequest, 

8 CreateTableResponse, 

9 GetTableResponse, 

10 ListTablesResponse, 

11) 

12from ..models.schemas import SuccessResponse 

13from ..services.vectordb import vectordb 

14from .auth import api_key_auth 

15 

16router = APIRouter() 

17 

18 

19@router.post( 

20 "", 

21 response_model=CreateTableResponse, 

22 status_code=status.HTTP_201_CREATED, 

23 summary="Create a new table", 

24 description="Create a new table with the specified schema for storing documents and embeddings.", 

25) 

26async def create_table( 

27 request: CreateTableRequest, api_key: str = Depends(api_key_auth) 

28) -> CreateTableResponse: 

29 """Create a new table with the specified schema.""" 

30 try: 

31 logger.info(f"Creating table: {request.table_schema.name}") 

32 vectordb.create_table(request.table_schema) 

33 

34 return CreateTableResponse( 

35 message=f"Table '{request.table_schema.name}' created successfully", 

36 table_name=request.table_schema.name, 

37 ) 

38 

39 except ValueError as e: 

40 logger.error(f"Table creation failed: {e}") 

41 raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e)) 

42 except Exception as e: 

43 logger.exception(f"Unexpected error creating table: {e}") 

44 raise HTTPException( 

45 status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, 

46 detail="Failed to create table", 

47 ) 

48 

49 

50@router.get( 

51 "", 

52 response_model=ListTablesResponse, 

53 summary="List all tables", 

54 description="Get a list of all available tables in the database.", 

55) 

56async def list_tables(api_key: str = Depends(api_key_auth)) -> ListTablesResponse: 

57 """List all available tables.""" 

58 try: 

59 logger.debug("Listing all tables") 

60 table_names = vectordb.get_table_names() 

61 

62 return ListTablesResponse(tables=table_names, count=len(table_names)) 

63 

64 except Exception as e: 

65 logger.exception(f"Error listing tables: {e}") 

66 raise HTTPException( 

67 status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, 

68 detail="Failed to list tables", 

69 ) 

70 

71 

72@router.get( 

73 "/{table_name}", 

74 response_model=GetTableResponse, 

75 summary="Get table information", 

76 description="Get detailed information about a specific table including its schema and record count.", 

77) 

78async def get_table( 

79 table_name: str, api_key: str = Depends(api_key_auth) 

80) -> GetTableResponse: 

81 """Get information about a specific table.""" 

82 try: 

83 logger.debug(f"Getting table info: {table_name}") 

84 

85 # Get table schema 

86 schema = vectordb.get_table_schema(table_name) 

87 

88 # Get record count 

89 record_count = vectordb.get_table_record_count(table_name) 

90 

91 return GetTableResponse( 

92 table_name=table_name, table_schema=schema, record_count=record_count 

93 ) 

94 

95 except ValueError as e: 

96 logger.error(f"Table not found: {e}") 

97 raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=str(e)) 

98 except Exception as e: 

99 logger.exception(f"Error getting table info: {e}") 

100 raise HTTPException( 

101 status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, 

102 detail="Failed to get table information", 

103 ) 

104 

105 

106@router.delete( 

107 "/{table_name}", 

108 response_model=SuccessResponse, 

109 summary="Delete a table", 

110 description="Delete a table and all its data permanently. This operation cannot be undone.", 

111) 

112async def delete_table( 

113 table_name: str, api_key: str = Depends(api_key_auth) 

114) -> SuccessResponse: 

115 """Delete a table and all its data.""" 

116 try: 

117 logger.info(f"Deleting table: {table_name}") 

118 vectordb.delete_table(table_name) 

119 

120 return SuccessResponse(message=f"Table '{table_name}' deleted successfully") 

121 

122 except ValueError as e: 

123 logger.error(f"Table deletion failed: {e}") 

124 raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=str(e)) 

125 except Exception as e: 

126 logger.exception(f"Error deleting table: {e}") 

127 raise HTTPException( 

128 status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, 

129 detail="Failed to delete table", 

130 )