Coverage for /usr/local/lib/python3.12/site-packages/prefect/server/api/block_documents.py: 83%

48 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2025-12-05 10:48 +0000

1""" 

2Routes for interacting with block objects. 

3""" 

4 

5from typing import List, Optional 1c

6from uuid import UUID 1c

7 

8from fastapi import Body, Depends, HTTPException, Path, Query, status 1c

9 

10from prefect.server import models, schemas 1c

11from prefect.server.api import dependencies 1c

12from prefect.server.database import PrefectDBInterface, provide_database_interface 1c

13from prefect.server.utilities.server import PrefectRouter 1c

14 

15router: PrefectRouter = PrefectRouter( 1c

16 prefix="/block_documents", tags=["Block documents"] 

17) 

18 

19 

20@router.post("/", status_code=status.HTTP_201_CREATED) 1c

21async def create_block_document( 1c

22 block_document: schemas.actions.BlockDocumentCreate, 

23 db: PrefectDBInterface = Depends(provide_database_interface), 

24) -> schemas.core.BlockDocument: 

25 """ 

26 Create a new block document. 

27 

28 For more information, see https://docs.prefect.io/v3/concepts/blocks. 

29 """ 

30 async with db.session_context(begin_transaction=True) as session: 1ab

31 if block_document.name is not None: 1ab

32 exists = ( 

33 await models.block_documents.block_document_with_unique_values_exists( 

34 session=session, 

35 block_type_id=block_document.block_type_id, 

36 name=block_document.name, 

37 ) 

38 ) 

39 if exists: 

40 raise HTTPException( 

41 status.HTTP_409_CONFLICT, 

42 detail="Block already exists", 

43 ) 

44 

45 return await models.block_documents.create_block_document( 1ab

46 session=session, block_document=block_document 

47 ) 

48 

49 

50@router.post("/filter") 1c

51async def read_block_documents( 1c

52 limit: int = dependencies.LimitBody(), 

53 block_documents: Optional[schemas.filters.BlockDocumentFilter] = None, 

54 block_types: Optional[schemas.filters.BlockTypeFilter] = None, 

55 block_schemas: Optional[schemas.filters.BlockSchemaFilter] = None, 

56 include_secrets: bool = Body( 

57 False, description="Whether to include sensitive values in the block document." 

58 ), 

59 sort: Optional[schemas.sorting.BlockDocumentSort] = Body( 

60 schemas.sorting.BlockDocumentSort.NAME_ASC 

61 ), 

62 offset: int = Body(0, ge=0), 

63 db: PrefectDBInterface = Depends(provide_database_interface), 

64) -> List[schemas.core.BlockDocument]: 

65 """ 

66 Query for block documents. 

67 """ 

68 async with db.session_context() as session: 1ab

69 result = await models.block_documents.read_block_documents( 1ab

70 session=session, 

71 block_document_filter=block_documents, 

72 block_type_filter=block_types, 

73 block_schema_filter=block_schemas, 

74 include_secrets=include_secrets, 

75 sort=sort, 

76 offset=offset, 

77 limit=limit, 

78 ) 

79 

80 return result 1ab

81 

82 

83@router.post("/count") 1c

84async def count_block_documents( 1c

85 block_documents: Optional[schemas.filters.BlockDocumentFilter] = None, 

86 block_types: Optional[schemas.filters.BlockTypeFilter] = None, 

87 block_schemas: Optional[schemas.filters.BlockSchemaFilter] = None, 

88 db: PrefectDBInterface = Depends(provide_database_interface), 

89) -> int: 

90 """ 

91 Count block documents. 

92 """ 

93 async with db.session_context() as session: 1ab

94 result = await models.block_documents.count_block_documents( 1ab

95 session=session, 

96 block_document_filter=block_documents, 

97 block_type_filter=block_types, 

98 block_schema_filter=block_schemas, 

99 ) 

100 

101 return result 1ab

102 

103 

104@router.get("/{id:uuid}") 1c

105async def read_block_document_by_id( 1c

106 block_document_id: UUID = Path( 

107 ..., description="The block document id", alias="id" 

108 ), 

109 include_secrets: bool = Query( 

110 False, description="Whether to include sensitive values in the block document." 

111 ), 

112 db: PrefectDBInterface = Depends(provide_database_interface), 

113) -> schemas.core.BlockDocument: 

114 async with db.session_context() as session: 1adefgb

115 block_document = await models.block_documents.read_block_document_by_id( 1adefgb

116 session=session, 

117 block_document_id=block_document_id, 

118 include_secrets=include_secrets, 

119 ) 

120 if not block_document: 120 ↛ 122line 120 didn't jump to line 122 because the condition on line 120 was always true1adefgb

121 raise HTTPException(status.HTTP_404_NOT_FOUND, "Block document not found") 1adefgb

122 return block_document 

123 

124 

125@router.delete("/{id:uuid}", status_code=status.HTTP_204_NO_CONTENT) 1c

126async def delete_block_document( 1c

127 block_document_id: UUID = Path( 

128 ..., description="The block document id", alias="id" 

129 ), 

130 db: PrefectDBInterface = Depends(provide_database_interface), 

131) -> None: 

132 async with db.session_context(begin_transaction=True) as session: 1ab

133 result = await models.block_documents.delete_block_document( 1ab

134 session=session, block_document_id=block_document_id 

135 ) 

136 if not result: 136 ↛ exitline 136 didn't return from function 'delete_block_document' because the condition on line 136 was always true1ab

137 raise HTTPException( 1ab

138 status.HTTP_404_NOT_FOUND, detail="Block document not found" 

139 ) 

140 

141 

142@router.patch("/{id:uuid}", status_code=status.HTTP_204_NO_CONTENT) 1c

143async def update_block_document_data( 1c

144 block_document: schemas.actions.BlockDocumentUpdate, 

145 block_document_id: UUID = Path( 

146 ..., description="The block document id", alias="id" 

147 ), 

148 db: PrefectDBInterface = Depends(provide_database_interface), 

149) -> None: 

150 try: 1ab

151 async with db.session_context(begin_transaction=True) as session: 1ab

152 result = await models.block_documents.update_block_document( 1ab

153 session=session, 

154 block_document_id=block_document_id, 

155 block_document=block_document, 

156 ) 

157 except ValueError as exc: 

158 raise HTTPException( 

159 status_code=status.HTTP_400_BAD_REQUEST, 

160 detail=str(exc), 

161 ) 

162 

163 if not result: 163 ↛ exitline 163 didn't return from function 'update_block_document_data' because the condition on line 163 was always true1ab

164 raise HTTPException( 1ab

165 status.HTTP_404_NOT_FOUND, detail="Block document not found" 

166 )