Skip to content

Commit

Permalink
Migrate magic numbers for status codes to named constants (#44176)
Browse files Browse the repository at this point in the history
  • Loading branch information
amoghrajesh authored Nov 19, 2024
1 parent 8daf74e commit 5f7f95b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
18 changes: 12 additions & 6 deletions airflow/api_fastapi/core_api/routes/public/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@

@connections_router.delete(
"/{connection_id}",
status_code=204,
status_code=status.HTTP_204_NO_CONTENT,
responses=create_openapi_http_exception_doc([status.HTTP_404_NOT_FOUND]),
)
def delete_connection(
Expand Down Expand Up @@ -120,7 +120,7 @@ def get_connections(

@connections_router.post(
"/",
status_code=201,
status_code=status.HTTP_201_CREATED,
responses=create_openapi_http_exception_doc([status.HTTP_409_CONFLICT]),
)
def post_connection(
Expand All @@ -131,11 +131,14 @@ def post_connection(
try:
helpers.validate_key(post_body.connection_id, max_length=200)
except Exception as e:
raise HTTPException(400, f"{e}")
raise HTTPException(status.HTTP_400_BAD_REQUEST, f"{e}")

connection = session.scalar(select(Connection).filter_by(conn_id=post_body.connection_id))
if connection is not None:
raise HTTPException(409, f"Connection with connection_id: `{post_body.connection_id}` already exists")
raise HTTPException(
status.HTTP_409_CONFLICT,
f"Connection with connection_id: `{post_body.connection_id}` already exists",
)

connection = Connection(**post_body.model_dump(by_alias=True))
session.add(connection)
Expand All @@ -160,7 +163,10 @@ def patch_connection(
) -> ConnectionResponse:
"""Update a connection entry."""
if patch_body.connection_id != connection_id:
raise HTTPException(400, "The connection_id in the request body does not match the URL parameter")
raise HTTPException(
status.HTTP_400_BAD_REQUEST,
"The connection_id in the request body does not match the URL parameter",
)

non_update_fields = {"connection_id", "conn_id"}
connection = session.scalar(select(Connection).filter_by(conn_id=connection_id).limit(1))
Expand Down Expand Up @@ -195,7 +201,7 @@ def test_connection(
"""
if conf.get("core", "test_connection", fallback="Disabled").lower().strip() != "enabled":
raise HTTPException(
403,
status.HTTP_403_FORBIDDEN,
"Testing connections is disabled in Airflow configuration. "
"Contact your deployment admin to enable it.",
)
Expand Down
2 changes: 1 addition & 1 deletion airflow/api_fastapi/core_api/routes/public/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

@variables_router.delete(
"/{variable_key}",
status_code=204,
status_code=status.HTTP_204_NO_CONTENT,
responses=create_openapi_http_exception_doc([status.HTTP_404_NOT_FOUND]),
)
def delete_variable(
Expand Down

0 comments on commit 5f7f95b

Please sign in to comment.