zikun commented on a change in pull request #9322: URL: https://github.com/apache/airflow/pull/9322#discussion_r442261000
########## File path: airflow/api_connexion/endpoints/dag_source_endpoint.py ########## @@ -14,13 +14,35 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. +import json +import logging -# TODO(mik-laj): We have to implement it. -# Do you want to help? Please look at: https://github.com/apache/airflow/issues/8137 +from flask import Response, current_app, request +from itsdangerous import BadSignature, URLSafeSerializer +from airflow.api_connexion.exceptions import NotFound +from airflow.models.dagcode import DagCode -def get_dag_source(): +log = logging.getLogger(__name__) + + +def get_dag_source(file_token: str): """ Get source code using file token """ - raise NotImplementedError("Not implemented yet.") + secret_key = current_app.config["SECRET_KEY"] + auth_s = URLSafeSerializer(secret_key) + try: + path = auth_s.loads(file_token) + dag_source = DagCode.code(path) + except (BadSignature, FileNotFoundError): + raise NotFound("Dag Source not found") + + from accept_types import get_best_match + return_type = get_best_match(request.headers.get('Accept'), ['text/plain', 'application/json']) + if return_type == 'text/plain': + return Response(dag_source, headers={'Content-Type': return_type}) + if return_type == 'application/json': + content = json.dumps(dict(content=dag_source)) Review comment: Can you create a dag source schema and use the schema to do dumps? You can refer to other PRs for examples - https://github.com/apache/airflow/pulls?q=is%3Apr+is%3Aclosed+label%3Aarea%3AAPI+ ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected]
