From fab5ef97e307c16d30282c575c3f5cdf75b50617 Mon Sep 17 00:00:00 2001 From: Gustavo Padovan Date: Thu, 16 Jan 2025 11:48:41 -0300 Subject: [PATCH] maestro_common: move function to show api error in Consolidate our API error message func. This will facilitate future usage. Signed-off-by: Gustavo Padovan --- kcidev/libs/common.py | 4 ++++ kcidev/libs/maestro_common.py | 11 +++++++++++ kcidev/subcommands/checkout.py | 15 ++------------- kcidev/subcommands/testretry.py | 11 +---------- 4 files changed, 18 insertions(+), 23 deletions(-) diff --git a/kcidev/libs/common.py b/kcidev/libs/common.py index 8fcb08d..cdb0c4f 100644 --- a/kcidev/libs/common.py +++ b/kcidev/libs/common.py @@ -66,6 +66,10 @@ def kci_msg(content): click.echo(content) +def kci_warning(content): + click.secho(content, fg="yellow") + + def kci_err(content): click.secho(content, fg="red", err=True) diff --git a/kcidev/libs/maestro_common.py b/kcidev/libs/maestro_common.py index 422cde6..2e29b76 100644 --- a/kcidev/libs/maestro_common.py +++ b/kcidev/libs/maestro_common.py @@ -12,3 +12,14 @@ def maestro_print_api_call(host, data=None): click.secho("maestro api endpoint: " + host, fg="green") if data: kci_msg(json.dumps(data, indent=4)) + + +def maestro_api_error(response): + kci_err(f"API response error code: {response.status_code}") + try: + kci_err(response.json()) + except json.decoder.JSONDecodeError: + kci_warning(f"No JSON response. Plain text: {response.text}") + except Exception as e: + kci_err(f"API response error: {e}: {response.text}") + return diff --git a/kcidev/subcommands/checkout.py b/kcidev/subcommands/checkout.py index bb6e35b..64f01ef 100644 --- a/kcidev/subcommands/checkout.py +++ b/kcidev/subcommands/checkout.py @@ -16,17 +16,6 @@ from kcidev.libs.maestro_common import * -def display_api_error(response): - kci_err(f"API response error code: {response.status_code}") - try: - kci_err(response.json()) - except json.decoder.JSONDecodeError: - click.secho(f"No JSON response. Plain text: {response.text}", fg="yellow") - except Exception as e: - kci_err(f"API response error: {e}: {response.text}") - return - - def send_checkout_full(baseurl, token, **kwargs): url = baseurl + "api/checkout" headers = { @@ -50,7 +39,7 @@ def send_checkout_full(baseurl, token, **kwargs): return if response.status_code != 200: - display_api_error(response) + maestro_api_error(response) return None return response.json() @@ -71,7 +60,7 @@ def retrieve_treeid_nodes(baseurl, token, treeid): return None if response.status_code >= 400: - display_api_error(response) + maestro_api_error(response) return None return response.json() diff --git a/kcidev/subcommands/testretry.py b/kcidev/subcommands/testretry.py index a908913..c4b1d3e 100644 --- a/kcidev/subcommands/testretry.py +++ b/kcidev/subcommands/testretry.py @@ -11,15 +11,6 @@ from kcidev.libs.maestro_common import * -def display_api_error(response): - kci_err(f"API response error code: {response.status_code}") - try: - kci_err(response.json()) - except json.decoder.JSONDecodeError: - click.secho(f"No JSON response. Plain text: {response.text}", fg="yellow") - return - - def send_jobretry(baseurl, jobid, token): url = baseurl + "api/jobretry" headers = { @@ -36,7 +27,7 @@ def send_jobretry(baseurl, jobid, token): return if response.status_code != 200: - display_api_error(response) + maestro_api_error(response) return None return response.json()