Copilot commented on code in PR #44311: URL: https://github.com/apache/airflow/pull/44311#discussion_r1855236515
########## providers/src/airflow/providers/edge/worker_api/routes/_v2_routes.py: ########## @@ -0,0 +1,95 @@ +# 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. +"""Compatibility layer for Connexion API to Airflow v2.10 API routes.""" + +from __future__ import annotations + +import json +from typing import TYPE_CHECKING, Any + +from airflow.providers.edge.worker_api.auth import jwt_token_authorization, jwt_token_authorization_rpc +from airflow.providers.edge.worker_api.datamodels import JsonRpcRequest, WorkerStateBody +from airflow.providers.edge.worker_api.routes._v2_compat import HTTPException +from airflow.providers.edge.worker_api.routes.rpc_api import json_request_headers, json_rpc_version, rpcapi +from airflow.providers.edge.worker_api.routes.worker import register, set_state +from airflow.utils.session import NEW_SESSION, provide_session + +if TYPE_CHECKING: + from airflow.api_connexion.types import APIResponse + + +def rpcapi_v2(body: dict[str, Any]) -> APIResponse: + """Handle Edge Worker API `/edge_worker/v1/rpcapi` endpoint for Airflow 2.10.""" + # Note: Except the method map this _was_ a 100% copy of internal API module + # airflow.api_internal.endpoints.rpc_api_endpoint.internal_airflow_api() + # As of rework for FastAPI in Airflow 3.0, this is updated and to be removed in future. + from flask import Response, request + + try: + json_request_headers( + content_type=request.headers.get("Content-Type", ""), accept=request.headers.get("Accept", "") + ) + auth = request.headers.get("Authorization", "") + json_rpc = body.get("jsonrpc", "") + method_name = body.get("method", "") + request_obj = JsonRpcRequest(method=method_name, jsonrpc=json_rpc, params=body.get("params")) + jwt_token_authorization_rpc(request_obj, auth) + json_rpc_version(request_obj) + output_json = rpcapi(request_obj) + response = json.dumps(output_json) if output_json is not None else None + return Response(response=response, headers={"Content-Type": "application/json"}) + except HTTPException as e: + return e.to_response() # type: ignore[attr-defined] + + +@provide_session +def register_v2(worker_name: str, body: dict[str, Any], session=NEW_SESSION) -> Any: + """Handle Edge Worker API `/edge_worker/v1/worker/{worker_name}` endpoint for Airflow 2.10.""" + from flask import request + + try: + auth = request.headers.get("Authorization", "") + jwt_token_authorization(request.path, auth) + + state = body.get("state", "") Review Comment: The default value for 'state' should not be an empty string. It should be a valid 'EdgeWorkerState' or 'None'. ```suggestion state = body.get("state", None) ``` ########## providers/src/airflow/providers/edge/worker_api/routes/_v2_routes.py: ########## @@ -0,0 +1,95 @@ +# 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. +"""Compatibility layer for Connexion API to Airflow v2.10 API routes.""" + +from __future__ import annotations + +import json +from typing import TYPE_CHECKING, Any + +from airflow.providers.edge.worker_api.auth import jwt_token_authorization, jwt_token_authorization_rpc +from airflow.providers.edge.worker_api.datamodels import JsonRpcRequest, WorkerStateBody +from airflow.providers.edge.worker_api.routes._v2_compat import HTTPException +from airflow.providers.edge.worker_api.routes.rpc_api import json_request_headers, json_rpc_version, rpcapi +from airflow.providers.edge.worker_api.routes.worker import register, set_state +from airflow.utils.session import NEW_SESSION, provide_session + +if TYPE_CHECKING: + from airflow.api_connexion.types import APIResponse + + +def rpcapi_v2(body: dict[str, Any]) -> APIResponse: + """Handle Edge Worker API `/edge_worker/v1/rpcapi` endpoint for Airflow 2.10.""" + # Note: Except the method map this _was_ a 100% copy of internal API module + # airflow.api_internal.endpoints.rpc_api_endpoint.internal_airflow_api() + # As of rework for FastAPI in Airflow 3.0, this is updated and to be removed in future. + from flask import Response, request + + try: + json_request_headers( + content_type=request.headers.get("Content-Type", ""), accept=request.headers.get("Accept", "") + ) + auth = request.headers.get("Authorization", "") + json_rpc = body.get("jsonrpc", "") + method_name = body.get("method", "") + request_obj = JsonRpcRequest(method=method_name, jsonrpc=json_rpc, params=body.get("params")) + jwt_token_authorization_rpc(request_obj, auth) + json_rpc_version(request_obj) + output_json = rpcapi(request_obj) + response = json.dumps(output_json) if output_json is not None else None + return Response(response=response, headers={"Content-Type": "application/json"}) + except HTTPException as e: + return e.to_response() # type: ignore[attr-defined] + + +@provide_session +def register_v2(worker_name: str, body: dict[str, Any], session=NEW_SESSION) -> Any: + """Handle Edge Worker API `/edge_worker/v1/worker/{worker_name}` endpoint for Airflow 2.10.""" + from flask import request + + try: + auth = request.headers.get("Authorization", "") + jwt_token_authorization(request.path, auth) + + state = body.get("state", "") + queues = body.get("queues", "") + sysinfo = body.get("sysinfo", "") + request_obj = WorkerStateBody(state=state, jobs_active=0, queues=queues, sysinfo=sysinfo) + + return register(worker_name, request_obj, session) + except HTTPException as e: + return e.to_response() # type: ignore[attr-defined] + + +@provide_session +def set_state_v2(worker_name: str, body: dict[str, Any], session=NEW_SESSION) -> Any: + """Handle Edge Worker API `/edge_worker/v1/worker/{worker_name}` endpoint for Airflow 2.10.""" + from flask import request + + try: + auth = request.headers.get("Authorization", "") + jwt_token_authorization(request.path, auth) + + state = body.get("state", "") + jobs_active = int(body.get("jobs_active", "")) Review Comment: The default value for 'jobs_active' should not be an empty string. It should be a valid integer or '0'. ```suggestion jobs_active = int(body.get("jobs_active", 0)) ``` ########## providers/src/airflow/providers/edge/worker_api/routes/_v2_routes.py: ########## @@ -0,0 +1,95 @@ +# 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. +"""Compatibility layer for Connexion API to Airflow v2.10 API routes.""" + +from __future__ import annotations + +import json +from typing import TYPE_CHECKING, Any + +from airflow.providers.edge.worker_api.auth import jwt_token_authorization, jwt_token_authorization_rpc +from airflow.providers.edge.worker_api.datamodels import JsonRpcRequest, WorkerStateBody +from airflow.providers.edge.worker_api.routes._v2_compat import HTTPException +from airflow.providers.edge.worker_api.routes.rpc_api import json_request_headers, json_rpc_version, rpcapi +from airflow.providers.edge.worker_api.routes.worker import register, set_state +from airflow.utils.session import NEW_SESSION, provide_session + +if TYPE_CHECKING: + from airflow.api_connexion.types import APIResponse + + +def rpcapi_v2(body: dict[str, Any]) -> APIResponse: + """Handle Edge Worker API `/edge_worker/v1/rpcapi` endpoint for Airflow 2.10.""" + # Note: Except the method map this _was_ a 100% copy of internal API module + # airflow.api_internal.endpoints.rpc_api_endpoint.internal_airflow_api() + # As of rework for FastAPI in Airflow 3.0, this is updated and to be removed in future. Review Comment: The phrase 'to be removed in future' should be 'to be removed in the future'. ```suggestion # As of rework for FastAPI in Airflow 3.0, this is updated and to be removed in the future. ``` -- 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]
