HonahX commented on code in PR #2643:
URL: https://github.com/apache/polaris/pull/2643#discussion_r2400430129


##########
client/python/cli/api_client_builder.py:
##########
@@ -0,0 +1,188 @@
+#
+#  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.
+#
+import json
+import os
+from argparse import Namespace
+from functools import cached_property
+from typing import Optional, Dict
+
+from cli.constants import (
+  CONFIG_FILE,
+  CLIENT_PROFILE_ENV,
+  CLIENT_ID_ENV,
+  CLIENT_SECRET_ENV,
+  REALM_ENV,
+  HEADER_ENV,
+  Arguments,
+  DEFAULT_HOSTNAME,
+  DEFAULT_PORT
+)
+from cli.options.option_tree import Argument
+from polaris.management import ApiClient, Configuration
+
+
+def _load_profiles() -> Dict[str, str]:
+    if not os.path.exists(CONFIG_FILE):
+        return {}
+    with open(CONFIG_FILE, "r") as f:
+        return json.load(f)
+
+
+class BuilderConfig:
+    def __init__(self, options: Namespace):
+        self.options = options
+        self.proxy = options.proxy
+        self.access_token = options.access_token
+
+    @cached_property
+    def profile(self) -> Dict[str, str]:
+        profile = {}
+        client_profile = self.options.profile or os.getenv(CLIENT_PROFILE_ENV)
+        if client_profile:
+            profiles = _load_profiles()
+            profile = profiles.get(client_profile)
+            if not profile:
+                raise Exception(f"Polaris profile {client_profile} not found")
+        return profile
+
+    @cached_property
+    def base_url(self) -> str:
+        if self.options.base_url:
+            if self.options.host is not None or self.options.port is not None:
+                raise Exception(
+                    f"Please provide either 
{Argument.to_flag_name(Arguments.BASE_URL)} or"
+                    f" {Argument.to_flag_name(Arguments.HOST)} &"
+                    f" {Argument.to_flag_name(Arguments.PORT)}, but not both"
+                )
+            return self.options.base_url
+
+        host = self.options.host or self.profile.get(Arguments.HOST) or 
DEFAULT_HOSTNAME
+        port = self.options.port or self.profile.get(Arguments.PORT) or 
DEFAULT_PORT
+        return f"http://{host}:{port}";
+
+    @cached_property
+    def management_url(self) -> str:
+        return f"{self.base_url}/api/management/v1"
+
+    @cached_property
+    def catalog_url(self) -> str:
+        return f"{self.base_url}/api/catalog/v1"
+
+    @cached_property
+    def client_id(self) -> Optional[str]:
+        return (
+                self.options.client_id
+                or os.getenv(CLIENT_ID_ENV)
+                or self.profile.get(Arguments.CLIENT_ID)
+        )
+
+    @cached_property
+    def client_secret(self) -> Optional[str]:
+        return (
+                self.options.client_secret
+                or os.getenv(CLIENT_SECRET_ENV)
+                or self.profile.get(Arguments.CLIENT_SECRET)
+        )
+
+    @cached_property
+    def realm(self) -> Optional[str]:
+        realms = (
+                self.options.realm
+                or os.getenv(REALM_ENV)
+                or self.profile.get(Arguments.REALM)
+        )
+        return realms
+
+    @cached_property
+    def header(self) -> Optional[str]:
+        return (
+                self.options.header
+                or os.getenv(HEADER_ENV)
+                or self.profile.get(Arguments.HEADER)
+        )
+
+
+class ApiClientBuilder:
+    def __init__(self, options: Namespace, *, direct_authentication: bool = 
False):
+        self.conf = BuilderConfig(options)
+        self.direct_auth_enabled = direct_authentication
+
+    def _get_token(self):
+        header_params = {"Content-Type": "application/x-www-form-urlencoded"}
+        if self.conf.header and self.conf.realm:
+            header_params[self.conf.header] = self.conf.realm

Review Comment:
   In a follow-up, is it possible to add some tests for the new arguments? 



##########
client/python/generate_clients.py:
##########
@@ -164,7 +164,7 @@ def generate_polaris_catalog_client() -> None:
             "-g",
             "python",
             "-o",
-            CLIENT_DIR,
+            str(CLIENT_DIR),

Review Comment:
   Just curious, did you encounter any issue that need this fix? 



##########
client/python/cli/command/profiles.py:
##########
@@ -104,6 +110,8 @@ def _update_profile(self, name: str) -> None:
             current_client_secret = profiles[name].get("client_secret")
             current_host = profiles[name].get("host")
             current_port = profiles[name].get("port")
+            current_realm = profiles[name].get(Arguments.REALM)

Review Comment:
   +1 on using constants instead of pure string. I think this shall be a 
general practice 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]

Reply via email to