Skip to content

Commit

Permalink
refactor: use negative local id if cloud id is not provided
Browse files Browse the repository at this point in the history
  • Loading branch information
Romazes committed Dec 20, 2024
1 parent 168e375 commit e0e6b54
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 27 deletions.
4 changes: 2 additions & 2 deletions lean/components/api/auth0_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def read(self, brokerage_id: str) -> QCAuth0Authorization:
return QCAuth0Authorization(authorization=None)

@staticmethod
def authorize(brokerage_id: str, logger: Logger, project_id: str = None) -> None:
def authorize(brokerage_id: str, logger: Logger, project_id: int) -> None:
"""Starts the authorization process for a brokerage.
:param brokerage_id: the id of the brokerage to start the authorization process for
Expand All @@ -63,7 +63,7 @@ def authorize(brokerage_id: str, logger: Logger, project_id: str = None) -> Non

full_url = f"{API_BASE_URL}live/auth0/authorize?brokerage={brokerage_id}"

if project_id:
if project_id > 0:
full_url += f"&projectId={project_id}"

logger.info(f"Please open the following URL in your browser to authorize the LEAN CLI.")
Expand Down
2 changes: 1 addition & 1 deletion lean/components/config/project_config_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def get_cloud_id(self, project_directory: Path) -> int:
"""
project_config = self.get_project_config(project_directory)

return project_config.get("cloud-id", default=0)
return project_config.get("cloud-id") or -project_config.get("local-id")

def get_latest_live_directory(self, project_directory: Path) -> Path:
"""Returns the path of the latest live directory.
Expand Down
2 changes: 1 addition & 1 deletion lean/components/util/auth0_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from lean.components.util.logger import Logger


def get_authorization(auth0_client: Auth0Client, brokerage_id: str, logger: Logger, project_id: str = None) -> QCAuth0Authorization:
def get_authorization(auth0_client: Auth0Client, brokerage_id: str, logger: Logger, project_id: int) -> QCAuth0Authorization:
"""Gets the authorization data for a brokerage, authorizing if necessary.
:param auth0_client: An instance of Auth0Client, containing methods to interact with live/auth0/* API endpoints.
Expand Down
24 changes: 1 addition & 23 deletions lean/models/json_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,27 +175,6 @@ def convert_variable_to_lean_key(self, variable_key: str) -> str:
"""
return variable_key.replace('_', '-')

def validate_project_id(self, project_id: int, display_name: str) -> str:
"""
Validates the given project ID and raises an exception for specific conditions.
Args:
project_id (int): The project ID to validate.
display_name (str): The display name of the module.
Returns:
int: The validated project ID.
Raises:
NotImplementedError: If the project ID is 0 and the display name is "CharlesSchwab".
"""
if project_id == 0 and display_name.strip().lower() == "CharlesSchwab".lower():
raise NotImplementedError(
"Cloud ID is missing from the project configuration for the module 'CharlesSchwabBrokerage'. "
"Please ensure the project is synchronized with the cloud by pushing or pulling the project."
)
return str(project_id)

def config_build(self,
lean_config: Dict[str, Any],
logger: Logger,
Expand Down Expand Up @@ -240,9 +219,8 @@ def config_build(self,
logger.debug(f"skipping configuration '{configuration._id}': no choices available.")
continue
elif isinstance(configuration, AuthConfiguration):
project_id = self.validate_project_id(lean_config.get("project-id"), self._display_name.lower())
auth_authorizations = get_authorization(container.api_client.auth0, self._display_name.lower(),
logger, project_id)
logger, lean_config.get("project-id"))
logger.debug(f'auth: {auth_authorizations}')
configuration._value = auth_authorizations.get_authorization_config_without_account()
for inner_config in self._lean_configs:
Expand Down

0 comments on commit e0e6b54

Please sign in to comment.