From 93d929daad9c9639484c357ba29be734a0e876dc Mon Sep 17 00:00:00 2001 From: Bernardo Couto <35502483+bernardocouto@users.noreply.github.com> Date: Thu, 2 Jun 2022 14:52:19 -0300 Subject: [PATCH] Removing magic status code numbers from api_connecxion (#24050) (cherry picked from commit f2e6452efdf74828de9f73a8bf4f42f6dd10eb58) --- airflow/api_connexion/endpoints/config_endpoint.py | 4 +++- .../api_connexion/endpoints/connection_endpoint.py | 3 ++- airflow/api_connexion/endpoints/dag_endpoint.py | 3 ++- airflow/api_connexion/endpoints/dag_run_endpoint.py | 3 ++- .../api_connexion/endpoints/dag_source_endpoint.py | 4 +++- airflow/api_connexion/endpoints/pool_endpoint.py | 4 +++- .../endpoints/role_and_permission_endpoint.py | 3 ++- airflow/api_connexion/endpoints/user_endpoint.py | 3 ++- .../api_connexion/endpoints/variable_endpoint.py | 3 ++- airflow/api_connexion/exceptions.py | 13 +++++++------ 10 files changed, 28 insertions(+), 15 deletions(-) diff --git a/airflow/api_connexion/endpoints/config_endpoint.py b/airflow/api_connexion/endpoints/config_endpoint.py index 9514621447609..bdd2b3a959547 100644 --- a/airflow/api_connexion/endpoints/config_endpoint.py +++ b/airflow/api_connexion/endpoints/config_endpoint.py @@ -15,6 +15,8 @@ # specific language governing permissions and limitations # under the License. +from http import HTTPStatus + from flask import Response, request from airflow.api_connexion import security @@ -72,7 +74,7 @@ def get_config() -> Response: } return_type = request.accept_mimetypes.best_match(serializer.keys()) if return_type not in serializer: - return Response(status=406) + return Response(status=HTTPStatus.NOT_ACCEPTABLE) elif conf.getboolean("webserver", "expose_config"): conf_dict = conf.as_dict(display_source=False, display_sensitive=True) config = _conf_dict_to_config(conf_dict) diff --git a/airflow/api_connexion/endpoints/connection_endpoint.py b/airflow/api_connexion/endpoints/connection_endpoint.py index f9be9c227e3f3..b196b3236b911 100644 --- a/airflow/api_connexion/endpoints/connection_endpoint.py +++ b/airflow/api_connexion/endpoints/connection_endpoint.py @@ -16,6 +16,7 @@ # under the License. import os +from http import HTTPStatus from connexion import NoContent from flask import request @@ -51,7 +52,7 @@ def delete_connection(*, connection_id: str, session: Session = NEW_SESSION) -> detail=f"The Connection with connection_id: `{connection_id}` was not found", ) session.delete(connection) - return NoContent, 204 + return NoContent, HTTPStatus.NO_CONTENT @security.requires_access([(permissions.ACTION_CAN_READ, permissions.RESOURCE_CONNECTION)]) diff --git a/airflow/api_connexion/endpoints/dag_endpoint.py b/airflow/api_connexion/endpoints/dag_endpoint.py index 40113021cfad6..7940a25c8f9fb 100644 --- a/airflow/api_connexion/endpoints/dag_endpoint.py +++ b/airflow/api_connexion/endpoints/dag_endpoint.py @@ -15,6 +15,7 @@ # specific language governing permissions and limitations # under the License. +from http import HTTPStatus from typing import Collection, Optional from connexion import NoContent @@ -177,4 +178,4 @@ def delete_dag(dag_id: str, session: Session = NEW_SESSION) -> APIResponse: except AirflowException: raise AlreadyExists(detail=f"Task instances of dag with id: '{dag_id}' are still running") - return NoContent, 204 + return NoContent, HTTPStatus.NO_CONTENT diff --git a/airflow/api_connexion/endpoints/dag_run_endpoint.py b/airflow/api_connexion/endpoints/dag_run_endpoint.py index 1fad48f7b6fe7..351b723c4a283 100644 --- a/airflow/api_connexion/endpoints/dag_run_endpoint.py +++ b/airflow/api_connexion/endpoints/dag_run_endpoint.py @@ -14,6 +14,7 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. +from http import HTTPStatus from typing import List, Optional, Tuple import pendulum @@ -55,7 +56,7 @@ def delete_dag_run(*, dag_id: str, dag_run_id: str, session: Session = NEW_SESSI """Delete a DAG Run""" if session.query(DagRun).filter(DagRun.dag_id == dag_id, DagRun.run_id == dag_run_id).delete() == 0: raise NotFound(detail=f"DAGRun with DAG ID: '{dag_id}' and DagRun ID: '{dag_run_id}' not found") - return NoContent, 204 + return NoContent, HTTPStatus.NO_CONTENT @security.requires_access( diff --git a/airflow/api_connexion/endpoints/dag_source_endpoint.py b/airflow/api_connexion/endpoints/dag_source_endpoint.py index 74c3496a2c208..ad6209221e523 100644 --- a/airflow/api_connexion/endpoints/dag_source_endpoint.py +++ b/airflow/api_connexion/endpoints/dag_source_endpoint.py @@ -15,6 +15,8 @@ # specific language governing permissions and limitations # under the License. +from http import HTTPStatus + from flask import Response, current_app, request from itsdangerous import BadSignature, URLSafeSerializer @@ -42,4 +44,4 @@ def get_dag_source(*, file_token: str) -> Response: if return_type == 'application/json': content = dag_source_schema.dumps(dict(content=dag_source)) return Response(content, headers={'Content-Type': return_type}) - return Response("Not Allowed Accept Header", status=406) + return Response("Not Allowed Accept Header", status=HTTPStatus.NOT_ACCEPTABLE) diff --git a/airflow/api_connexion/endpoints/pool_endpoint.py b/airflow/api_connexion/endpoints/pool_endpoint.py index 8c3d3f3b86d38..594afeb49bc1a 100644 --- a/airflow/api_connexion/endpoints/pool_endpoint.py +++ b/airflow/api_connexion/endpoints/pool_endpoint.py @@ -14,6 +14,8 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. + +from http import HTTPStatus from typing import Optional from flask import Response @@ -42,7 +44,7 @@ def delete_pool(*, pool_name: str, session: Session = NEW_SESSION) -> APIRespons affected_count = session.query(Pool).filter(Pool.pool == pool_name).delete() if affected_count == 0: raise NotFound(detail=f"Pool with name:'{pool_name}' not found") - return Response(status=204) + return Response(status=HTTPStatus.NO_CONTENT) @security.requires_access([(permissions.ACTION_CAN_READ, permissions.RESOURCE_POOL)]) diff --git a/airflow/api_connexion/endpoints/role_and_permission_endpoint.py b/airflow/api_connexion/endpoints/role_and_permission_endpoint.py index 25419066d20fa..1b25769af7737 100644 --- a/airflow/api_connexion/endpoints/role_and_permission_endpoint.py +++ b/airflow/api_connexion/endpoints/role_and_permission_endpoint.py @@ -15,6 +15,7 @@ # specific language governing permissions and limitations # under the License. +from http import HTTPStatus from typing import List, Optional, Tuple from connexion import NoContent @@ -105,7 +106,7 @@ def delete_role(*, role_name: str) -> APIResponse: if not role: raise NotFound(title="Role not found", detail=f"Role with name {role_name!r} was not found") ab_security_manager.delete_role(role_name=role_name) - return NoContent, 204 + return NoContent, HTTPStatus.NO_CONTENT @security.requires_access([(permissions.ACTION_CAN_EDIT, permissions.RESOURCE_ROLE)]) diff --git a/airflow/api_connexion/endpoints/user_endpoint.py b/airflow/api_connexion/endpoints/user_endpoint.py index 2ed0db2aae864..3ab476e219cb9 100644 --- a/airflow/api_connexion/endpoints/user_endpoint.py +++ b/airflow/api_connexion/endpoints/user_endpoint.py @@ -14,6 +14,7 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. +from http import HTTPStatus from typing import List, Optional from connexion import NoContent @@ -205,4 +206,4 @@ def delete_user(*, username: str) -> APIResponse: security_manager.get_session.delete(user) security_manager.get_session.commit() - return NoContent, 204 + return NoContent, HTTPStatus.NO_CONTENT diff --git a/airflow/api_connexion/endpoints/variable_endpoint.py b/airflow/api_connexion/endpoints/variable_endpoint.py index 4dfc0803c5c62..8f039b6fdbe69 100644 --- a/airflow/api_connexion/endpoints/variable_endpoint.py +++ b/airflow/api_connexion/endpoints/variable_endpoint.py @@ -14,6 +14,7 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. +from http import HTTPStatus from typing import Optional from flask import Response @@ -37,7 +38,7 @@ def delete_variable(*, variable_key: str) -> Response: """Delete variable""" if Variable.delete(variable_key) == 0: raise NotFound("Variable not found") - return Response(status=204) + return Response(status=HTTPStatus.NO_CONTENT) @security.requires_access([(permissions.ACTION_CAN_READ, permissions.RESOURCE_VARIABLE)]) diff --git a/airflow/api_connexion/exceptions.py b/airflow/api_connexion/exceptions.py index 0c6c4fa0d3a8f..8fb7f2e78883b 100644 --- a/airflow/api_connexion/exceptions.py +++ b/airflow/api_connexion/exceptions.py @@ -14,6 +14,7 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. +from http import HTTPStatus from typing import Any, Dict, Optional import flask @@ -80,7 +81,7 @@ def __init__( **kwargs: Any, ) -> None: super().__init__( - status=404, + status=HTTPStatus.NOT_FOUND, type=EXCEPTIONS_LINK_MAP[404], title=title, detail=detail, @@ -100,7 +101,7 @@ def __init__( **kwargs: Any, ) -> None: super().__init__( - status=400, + status=HTTPStatus.BAD_REQUEST, type=EXCEPTIONS_LINK_MAP[400], title=title, detail=detail, @@ -120,7 +121,7 @@ def __init__( **kwargs: Any, ): super().__init__( - status=401, + status=HTTPStatus.UNAUTHORIZED, type=EXCEPTIONS_LINK_MAP[401], title=title, detail=detail, @@ -140,7 +141,7 @@ def __init__( **kwargs: Any, ) -> None: super().__init__( - status=403, + status=HTTPStatus.FORBIDDEN, type=EXCEPTIONS_LINK_MAP[403], title=title, detail=detail, @@ -160,7 +161,7 @@ def __init__( **kwargs: Any, ): super().__init__( - status=409, + status=HTTPStatus.CONFLICT, type=EXCEPTIONS_LINK_MAP[409], title=title, detail=detail, @@ -180,7 +181,7 @@ def __init__( **kwargs: Any, ) -> None: super().__init__( - status=500, + status=HTTPStatus.INTERNAL_SERVER_ERROR, type=EXCEPTIONS_LINK_MAP[500], title=title, detail=detail,