Copilot commented on code in PR #45472:
URL: https://github.com/apache/airflow/pull/45472#discussion_r1907974764
##########
providers/src/airflow/providers/fab/www/extensions/init_views.py:
##########
@@ -18,17 +18,47 @@
import logging
from functools import cached_property
+from pathlib import Path
from typing import TYPE_CHECKING
-from connexion import Resolver
+from connexion import FlaskApi, Resolver
from connexion.decorators.validation import RequestBodyValidator
-from connexion.exceptions import BadRequestProblem
+from connexion.exceptions import BadRequestProblem, ProblemException
+from flask import request
+
+from airflow.api_connexion.exceptions import common_error_handler
+from airflow.api_fastapi.app import get_auth_manager
+from airflow.configuration import conf
+from airflow.providers.fab.www.constants import SWAGGER_BUNDLE, SWAGGER_ENABLED
+from airflow.utils.yaml import safe_load
if TYPE_CHECKING:
from flask import Flask
log = logging.getLogger(__name__)
+# providers/src/airflow/providers/fab/www/extensions/init_views.py => airflow/
+ROOT_APP_DIR = Path(__file__).parents[7].joinpath("airflow").resolve()
+
+
+def set_cors_headers_on_response(response):
+ """Add response headers."""
+ allow_headers = conf.get("api", "access_control_allow_headers")
+ allow_methods = conf.get("api", "access_control_allow_methods")
+ allow_origins = conf.get("api", "access_control_allow_origins")
+ if allow_headers:
+ response.headers["Access-Control-Allow-Headers"] = allow_headers
+ if allow_methods:
+ response.headers["Access-Control-Allow-Methods"] = allow_methods
+ if allow_origins == "*":
+ response.headers["Access-Control-Allow-Origin"] = "*"
+ elif allow_origins:
+ allowed_origins = allow_origins.split(" ")
Review Comment:
The allow_origins variable should be split by commas instead of spaces to
handle multiple origins more accurately.
```suggestion
allowed_origins = allow_origins.split(",")
```
##########
providers/src/airflow/providers/fab/www/extensions/init_views.py:
##########
@@ -78,6 +108,59 @@ def validate_schema(self, data, url):
return super().validate_schema(data, url)
+base_paths: list[str] = [] # contains the list of base paths that have api
endpoints
+
+
+def init_api_error_handlers(app: Flask) -> None:
+ """Add error handlers for 404 and 405 errors for existing API paths."""
+
+ @app.errorhandler(404)
+ def _handle_api_not_found(ex):
+ if any([request.path.startswith(p) for p in base_paths]):
+ # 404 errors are never handled on the blueprint level
+ # unless raised from a view func so actual 404 errors,
+ # i.e. "no route for it" defined, need to be handled
+ # here on the application level
+ return common_error_handler(ex)
+ else:
+ from airflow.providers.fab.www.views import not_found
Review Comment:
The not_found function should be imported at the top of the file for clarity
and to avoid potential issues with circular imports.
```suggestion
return not_found(ex)
```
--
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]