pierrejeambrun commented on code in PR #43084: URL: https://github.com/apache/airflow/pull/43084#discussion_r1806387982
########## tests/api_fastapi/core_api/routes/public/test_dag_sources.py: ########## @@ -0,0 +1,103 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +from __future__ import annotations + +import ast +import os + +import pytest +from httpx import Response + +from airflow.models.dag import DAG +from airflow.models.dagbag import DagBag + +from tests_common.test_utils.db import clear_db_dag_code, clear_db_dags, clear_db_serialized_dags + +pytestmark = pytest.mark.db_test + +ROOT_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir)) Review Comment: All of those hardcoded path do not look right. There is most likely constants already defined in the test folder or the global airflow config to not have to manually do all this. ########## airflow/api_fastapi/core_api/routes/public/dag_sources.py: ########## @@ -0,0 +1,72 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +from __future__ import annotations + +from fastapi import Depends, HTTPException, Request, Response +from itsdangerous import BadSignature, URLSafeSerializer +from sqlalchemy.orm import Session +from typing_extensions import Annotated + +from airflow.api_fastapi.common.db.common import get_session +from airflow.api_fastapi.common.router import AirflowRouter +from airflow.api_fastapi.core_api.openapi.exceptions import create_openapi_http_exception_doc +from airflow.api_fastapi.core_api.serializers.dag_sources import DAGSourceModel +from airflow.models.dagcode import DagCode + +dag_sources_router = AirflowRouter(tags=["DagSource"], prefix="/dagSources") +mime_type_text = "text/plain" +mime_type_json = "application/json" +supported_mime_types = [mime_type_text, mime_type_json] + + +def _get_matching_mime_type(request: Request) -> str | None: + """Return the matching MIME type in the request's ACCEPT header.""" + accept_header = request.headers.get("Accept", "").lower() + for mime_type in supported_mime_types: + if mime_type in accept_header: + return mime_type + return None + + +@dag_sources_router.get( + "/{file_token}", + responses=create_openapi_http_exception_doc([400, 401, 403, 404, 422]), +) +async def get_dag_source( + file_token: str, + session: Annotated[Session, Depends(get_session)], + request: Request, +) -> Response: + """Get source code using file token.""" + auth_s = URLSafeSerializer(request.app.state.secret_key) + + try: + path = auth_s.loads(file_token) + dag_source = DagCode.code(path, session=session) + except (BadSignature, FileNotFoundError): + raise HTTPException(404, "DAG source not found") + + return_type = _get_matching_mime_type(request) + + if return_type == mime_type_text: + return Response(dag_source, media_type=return_type) + + if return_type == mime_type_json: + content = DAGSourceModel(content=dag_source).model_dump_json() + return Response(content, media_type=return_type) Review Comment: I'm not sure you need to do that. If I remember correctly fastapi will try to guess the appropriate mimetype for the response based on the query. For instance in the query you can specify the `Accept` header to explicitly request something. -- 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]
