From 28a05a21f1180632633c4c17825e6f7c4b5d6b99 Mon Sep 17 00:00:00 2001 From: Romazes Date: Wed, 14 Aug 2024 19:26:24 +0300 Subject: [PATCH 1/3] feat: add login process in init command --- lean/commands/init.py | 7 +++++ lean/commands/login.py | 59 ++++++++++++++++++++++++++++++++---------- 2 files changed, 53 insertions(+), 13 deletions(-) diff --git a/lean/commands/init.py b/lean/commands/init.py index 45a7ade8..8b86ee34 100644 --- a/lean/commands/init.py +++ b/lean/commands/init.py @@ -17,6 +17,7 @@ from click import command, option, Choice, confirm, prompt from lean.click import LeanCommand +from lean.commands.login import get_credentials, validate_credentials, get_lean_config_credentials from lean.constants import DEFAULT_DATA_DIRECTORY_NAME, DEFAULT_LEAN_CONFIG_FILE_NAME from lean.container import container from lean.models.errors import MoreInfoError @@ -128,6 +129,12 @@ def init(organization: Optional[str], language: Optional[str]) -> None: from shutil import copytree from zipfile import ZipFile + # Retrieve current credentials from the Lean CLI configuration + # If credentials are not available, prompt the user to provide them + current_user_id, current_api_token = get_lean_config_credentials() + user_id, api_token = get_credentials(current_user_id, current_api_token, False) + validate_credentials(user_id, api_token) + # Select and set organization if organization is not None: diff --git a/lean/commands/login.py b/lean/commands/login.py index 834318bb..4c609394 100644 --- a/lean/commands/login.py +++ b/lean/commands/login.py @@ -21,19 +21,29 @@ from lean.components.api.api_client import APIClient -@command(cls=LeanCommand) -@option("--user-id", "-u", type=str, help="QuantConnect user id") -@option("--api-token", "-t", type=str, help="QuantConnect API token") -@option("--show-secrets", is_flag=True, show_default=True, default=False, help="Show secrets as they are input") -def login(user_id: Optional[str], api_token: Optional[str], show_secrets: bool) -> None: - """Log in with a QuantConnect account. +def get_lean_config_credentials() -> tuple[str, str]: + """Retrieve the QuantConnect credentials from the Lean CLI configuration. - If user id or API token is not provided an interactive prompt will show. + This function accesses the Lean CLI configuration manager to obtain the + stored user ID and API token. The credentials are retrieved from the + configuration settings managed by the Lean CLI. - Credentials are stored in ~/.lean/credentials and are removed upon running `lean logout`. + Returns: + tuple[str, str]: A tuple containing the user ID and API token as strings. """ + cli_config_manager = container.cli_config_manager + + user_id = cli_config_manager.user_id.get_value() + api_token = cli_config_manager.api_token.get_value() + + return user_id, api_token + + +def get_credentials(user_id: Optional[str], api_token: Optional[str], show_secrets: bool) -> tuple[str, str]: + """Fetch user credentials, prompting the user if necessary.""" logger = container.logger credentials_storage = container.credentials_storage + current_user_id, current_api_token = get_lean_config_credentials() if user_id is None or api_token is None: logger.info("Your user id and API token are needed to make authenticated requests to the QuantConnect API") @@ -41,18 +51,41 @@ def login(user_id: Optional[str], api_token: Optional[str], show_secrets: bool) logger.info(f"Both will be saved in {credentials_storage.file}") if user_id is None: - user_id = prompt("User id") + user_id = prompt("User id", current_user_id) if api_token is None: - api_token = logger.prompt_password("API token", hide_input=not show_secrets) + api_token = logger.prompt_password("API token", current_api_token, hide_input=not show_secrets) + + return user_id, api_token + +def validate_credentials(user_id: str, api_token: str) -> None: + """Validate the user credentials by attempting to authenticate with the QuantConnect API.""" container.api_client.set_user_token(user_id=user_id, api_token=api_token) + if not container.api_client.is_authenticated(): - raise MoreInfoError("Credentials are invalid. Please ensure your computer clock is correct, or try using another terminal, or enter API token manually instead of copy-pasting.", - "https://www.lean.io/docs/v2/lean-cli") + raise MoreInfoError( + "Credentials are invalid. Please ensure your computer clock is correct, or try using another terminal, or enter API token manually instead of copy-pasting.", + "https://www.lean.io/docs/v2/lean-cli" + ) cli_config_manager = container.cli_config_manager cli_config_manager.user_id.set_value(user_id) cli_config_manager.api_token.set_value(api_token) - logger.info("Successfully logged in") + container.logger.info("Successfully logged in") + +@command(cls=LeanCommand) +@option("--user-id", "-u", type=str, help="QuantConnect user id") +@option("--api-token", "-t", type=str, help="QuantConnect API token") +@option("--show-secrets", is_flag=True, show_default=True, default=False, help="Show secrets as they are input") +def login(user_id: Optional[str], api_token: Optional[str], show_secrets: bool) -> None: + """Log in with a QuantConnect account. + + If user id or API token is not provided an interactive prompt will show. + + Credentials are stored in ~/.lean/credentials and are removed upon running `lean logout`. + """ + + user_id, api_token = get_credentials(user_id, api_token, show_secrets) + validate_credentials(user_id, api_token) From cad76ee6765c593a2bb271f2005a83fdf69f3a82 Mon Sep 17 00:00:00 2001 From: Romazes Date: Wed, 14 Aug 2024 19:49:51 +0300 Subject: [PATCH 2/3] fix: Import Tuple from the typing module to ensure type hinting is compatible across different Python versions. --- lean/commands/init.py | 1 - lean/commands/login.py | 7 +++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/lean/commands/init.py b/lean/commands/init.py index 8b86ee34..89dd4a76 100644 --- a/lean/commands/init.py +++ b/lean/commands/init.py @@ -136,7 +136,6 @@ def init(organization: Optional[str], language: Optional[str]) -> None: validate_credentials(user_id, api_token) # Select and set organization - if organization is not None: organization_id, organization_name = _get_organization_id(organization) else: diff --git a/lean/commands/login.py b/lean/commands/login.py index 4c609394..f46d24a3 100644 --- a/lean/commands/login.py +++ b/lean/commands/login.py @@ -11,17 +11,16 @@ # See the License for the specific language governing permissions and # limitations under the License. -from typing import Optional +from typing import Optional, Tuple from click import command, option, prompt from lean.click import LeanCommand from lean.container import container from lean.models.errors import MoreInfoError -from lean.components.api.api_client import APIClient -def get_lean_config_credentials() -> tuple[str, str]: +def get_lean_config_credentials() -> Tuple[str, str]: """Retrieve the QuantConnect credentials from the Lean CLI configuration. This function accesses the Lean CLI configuration manager to obtain the @@ -39,7 +38,7 @@ def get_lean_config_credentials() -> tuple[str, str]: return user_id, api_token -def get_credentials(user_id: Optional[str], api_token: Optional[str], show_secrets: bool) -> tuple[str, str]: +def get_credentials(user_id: Optional[str], api_token: Optional[str], show_secrets: bool) -> Tuple[str, str]: """Fetch user credentials, prompting the user if necessary.""" logger = container.logger credentials_storage = container.credentials_storage From 7f910fae3e970a125a5620107ea6c0d65ca8f29e Mon Sep 17 00:00:00 2001 From: Romazes Date: Thu, 15 Aug 2024 16:38:00 +0300 Subject: [PATCH 3/3] test:feat: add authenticated procees in mock of init tests --- tests/commands/test_init.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/commands/test_init.py b/tests/commands/test_init.py index f39c8ce0..248a8e9e 100644 --- a/tests/commands/test_init.py +++ b/tests/commands/test_init.py @@ -80,6 +80,9 @@ def mock_get_organization(org_id: str) -> QCMinimalOrganization: # container.api_client is already a mock from set_unauthenticated() api_client = container.api_client + container.cli_config_manager.user_id.set_value("123") + container.cli_config_manager.api_token.set_value("456") + api_client.is_authenticated.return_value = True api_client.organizations.get_all.side_effect = _get_all_organizations api_client.organizations.get.side_effect = mock_get_organization