pierrejeambrun commented on code in PR #43084:
URL: https://github.com/apache/airflow/pull/43084#discussion_r1816997924


##########
airflow/api_fastapi/core_api/routes/public/dag_sources.py:
##########
@@ -0,0 +1,68 @@
+# 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, Header, 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 
DAGSourceResponse
+from airflow.models.dagcode import DagCode
+
+dag_sources_router = AirflowRouter(tags=["DagSource"], prefix="/dagSources")
+
+mime_type_text = "text/plain"
+mime_type_json = "application/json"
+mime_type_any = "*/*"

Review Comment:
   Handle the `any` mime_type as well, in case any is specified, or no `accept` 
header is specified.



##########
airflow/api_fastapi/core_api/routes/public/dag_sources.py:
##########
@@ -0,0 +1,68 @@
+# 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, Header, 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 
DAGSourceResponse
+from airflow.models.dagcode import DagCode
+
+dag_sources_router = AirflowRouter(tags=["DagSource"], prefix="/dagSources")
+
+mime_type_text = "text/plain"
+mime_type_json = "application/json"
+mime_type_any = "*/*"
+
+
+@dag_sources_router.get(
+    "/{file_token}",
+    responses={
+        **create_openapi_http_exception_doc([400, 401, 403, 404, 406]),
+        "200": {
+            "description": "Successful Response",
+            "content": {"plain/text": {"schema": {"type": "string", "example": 
"dag code"}}},
+        },
+    },

Review Comment:
   There is no way for FastAPI to know that the endpoint can also return plain 
text for a specific content type. Add a custom doc response. I think I need to 
do something for the json one as well. Doc doesn't look right.
   ![Screenshot 2024-10-25 at 18 18 
57](https://github.com/user-attachments/assets/e867df19-7e2c-41b1-a3fc-c2737865882a)
   
   



##########
airflow/api_fastapi/core_api/routes/public/dag_sources.py:
##########
@@ -0,0 +1,68 @@
+# 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, Header, 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 
DAGSourceResponse
+from airflow.models.dagcode import DagCode
+
+dag_sources_router = AirflowRouter(tags=["DagSource"], prefix="/dagSources")
+
+mime_type_text = "text/plain"
+mime_type_json = "application/json"
+mime_type_any = "*/*"
+
+
+@dag_sources_router.get(
+    "/{file_token}",
+    responses={
+        **create_openapi_http_exception_doc([400, 401, 403, 404, 406]),
+        "200": {
+            "description": "Successful Response",
+            "content": {"plain/text": {"schema": {"type": "string", "example": 
"dag code"}}},
+        },
+    },
+)
+async def get_dag_source(
+    file_token: str,
+    session: Annotated[Session, Depends(get_session)],
+    request: Request,
+    accept: Annotated[str, Header()] = mime_type_any,
+):
+    """Get source code using file token."""
+    auth_s = URLSafeSerializer(request.app.state.secret_key)
+
+    try:
+        path = auth_s.loads(file_token)
+        dag_source_model = DAGSourceResponse(

Review Comment:
   Rename Dag Serializer. `Model` is already used for ORM/SQLAlchemy entities. 
We should try to avoid to name things models for serializers. We can use 
`serializers` or just `Response/Body` as most of the time the serializers are 
specific to a response or body context.



##########
airflow/api_connexion/endpoints/dag_source_endpoint.py:
##########
@@ -37,6 +38,7 @@
     from airflow.auth.managers.models.batch_apis import IsAuthorizedDagRequest
 
 
+@mark_fastapi_migration_done

Review Comment:
   Added the decorator



##########
airflow/api_fastapi/core_api/routes/public/dag_sources.py:
##########
@@ -0,0 +1,68 @@
+# 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, Header, 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 
DAGSourceResponse
+from airflow.models.dagcode import DagCode
+
+dag_sources_router = AirflowRouter(tags=["DagSource"], prefix="/dagSources")
+
+mime_type_text = "text/plain"
+mime_type_json = "application/json"
+mime_type_any = "*/*"
+
+
+@dag_sources_router.get(
+    "/{file_token}",
+    responses={
+        **create_openapi_http_exception_doc([400, 401, 403, 404, 406]),

Review Comment:
   Remove `422`, it is automatically handled by FastAPI and documented.
   
   Added `406` to the documentatation.



-- 
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]

Reply via email to