From 495b447cc262c568c7281a743d46fc87ebccad99 Mon Sep 17 00:00:00 2001 From: Tito Brasolin Date: Thu, 14 Nov 2024 13:56:20 +0100 Subject: [PATCH] feat: login command --- scp_udg_client_app/cli.py | 4 +++- scp_udg_client_app/commands/login.py | 36 ++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 scp_udg_client_app/commands/login.py diff --git a/scp_udg_client_app/cli.py b/scp_udg_client_app/cli.py index 5a22a40..918480a 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 0000000..35269c1 --- /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) -- GitLab