forked from QuantConnect/lean-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
login.py
56 lines (43 loc) · 2.31 KB
/
login.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
# Lean CLI v1.0. Copyright 2021 QuantConnect Corporation.
#
# Licensed 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 typing import Optional
import click
from lean.click import LeanCommand
from lean.container import container
from lean.models.errors import MoreInfoError
@click.command(cls=LeanCommand)
@click.option("--user-id", "-u", type=str, help="QuantConnect user id")
@click.option("--api-token", "-t", type=str, help="QuantConnect API token")
def login(user_id: Optional[str], api_token: Optional[str]) -> 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`.
"""
logger = container.logger()
credentials_storage = container.credentials_storage()
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")
logger.info("You can request these credentials on https://www.quantconnect.com/account")
logger.info(f"Both will be saved in {credentials_storage.file}")
if user_id is None:
user_id = click.prompt("User id")
if api_token is None:
api_token = logger.prompt_password("API token")
api_client = container.api_client(user_id=user_id, api_token=api_token)
if not api_client.is_authenticated():
raise MoreInfoError("Credentials are invalid",
"https://www.lean.io/docs/lean-cli/initialization/authenticating-accounts#02-Log-In")
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")