pierrejeambrun commented on code in PR #44121:
URL: https://github.com/apache/airflow/pull/44121#discussion_r1850350555


##########
airflow/api_fastapi/core_api/routes/public/pools.py:
##########
@@ -165,15 +167,51 @@ def patch_pool(
 @pools_router.post(
     "/",
     status_code=status.HTTP_201_CREATED,
-    responses=create_openapi_http_exception_doc([status.HTTP_401_UNAUTHORIZED, 
status.HTTP_403_FORBIDDEN]),
+    responses=create_openapi_http_exception_doc([status.HTTP_409_CONFLICT]),
 )
 def post_pool(
-    post_body: PoolPostBody,
+    body: PoolPostBody,
     session: Annotated[Session, Depends(get_session)],
 ) -> PoolResponse:
     """Create a Pool."""
-    pool = Pool(**post_body.model_dump())
+    pool = Pool(**body.model_dump())
 
     session.add(pool)
+    try:
+        session.commit()
+    except IntegrityError:
+        session.rollback()
+        raise HTTPException(status.HTTP_409_CONFLICT, f"Pool with name: 
`{body.pool}` already exists")
 
     return PoolResponse.model_validate(pool, from_attributes=True)
+
+
+@pools_router.post(
+    "/bulk",
+    status_code=status.HTTP_201_CREATED,
+    responses=create_openapi_http_exception_doc(
+        [
+            status.HTTP_409_CONFLICT,
+        ]
+    ),
+)
+def post_pools(
+    body: PoolPostBulkBody,
+    session: Annotated[Session, Depends(get_session)],
+) -> PoolCollectionResponse:
+    """Create multiple pools."""
+    pools = [Pool(**body.model_dump()) for body in body.pools]
+    session.add_all(pools)
+    try:
+        session.commit()
+    except IntegrityError as e:
+        session.rollback()
+        raise HTTPException(
+            status.HTTP_409_CONFLICT,
+            detail=f"One or more pools already exists. Error: {e}",
+        )

Review Comment:
   Maybe something like this:
   
https://fastapi.tiangolo.com/tutorial/handling-errors/#install-custom-exception-handlers
   
   And we check that in the error there is `Unique` and then we return 409 
response ?
   
   So we can just remove all the `try / catch / etc...` specific code, and the 
double call to `commit()`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to