From 9741b1b59f83307f94ac4a67651b6ab9adaa4edf Mon Sep 17 00:00:00 2001 From: Tito Brasolin Date: Wed, 30 Oct 2024 15:58:48 +0100 Subject: [PATCH] fix: better handling of lastRequest failure --- schema/openapi3_0.yaml | 5 ++++- scp_udg_client_app/commands/fetch.py | 6 +++--- scp_udg_client_app/commands/fetch_last.py | 14 +++++++++----- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/schema/openapi3_0.yaml b/schema/openapi3_0.yaml index 91f0e7b..6965381 100644 --- a/schema/openapi3_0.yaml +++ b/schema/openapi3_0.yaml @@ -769,7 +769,10 @@ components: - type: object properties: dataset: - $ref: '#/components/schemas/ScpsUrbandatasetSchema20' + anyOf: + - $ref: '#/components/schemas/ScpsUrbandatasetSchema20' + - type: object + properties: {} required: - dataset securitySchemes: diff --git a/scp_udg_client_app/commands/fetch.py b/scp_udg_client_app/commands/fetch.py index c1bc89e..ee9eeeb 100755 --- a/scp_udg_client_app/commands/fetch.py +++ b/scp_udg_client_app/commands/fetch.py @@ -23,7 +23,7 @@ def fetch_and_save_datasets(api_client, request, resource_id, inbox_dir): for dataset in response.dataset: save_dataset(dataset, resource_id, inbox_dir) else: - logger.warning("Dataset non trovati!") + logger.warning(response.to_str()) @click.command() @@ -32,7 +32,7 @@ def fetch_and_save_datasets(api_client, request, resource_id, inbox_dir): help="Read configuration from a Java .properties FILE.") @click.option('--udg-endpoint', type=UrlParamType(may_have_port=True), default=Configuration().host, show_default=True, help="URL endpoint for the Urban Dataset Gateway API.") -@click.option('--resource-id', prompt=True, prompt_required=False, callback=validate_resource_id) +@click.option('--resource-id', prompt=True, prompt_required=False, callback=validate_resource_id, show_default=True) @click.option('--username', 'user_name') @click.option('--password', 'user_password') @click.option('--access-token', hidden=True, prompt=True, prompt_required=False) @@ -53,7 +53,7 @@ def fetch(ctx, udg_endpoint, resource_id, access_token, user_name, user_password """Fetch and save datasets from an UDG endpoint.""" api_client = create_api_client_instance(udg_endpoint, access_token) - if not active_login(api_client, user_name, user_password): + if user_name and not active_login(api_client, user_name, user_password): logger.warning("Login fallito!") return diff --git a/scp_udg_client_app/commands/fetch_last.py b/scp_udg_client_app/commands/fetch_last.py index fb19c25..f9cdb09 100755 --- a/scp_udg_client_app/commands/fetch_last.py +++ b/scp_udg_client_app/commands/fetch_last.py @@ -12,6 +12,9 @@ from scp_udg_client_app.helpers import create_api_client_instance, active_login, # Setting up Logger logger = logging.getLogger() +# Introduce constant +RESPONSE_CODE_SUCCESS = "03" + @click.command() @click_config_file.configuration_option(cmd_name="scp-udg-client", provider=javaproperties_provider, @@ -19,16 +22,17 @@ logger = logging.getLogger() help="Read configuration from a Java .properties FILE.") @click.option('--udg-endpoint', type=UrlParamType(may_have_port=True), default=Configuration().host, show_default=True, help="URL endpoint for the Urban Dataset Gateway API.") -@click.option('--resource-id', prompt=True, prompt_required=False, callback=validate_resource_id) +@click.option('--resource-id', prompt=True, prompt_required=False, callback=validate_resource_id, show_default=True) @click.option('--username', 'user_name') @click.option('--password', 'user_password') @click.option('--access-token', hidden=True, prompt=True, prompt_required=False) @click.option('--inbox-dir', type=click.Path(resolve_path=True, file_okay=False), default="Inbox", show_default=True) +@click.pass_context def fetch_last(ctx, udg_endpoint, resource_id, access_token, user_name, user_password, inbox_dir): """Fetch and save the last dataset from an UDG endpoint.""" api_client = create_api_client_instance(udg_endpoint, access_token) - if not active_login(api_client, user_name, user_password): + if user_name and not active_login(api_client, user_name, user_password): logger.warning("Login fallito!") return @@ -40,10 +44,10 @@ def fetch_last(ctx, udg_endpoint, resource_id, access_token, user_name, user_pas def process_response(response, resource_id, inbox_dir): - if response.code == "03": - save_dataset(response.dataset, resource_id, inbox_dir) + if response.code == RESPONSE_CODE_SUCCESS: + save_dataset(response.dataset.actual_instance, resource_id, inbox_dir) else: - logger.warning("Dataset non trovato!") + logger.warning(response.to_json()) if __name__ == '__main__': -- GitLab