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


##########
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:
   The old habit from C. You are right. Agree, property is the right choice 
here, missed that. 
   Actually, on the constructor part, we don't even need that method indeed. 
Removed and moved the assignment to the constructor. I made the 
`input_cli_config_file` method `property` rather than a set method. :) Thanks! 
   Most probably, I wanted to set from multiple places while testing and forgot 
to delete some of them and CLI took precedence. Removing them should also fix 
that ambiguity. 



##########
airflow/cli/commands/remote_commands/auth_command.py:
##########
@@ -0,0 +1,44 @@
+#
+# 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 os
+import sys
+
+import rich
+
+from airflow.cli.api.cli_api_client import Credentials
+from airflow.utils import cli as cli_utils
+
+
+@cli_utils.action_cli
+def login(args) -> None:
+    """Login to a provider."""
+    if not args.api_token and not os.environ.get("APACHE_AIRFLOW_CLI_TOKEN"):
+        # Exit
+        rich.print("[red]No token found.")
+        rich.print(
+            "[green]Please pass: [blue]--api-token or set 
APACHE_AIRFLOW_CLI_TOKEN environment variable to login."

Review Comment:
   I fell into the same questioning but without closing worked for the rest and 
converted the colours when the other started in this case blue. I closed them 
and made them more specific to variable names. It looks better like this



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