You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by GitBox <gi...@apache.org> on 2021/11/29 09:36:31 UTC

[GitHub] [airflow] uranusjr commented on a change in pull request #19851: Allow the use of Stable API in the CLI

uranusjr commented on a change in pull request #19851:
URL: https://github.com/apache/airflow/pull/19851#discussion_r758145187



##########
File path: airflow/api/client/stable_api_client.py
##########
@@ -0,0 +1,114 @@
+# 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 json.decoder import JSONDecodeError

Review comment:
       ```suggestion
   from json import JSONDecodeError
   ```

##########
File path: airflow/api/client/stable_api_client.py
##########
@@ -0,0 +1,114 @@
+# 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 json.decoder import JSONDecodeError
+from typing import Optional
+from urllib.parse import urljoin
+
+from airflow.api.client import api_client
+from airflow.models import DagRun
+from airflow.utils.state import State
+
+
+class AirflowClient(api_client.Client):
+    """API Client using the stable REST API"""
+
+    def _request(self, url, method='GET', json=None):
+        params = {
+            'url': url,
+        }
+        if json is not None:
+            params['json'] = json
+        resp = getattr(self._session, method.lower())(**params)
+        if resp.is_error:
+            # It is justified here because there might be many resp types.
+            try:
+                data = resp.json()
+            except Exception:
+                data = {}
+            raise OSError(data.get('error', 'Server error'))
+        try:  # DELETE requests doesn't return a value
+            return resp.json()
+        except JSONDecodeError:
+            return resp.text

Review comment:
       Instead of trying to handle different methods differently, I’d just have a helper function `parse_response` that returns `str` (and raises on exception), and let the caller methods call `self._session` themselves. You can actually just do `self._session(method, url, ...)` directly (methods like `self._session.get()` are actually just convenience wrappers).

##########
File path: airflow/api/client/stable_api_client.py
##########
@@ -0,0 +1,114 @@
+# 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 json.decoder import JSONDecodeError
+from typing import Optional
+from urllib.parse import urljoin
+
+from airflow.api.client import api_client
+from airflow.models import DagRun
+from airflow.utils.state import State
+
+
+class AirflowClient(api_client.Client):
+    """API Client using the stable REST API"""
+
+    def _request(self, url, method='GET', json=None):
+        params = {
+            'url': url,
+        }
+        if json is not None:
+            params['json'] = json
+        resp = getattr(self._session, method.lower())(**params)
+        if resp.is_error:
+            # It is justified here because there might be many resp types.
+            try:
+                data = resp.json()
+            except Exception:
+                data = {}
+            raise OSError(data.get('error', 'Server error'))
+        try:  # DELETE requests doesn't return a value
+            return resp.json()
+        except JSONDecodeError:
+            return resp.text
+
+    def trigger_dag(
+        self, *, dag_id, run_id=None, conf=None, execution_date=None, logical_date=None, state=State.QUEUED
+    ):
+        endpoint = f'/api/v1/dags/{dag_id}/dagRuns'
+        url = urljoin(self._api_base_url, endpoint)
+        data = self._request(
+            url,
+            method='POST',
+            json={
+                "dag_run_id": run_id,
+                "conf": conf or {},
+                "execution_date": execution_date,
+                "state": state,
+                "logical_date": logical_date,
+            },
+        )

Review comment:
       For example:
   
   ```python
   resp = self._session.request(
       "POST",
       urljoin(self._api_base_url, f"/api/v1/dags/{dag_id}/dagRuns"),
       json={
           "dag_run_id": run_id,
           "conf": conf or {},
           "state": state,
           "logical_date": logical_date,
       },
   )
   return json.loads(self._parse_response(resp))
   ```
   
   (BTW I think you only need `logical_date` here, `execution_date` is deprecated and just an alias to `execution_date`.)




-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org