diff --git a/scp_udg_client_app/cli.py b/scp_udg_client_app/cli.py index 5a22a4017e5f6027119f0d9d33051b317b61a0f0..918480aa8093cbb0cdc59ce12209662f0c4254b2 100644 --- a/scp_udg_client_app/cli.py +++ b/scp_udg_client_app/cli.py @@ -6,7 +6,7 @@ from logging.handlers import RotatingFileHandler import click import click_log -from scp_udg_client_app.commands import last_request, push, report, searching_request, test +from scp_udg_client_app.commands import last_request, login, push, report, searching_request, test # Constants LOG_FORMAT = "%(asctime)s - %(levelname)s [%(funcName)s] %(message)s" @@ -47,6 +47,8 @@ def cli(ctx, log_file, log_max_bytes, log_backup_count): # noinspection PyTypeChecker cli.add_command(last_request.last_request) # noinspection PyTypeChecker +cli.add_command(login.login) +# noinspection PyTypeChecker cli.add_command(push.push) # noinspection PyTypeChecker cli.add_command(report.report) diff --git a/scp_udg_client_app/commands/login.py b/scp_udg_client_app/commands/login.py new file mode 100644 index 0000000000000000000000000000000000000000..35269c1b295f0535975bceeba9e46a7f0d420e1d --- /dev/null +++ b/scp_udg_client_app/commands/login.py @@ -0,0 +1,36 @@ +import logging + +import click +from scp_udg_client_rest import LoginRequest + +from scp_udg_client_app import options +from scp_udg_client_app.helpers import create_api_client_instance +from scp_udg_client_app.options import udg_endpoint_option, login_options + +# Setting up Logger +logger = logging.getLogger(__name__) + + +@click.command(context_settings={'show_default': True}) +@options.config_option +@udg_endpoint_option +@login_options +@click.option('--token-file', type=click.File('w'), + help="File name to save the successful login response in JSON format.") +@click.pass_context +def login(ctx, udg_endpoint, username, password, token_file): + api_client = create_api_client_instance(udg_endpoint=udg_endpoint) + try: + login_request = LoginRequest(username=username, password=password) + response = api_client.login(login_request) + msg = "The response of UrbanDatasetGatewayApi->login: %s" % response.to_str() + if response.code == "01": + logger.info(msg) + if token_file is not None: + logger.info("Saving JSON response to file") + token_file.write(response.to_json()) + else: + logger.warning(msg) + + except Exception as e: + logger.error("Exception when calling UrbanDatasetGatewayApi->login: %s" % e)