Coverage for /usr/local/lib/python3.12/site-packages/prefect/server/api/block_documents.py: 34%
48 statements
« prev ^ index » next coverage.py v7.10.6, created at 2025-12-05 13:38 +0000
« prev ^ index » next coverage.py v7.10.6, created at 2025-12-05 13:38 +0000
1"""
2Routes for interacting with block objects.
3"""
5from typing import List, Optional 1a
6from uuid import UUID 1a
8from fastapi import Body, Depends, HTTPException, Path, Query, status 1a
10from prefect.server import models, schemas 1a
11from prefect.server.api import dependencies 1a
12from prefect.server.database import PrefectDBInterface, provide_database_interface 1a
13from prefect.server.utilities.server import PrefectRouter 1a
15router: PrefectRouter = PrefectRouter( 1a
16 prefix="/block_documents", tags=["Block documents"]
17)
20@router.post("/", status_code=status.HTTP_201_CREATED) 1a
21async def create_block_document( 1a
22 block_document: schemas.actions.BlockDocumentCreate,
23 db: PrefectDBInterface = Depends(provide_database_interface),
24) -> schemas.core.BlockDocument:
25 """
26 Create a new block document.
28 For more information, see https://docs.prefect.io/v3/concepts/blocks.
29 """
30 async with db.session_context(begin_transaction=True) as session:
31 if block_document.name is not None:
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 )
45 return await models.block_documents.create_block_document(
46 session=session, block_document=block_document
47 )
50@router.post("/filter") 1a
51async def read_block_documents( 1a
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:
69 result = await models.block_documents.read_block_documents(
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 )
80 return result
83@router.post("/count") 1a
84async def count_block_documents( 1a
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:
94 result = await models.block_documents.count_block_documents(
95 session=session,
96 block_document_filter=block_documents,
97 block_type_filter=block_types,
98 block_schema_filter=block_schemas,
99 )
101 return result
104@router.get("/{id:uuid}") 1a
105async def read_block_document_by_id( 1a
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:
115 block_document = await models.block_documents.read_block_document_by_id(
116 session=session,
117 block_document_id=block_document_id,
118 include_secrets=include_secrets,
119 )
120 if not block_document:
121 raise HTTPException(status.HTTP_404_NOT_FOUND, "Block document not found")
122 return block_document
125@router.delete("/{id:uuid}", status_code=status.HTTP_204_NO_CONTENT) 1a
126async def delete_block_document( 1a
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:
133 result = await models.block_documents.delete_block_document(
134 session=session, block_document_id=block_document_id
135 )
136 if not result:
137 raise HTTPException(
138 status.HTTP_404_NOT_FOUND, detail="Block document not found"
139 )
142@router.patch("/{id:uuid}", status_code=status.HTTP_204_NO_CONTENT) 1a
143async def update_block_document_data( 1a
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:
151 async with db.session_context(begin_transaction=True) as session:
152 result = await models.block_documents.update_block_document(
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 )
163 if not result:
164 raise HTTPException(
165 status.HTTP_404_NOT_FOUND, detail="Block document not found"
166 )