Skip to content

Commit

Permalink
maestro_common: move function to show api error in
Browse files Browse the repository at this point in the history
Consolidate our API error message func. This will
facilitate future usage.

Signed-off-by: Gustavo Padovan <[email protected]>
  • Loading branch information
padovan committed Jan 16, 2025
1 parent 3ffb47e commit fab5ef9
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 23 deletions.
4 changes: 4 additions & 0 deletions kcidev/libs/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
11 changes: 11 additions & 0 deletions kcidev/libs/maestro_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 2 additions & 13 deletions kcidev/subcommands/checkout.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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()

Expand All @@ -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()
Expand Down
11 changes: 1 addition & 10 deletions kcidev/subcommands/testretry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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()

Expand Down

0 comments on commit fab5ef9

Please sign in to comment.