ashb commented on code in PR #45300:
URL: https://github.com/apache/airflow/pull/45300#discussion_r1981836971


##########
airflow/cli/api/client.py:
##########
@@ -0,0 +1,244 @@
+# 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 json
+import os
+import sys
+from typing import TYPE_CHECKING, Any
+
+import httpx
+import keyring
+import rich
+import structlog
+from platformdirs import user_config_path
+from uuid6 import uuid7
+
+from airflow.cli.api.operations import (
+    AssetsOperations,
+    BackfillsOperations,
+    ConfigOperations,
+    ConnectionsOperations,
+    DagOperations,
+    DagRunOperations,
+    JobsOperations,
+    PoolsOperations,
+    ProvidersOperations,
+    ServerResponseError,
+    VariablesOperations,
+    VersionOperations,
+)
+from airflow.utils.types import DagRunType
+from airflow.version import version
+
+if TYPE_CHECKING:
+    # # methodtools doesn't have typestubs, so give a stub
+    def lru_cache(maxsize: int | None = 128):
+        def wrapper(f):
+            return f
+
+        return wrapper
+else:
+    from methodtools import lru_cache
+
+log = structlog.get_logger(logger_name=__name__)
+
+__all__ = [
+    "Client",
+    "Credentials",
+]
+
+
+def add_correlation_id(request: httpx.Request):
+    request.headers["correlation-id"] = str(uuid7())
+
+
+def get_json_error(response: httpx.Response):
+    """Raise a ServerResponseError if we can extract error info from the 
error."""
+    err = ServerResponseError.from_response(response)
+    if err:
+        log.warning("Server error ", extra=dict(err.response.json()))
+        raise err
+
+
+def raise_on_4xx_5xx(response: httpx.Response):
+    return get_json_error(response) or response.raise_for_status()
+
+
+def noop_handler(request: httpx.Request) -> httpx.Response:
+    path = request.url.path
+    log.debug("Dry-run request", method=request.method, path=path)
+    # TODO change for test
+    if path.startswith("/task-instances/") and path.endswith("/run"):
+        # Return a fake context
+        return httpx.Response(
+            200,
+            json={
+                "dag_run": {
+                    "dag_id": "test_dag",
+                    "run_id": "test_run",
+                    "logical_date": "2021-01-01T00:00:00Z",
+                    "start_date": "2021-01-01T00:00:00Z",
+                    "run_type": DagRunType.MANUAL,
+                },
+            },
+        )
+    return httpx.Response(200, json={"text": "Hello, world!"})
+
+
+# Credentials for the API
+class Credentials:
+    """Credentials for the API."""
+
+    api_url: str | None
+    api_token: str | None
+    api_environment: str
+
+    def __init__(
+        self,
+        api_url: str | None = None,
+        api_token: str | None = None,
+        api_environment: str = "production",
+    ):
+        self.api_url = api_url
+        self.api_token = api_token
+        self.set_environment(api_environment)
+
+    @lru_cache()
+    def get_input_cli_config_file(self) -> str:
+        """Generate path and always generate that path but let's not world 
readable."""
+        self.set_environment()
+        return f"{self.api_environment}.json"
+
+    def set_environment(self, api_environment: str = "production"):
+        """Read the environment from the environment variable."""
+        self.api_environment = os.getenv("APACHE_AIRFLOW_CLI_ENVIRONMENT") or 
api_environment

Review Comment:
   Having a `set_x` method on a python class is un-common, it's more common to 
do this as a property setter instead, if it's even needed.
   
   Another option might be to do this in the constructor instead
   
   ```
       self.api_environment = os.getenv("APACHE_AIRFLOW_CLI_ENVIRONMENT") or 
api_environment 
   ```
   
   (Though I wonder why the default is the CLI value, not the value passed to 
the constructor?



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