Skip to content

Commit

Permalink
Removing magic status code numbers from api_connecxion (#24050)
Browse files Browse the repository at this point in the history
  • Loading branch information
bernardocouto authored Jun 2, 2022
1 parent 035553c commit f2e6452
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 15 deletions.
4 changes: 3 additions & 1 deletion airflow/api_connexion/endpoints/config_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion airflow/api_connexion/endpoints/connection_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# under the License.

import os
from http import HTTPStatus

from connexion import NoContent
from flask import request
Expand Down Expand Up @@ -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)])
Expand Down
3 changes: 2 additions & 1 deletion airflow/api_connexion/endpoints/dag_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -176,4 +177,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
3 changes: 2 additions & 1 deletion airflow/api_connexion/endpoints/dag_run_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -62,7 +63,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(
Expand Down
4 changes: 3 additions & 1 deletion airflow/api_connexion/endpoints/dag_source_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
4 changes: 3 additions & 1 deletion airflow/api_connexion/endpoints/pool_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, request
Expand Down Expand Up @@ -41,7 +43,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)])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -104,7 +105,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)])
Expand Down
3 changes: 2 additions & 1 deletion airflow/api_connexion/endpoints/user_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -204,4 +205,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
3 changes: 2 additions & 1 deletion airflow/api_connexion/endpoints/variable_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, request
Expand All @@ -36,7 +37,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)])
Expand Down
13 changes: 7 additions & 6 deletions airflow/api_connexion/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -120,7 +121,7 @@ def __init__(
**kwargs: Any,
):
super().__init__(
status=401,
status=HTTPStatus.UNAUTHORIZED,
type=EXCEPTIONS_LINK_MAP[401],
title=title,
detail=detail,
Expand All @@ -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,
Expand All @@ -160,7 +161,7 @@ def __init__(
**kwargs: Any,
):
super().__init__(
status=409,
status=HTTPStatus.CONFLICT,
type=EXCEPTIONS_LINK_MAP[409],
title=title,
detail=detail,
Expand All @@ -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,
Expand Down

0 comments on commit f2e6452

Please sign in to comment.